我正在开发一个android项目,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());
}
所有来自信标的初始化都应该在OnBeaconServiceConnect()中进行。我在DidExitRegion()和DidEnterRegion(。有时,你也可以在创建时这样做,但对我来说不起作用。它是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());
}
}