我在后台启动应用程序部分使用此页面上的示例代码(http://altbeacon.github.io/android-beacon-library/samples.html),并且我有一个工作应用程序。
即使在背景上,它也会检测到信标是否在范围内。
问题是我需要知道它是哪个信标(UUID、Major、Minor),然后将其与我的本地数据库进行匹配,并在应用程序仍在后台运行时发出通知。
didEnterRegion(Region region) 函数只有一个 matchesBeacon 方法,我尝试执行以下操作来识别哪些信标正在被看到,但它抛出了 NullPointerException:
public class SightSeeing extends Activity implements BootstrapNotifier, RangeNotifier {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Region region = new Region("sightRegion", null, null, null);
regionBootstrap = new RegionBootstrap(this, region);
BeaconManager.getInstanceForApplication(this).getBeaconParsers().add(
new BeaconParser(). setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")
);
BeaconManager.getInstanceForApplication(this).setRangeNotifier(this);
}
@Override
public void didEnterRegion(Region region) {
regionBootstrap.disable();
BeaconManager.getInstanceForApplication(this).setRangeNotifier(this);
try {
BeaconManager.getInstanceForApplication(this).startRangingBeaconsInRegion(region);
}
catch (RemoteException e) {
Log.e(TAG, "Can't start ranging");
}
}
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
Iterator<Beacon> beaconIterator = beacons.iterator();
while (beaconIterator.hasNext()) {
Beacon beacon = beaconIterator.next();
//check if beacon exists in our DB and throw notification
}
}
}
我是否遗漏了一些明显的东西,或者这个库不可能做到这一点?
编辑:
我已经更新了代码示例,以便为你们提供更广泛的想法,并且我尝试实施 FOliveira 的建议,但没有任何成功。
编辑2:
更新了代码以反映 davidgyoung 的建议。仍然没有运气。我在 didRangeBeaconsInRegion() 函数的第一行有一个 Log.d() ,它没有被调用。
我试过添加 BeaconManager.getInstanceForApplication(this).setRangeNotifier(this); 在 try/catch 块之前,结果是一样的。
我是否错误地执行了这个建议,或者有没有其他方法可以让它发挥作用?