Search code examples
androidbluetoothbeaconeddystone

Android Beacon Library for EddyStone beacons


I am new to scanning beacons using Android. I am using Library mentioned above, which is easy enough to understand. The reference application works and detects my Eddystone beacons. Based on the sample code, I wrote a simple app to detect Eddystone beacons. It does not work. On the logcat, I get the following messages; D/BluetoothAdapter: STATE_ON D/BluetoothLeScanner: could not find callback wrapper

Here is my code, can anyone tell, what am I doing wrong. Please Help.

       public class MainActivity extends AppCompatActivity implements BeaconConsumer {
private BeaconManager beaconManager;
Identifier myBeaconNamespaceId;
Identifier myBeaconInstanceId;
Region region;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myBeaconNamespaceId = Identifier.parse("0x334652820242ac130002");
    myBeaconInstanceId = Identifier.parse("0x987654321cba");
    beaconManager = BeaconManager.getInstanceForApplication(this.getApplicationContext());
    beaconManager.getBeaconParsers().clear();
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
   // beaconManager.setDebug(true);

    region = new Region("MyRegion",
            myBeaconNamespaceId, myBeaconInstanceId, null);
    beaconManager.bind(this);

}

@Override
protected void onDestroy() {
    super.onDestroy();
    beaconManager.unbind(this);
}


@Override
public void onBeaconServiceConnect() {
    beaconManager.removeAllRangeNotifiers();
    beaconManager.addRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            if (beacons.size() > 0) {

                Log.i("INFORMATION", "The first beacon I see is about "+beacons.iterator().next().getDistance()+" meters away.");
            }
        }
    });

    try {
        beaconManager.startRangingBeaconsInRegion(new Region("MyRegion", myBeaconNamespaceId, myBeaconInstanceId, null));
    } catch (RemoteException e) {
        Toast.makeText(getApplicationContext(), e.getMessage()  ,Toast.LENGTH_LONG).show();
    }
}

private void logToDisplay(final String line) {
    runOnUiThread(new Runnable() {
        public void run() {
            EditText editText = (EditText)MainActivity.this.findViewById(R.id.rangingText);
            editText.append(line+"\n");
        }
    });
}
}

Solution

  • Make sure you have obtained location permission from the user by dynamically requesting it:

    https://altbeacon.github.io/android-beacon-library/requesting_permission.html

    If that does not work I have a number of other troubleshooting steps here:

    https://altbeacon.github.io/android-beacon-library/detection-trouble.html