AltBeacon api 在测距信标时返回空集合
Posted
技术标签:
【中文标题】AltBeacon api 在测距信标时返回空集合【英文标题】:AltBeacon api return empty collection while ranging beacons 【发布时间】:2018-01-30 13:44:11 【问题描述】:我正在尝试对信标进行测距,并且正在按照此处所写的内容进行操作: http://altbeacon.github.io/android-beacon-library/samples.html
didRangeBeaconsInRegion 方法正在触发,但集合始终为空。
我已经通过 altbeacon 安装了“定位”,它找到了 3 个信标。
这是我的代码:我错过了什么吗?
public class BeaconSingletone implements BeaconConsumer
private static BeaconSingletone instance;
private final org.altbeacon.beacon.BeaconManager beaconManager2;
private ArrayList<BeaconThin> listNearBeacons = new ArrayList<>();
BeaconRegion region = new BeaconRegion("ranged region",
UUID.fromString("B9407F30-F5F8-466E-AFF9-25556B57FE6D"), null, null);
private List<EventInterface> listeners = new ArrayList<EventInterface>();
private BeaconSingletone()
beaconManager2 = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(ar_activity.get());
// To detect proprietary beacons, you must add a line like below corresponding to your beacon
// type. Do a web search for "setBeaconLayout" to get the proper expression.
beaconManager2.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
beaconManager2.bind(this);
public static BeaconSingletone getInstance()
if(instance == null)
instance = new BeaconSingletone();
return instance;
private void showNotification(Region region, final List<Beacon> list)
listNearBeacons.clear();
listNearBeacons.add(new BeaconThin(3514,7580,-1));
for (Iterator<EventInterface> i = listeners.iterator(); i.hasNext(); )
EventInterface item = i.next();
//item.NewBeaconFound(list.get(0).getMajor(),list.get(0).getMinor(),Utils.computeAccuracy(list.get(0)));
item.NewBeaconsFound(listNearBeacons);
return;
@Override
public void onBeaconServiceConnect()
beaconManager2.addRangeNotifier(new RangeNotifier()
@Override
public void didRangeBeaconsInRegion(Collection<org.altbeacon.beacon.Beacon> beacons, org.altbeacon.beacon.Region region)
if (beacons.size() > 0)
Log.i("BeaconManager", "The first beacon I see is about "+beacons.iterator().next().getDistance()+" meters away.");
);
try
beaconManager2.startRangingBeaconsInRegion(new org.altbeacon.beacon.Region("mybeacons", null, null, null));
catch (RemoteException e)
@Override
public Context getApplicationContext()
return ar_activity.get();
@Override
public void unbindService(ServiceConnection serviceConnection)
ar_activity.get().unbindService(serviceConnection);
@Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i)
return ar_activity.get().bindService(intent, serviceConnection, i);
【问题讨论】:
【参考方案1】:您需要为您正在使用的信标类型添加信标布局(iBeacon?)。替换这一行:
beaconManager2.getBeaconParsers().add(new BeaconParser(). setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
从这里使用正确的布局: https://beaconlayout.wordpress.com
【讨论】:
是的,这正是问题所在。我不得不将其更改为:""m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24" 谢谢以上是关于AltBeacon api 在测距信标时返回空集合的主要内容,如果未能解决你的问题,请参考以下文章
altbeacon 以 10Hz 连续检测信标 RSSI 值
如何使用 android-beacon-library (altbeacon) 正确停止扫描信标
使用 altBeacon 库在 Android 中未显示所有信标