我正在用AltBeacon开发一个安卓项目,在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());
}发布于 2016-01-28 16:02:34
所有来自信标的初始化都应该在OnBeaconServiceConnect()中进行,我在使用DidExitRegion()和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());
}
}https://stackoverflow.com/questions/30982394
复制相似问题