我正在使用Android IBeacon Library并尝试获取该区域中的iBeacons数量。
使用测距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) { }
最佳答案
开始测距时,您是否提供iBeacon的接近性UUID?
我的代码如下所示:
String proximityUUID = "xxxxxx";
iBeaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", proximityUUID, null, null));
然后在
didRangeBeaconsInRegionenter
中:if (iBeacons.size() > 0) {
for (IBeacon iBeacon : iBeacons) {
logToDisplay(String.format("( %.2f m ) %d", iBeacon.getAccuracy(), iBeacon.getProximity()));
}
}
在这里,您应该获得每个看到的iBeacon的准确性和接近性。