我正在使用Android IBeacon 库并尝试获取该地区的 iBeacon 数量。使用测距 api 我得到了设备数。即使所有 iBeacons 和我的手机都静止,这个计数也会不断地从 0 变为 n。此外,当我尝试获取设备的详细信息时,我得到以下异常。是什么导致了异常。当我尝试调试时,我看到数据不为空。
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)
我的代码:
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) { }