Search code examples
androidibeacon-android

Android iBeacon Library ranging api


I'm using Android IBeacon Library and trying to get the number of iBeacons in the region. Using the ranging api I get the devices count. This count keeps on changing from 0 to n even when all iBeacons and my phone is still. Also when I try to get the details of the devices I get the following exception. What's causing the exception. When I tried debugging I see the datas are not null.

04-14 11:26:37.203  11754-11883/com.test.ibeacon E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[IBeaconIntentProcessor]
Process: com.test.ibeacon, PID: 11754
java.util.NoSuchElementException
        at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:576)
        at com.test.ibeacon.MainActivity$1.didRangeBeaconsInRegion(MainActivity.java:115)
        at com.radiusnetworks.ibeacon.IBeaconIntentProcessor.onHandleIntent(IBeaconIntentProcessor.java:73)
        at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.os.HandlerThread.run(HandlerThread.java:61)

My Code:

iBeaconManager.setRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
            Log.d(LOG_TAG,"No of devices == "+iBeacons.size());
            if (iBeacons.size() > 0) {
                Log.i(LOG_TAG, "The first iBeacon I see is about "+iBeacons.iterator().next().getAccuracy()+" meters away.");
            }
            Iterator<IBeacon> iterator = iBeacons.iterator();
            while (iterator.hasNext()) {
                Log.d(LOG_TAG,"Major = "+(iterator.next()).getMajor()+" Minor = "+(iterator.next()).getMinor());
            }
        }
    });

    try {
        iBeaconManager.startRangingBeaconsInRegion(new Region("myUniqueId", null, null, null)); //I'm specifying the UniqueId.
    } catch (RemoteException e) {   }

Solution

  • Are you supplying the proximityUUID of your iBeacon when starting ranging? My Code looks like this:

    String proximityUUID = "xxxxxx";
    iBeaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", proximityUUID, null, null));
    

    Then in didRangeBeaconsInRegionenter :

    if (iBeacons.size() > 0) {
        for (IBeacon iBeacon : iBeacons) {                              
            logToDisplay(String.format("( %.2f m ) %d", iBeacon.getAccuracy(), iBeacon.getProximity()));
        }
    }
    

    Here you should get the accuracy and proximity of each seen iBeacon.