我正在使用AltBeacon开发一个Android项目,在GitHub中引用此代码 - https://github.com/justinodwyer/Beacon-Scanner-and-Logger 但是在eclipse中遇到以下问题 -
The BeaconManager is not bound to the service. Call beaconManager.bind(BeaconConsumer consumer) and wait for a callback to onBeaconServiceConnect()
我的代码如下。
BeaconScannerApp app = (BeaconScannerApp)this.getApplication();
beaconManager = app.getBeaconManager();
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);
region = new Region("myRangingUniqueId", null, null, null);
beaconManager.setRangeNotifier(new RangeNotifier() {
@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();
logBeaconData(beacon);
}
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
Log.v("TEST", e.getMessage());
}
答案 0 :(得分:0)
信标的所有初始化都应该在OnBeaconServiceConnect()中进行。我遇到了DoExitRegion()和DidEnterRegion()的问题,这正是因为我没有在OnBeaconServiceConnect()中初始化我的BeaconManager处理程序。你有时也可以在OnCreate上做,但对我来说不行。它是C#,但它非常相似。
这是一个效果很好的例子:
public void OnBeaconServiceConnect()
{
beaconManager.SetForegroundScanPeriod(1000L);
beaconManager.SetForegroundBetweenScanPeriod(10000L);
rangeNotifier.DidRangeBeaconsInRegionComplete += RangeNotifier_DidRangeBeaconsInRegionComplete;
monitorNotifier.EnterRegionComplete += MonitorNotifier_EnterRegionComplete;
monitorNotifier.ExitRegionComplete += MonitorNotifier_ExitRegionComplete;
beaconManager.SetRangeNotifier(rangeNotifier);
beaconManager.SetMonitorNotifier(monitorNotifier);
try
{
beaconManager.StartMonitoringBeaconsInRegion(region);
beaconManager.StartRangingBeaconsInRegion(region);
}catch(RemoteException e)
{
Log.Debug(TAG,e.ToString());
}
}