2017-09-16 88 views
0

我使用AltBeacon库来检测iBeacon设备。这是我使用的代码,基于AltBeacon的文档,但没有检测到信标:使用Android上的AltBeacon库检测iBeacon

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) { } 

这段代码错了吗?

回答

0

两个问题:

问题1:

当你调用beaconManager.bind(this);封装类必须是BeaconConsumer一个实例(和一般的ApplicationActivity一个实例,否则你需要链BeaconConsumer方法。)

关键的一点是,你可能只调用此块:

try { beaconManager.startMonitoringBeaconsInRegion(new Region("e2c56db5-dffb-48d2-b060-d0f5a71096e0", null, null, null)); } catch (RemoteException e) { }

收到回拨给onBeaconServicecConnected后。这段代码通常应该在回调中。因为异常块是空的并且什么都不记录,所以我怀疑这个代码抛出了这个异常,并且失败了。您应该至少在异常块中记录一个错误,以帮助找到像这样的问题。

问题2:

对区域的构造应该是这样的:

new Region("com.mydomain.myapp.region1", Identifier.parse("e2c56db5-dffb-48d2-b060-d0f5a71096e0"), null, null);

注意,第一个参数是作为一个唯一的标识符该地区的一个字符串,以便以后可以使用相同的标识符来停止或替换正在监控的区域。第二个参数是ProximityUUID,第三个和第四个参数分别是主要和次要参数。

标识符所示应该工作的代码构成,因为它定义了“e2c56db5-dffb-48d2-B060-d0f5a71096e0”与ProximityUUID /其均为空,因此通配符主要/次要值的唯一ID的方式。然而,这是误导性的,因为它表明它不是在寻找特定的ProximityUUID。

+0

我的活动实际上实现了BeaconConsumer事实上的代码编译没有问题。 现在我已经修改了代码: 'beaconManager.startMonitoringBeaconsInRegion(新区域( “com.example.app.region1”, Identifier.parse( “e2c56db5-dffb-48d2-B060-d0f5a71096e0”),空, null));' 但仍然无法正常工作。 另外我已经在try/catch块中添加了一个日志,并且代码没有失败。任何解决方案 – Loris

+0

此外,当我第一次启动应用程序时,事件'didDetermineStateForRegion'被触发,状态参数为0 – Loris

+0

您是否获得了onBeaconServiceConnected的回调? – davidgyoung

相关问题