[英]Detect iBeacon with AltBeacon Library on Android
我正在使用AltBeacon库来检测iBeacon设备。 这是我根据AltBeacon的文档使用的代码,但是未检测到该信标:
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
beaconManager.bind(this);
beaconManager.addMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
Log.i("test", "I just saw an beacon for the first time!");
}
@Override
public void didExitRegion(Region region) {
Log.i("test", "I no longer see an beacon");
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
Log.i("test", "I have just switched from seeing/not seeing beacons: "+state);
}
});
try {
beaconManager.startMonitoringBeaconsInRegion(new Region("e2c56db5-dffb-48d2-b060-d0f5a71096e0", null, null, null));
} catch (RemoteException e) { }
此代码是否错误?
两个问题:
问题1:
当您调用beaconManager.bind(this);
封闭的类必须是BeaconConsumer
的实例(通常是Application
或Activity
的实例,否则您需要链接BeaconConsumer
方法。)
关键是您只能调用此块:
try { beaconManager.startMonitoringBeaconsInRegion(new Region("e2c56db5-dffb-48d2-b060-d0f5a71096e0", null, null, null)); } catch (RemoteException e) { }
在您收到对onBeaconServicecConnected
的回调onBeaconServicecConnected
。 此代码通常应在该回调中。 因为异常块为空,并且什么也不记录,所以我怀疑代码会抛出此异常并以静默方式失败。 您应该始终至少将错误记录在异常块中,以帮助查找此类问题。
问题2:
区域的构造函数应如下所示:
new Region("com.mydomain.myapp.region1", Identifier.parse("e2c56db5-dffb-48d2-b060-d0f5a71096e0"), null, null);
请注意,第一个参数是用作区域唯一标识符的字符串,因此您以后可以使用相同的标识符来停止或替换要监视的区域。 第二个参数是ProximityUUID,第三个和第四个参数分别是主要和次要。
标识符在所示代码中的构造方式应该起作用,因为它定义了唯一的id为“ e2c56db5-dffb-48d2-b060-d0f5a71096e0”,且ProximityUUID / major / minor值均为null,因此为通配符。 但是,它具有误导性,因为它表明它正在寻找特定的ProximityUUID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.