我正在编写一个利用 AltBeacon 的 Android-Beacon-Library 的 Android App 自定义类。
我没有发布课程,而是使用库提供的参考实现示例来重现问题。
如果我直接在 onCreate() 中同时激活监控(通过实现 RegionBootstrap)和测距 beaconManager.bind(new BeaconConsumer()...),一切都会完美运行。
但是,我想仅在 Beacon 进入 region 时才开始测距,因此在 RegionBootstrap 的 didEnterRegion(Region arg0) 回调中,但是当我这样做时,永远不会调用 onBeaconServiceConnect() 。我相信这样做在电池消耗方面会更有效。
我尝试在主线程上强制执行以及使用延迟计时器,但它没有用。
任何人都可以让它工作吗?任何帮助表示赞赏!
public class BeaconReferenceApplication extends Application implements BootstrapNotifier {
private static final String TAG = "AndroidProximityReferenceApplication";
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
BeaconManager beaconManager;
Context context = this;
public void onCreate() {
super.onCreate();
beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
// Setup beacon layout - should be done once only
List<BeaconParser> bp = beaconManager.getBeaconParsers();
bp.add(new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
Region region = new Region("backgroundRegion", null, null, null);
regionBootstrap = new RegionBootstrap(this, region);
backgroundPowerSaver = new BackgroundPowerSaver(this);
// THIS WORKS
bindBeaconConsumer();
}
public void bindBeaconConsumer() {
beaconManager.bind(new BeaconConsumer() {
@Override
public void onBeaconServiceConnect() {
// ISSUE : Does not pass here when bindBeaconConsumer() is called from didEnterRegion
// Set Range Notifier and start ranging
}
@Override
public Context getApplicationContext() {
return context;
}
@Override
public void unbindService(ServiceConnection serviceConnection) {
context.unbindService(serviceConnection);
}
@Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
return context.bindService(intent, serviceConnection, i);
}
});
}
@Override
public void didEnterRegion(Region arg0) {
Log.d(TAG, "did enter region.");
// THIS DOESN'T WORK : onBeaconServiceConnect() never called
// bindBeaconConsumer();
}
@Override
public void didExitRegion(Region region) {
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
}
}