我的信标检测有问题 .
有时应用程序循环遍历BootstrapNotifier回调(didDetermineStateForRegion(int arg0,Region arg1),didEnterRegion(Region arg0)和didExitRegion(Region arg0)) .
有时会在我启动应用程序时发生,其他时候是应用程序处于后台时...我没有找到任何模式,因此我不知道问题出在哪里 .
循环就像这样(只有一块日志):
D/MyApplication: Got a didDetermineStateForRegion call: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
D/MyApplication: Got a didEnterRegion call: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
D/MyApplication: Got a didDetermineStateForRegion call: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
D/MyApplication: Got a didExitRegion call: null
D/MyApplication: Got a didDetermineStateForRegion call: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
D/MyApplication: Got a didEnterRegion call: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
D/MyApplication: Got a didDetermineStateForRegion call: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
D/MyApplication: Got a didExitRegion call: null
D/MyApplication: Got a didDetermineStateForRegion call: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
D/MyApplication: Got a didEnterRegion call: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
D/MyApplication: Got a didDetermineStateForRegion call: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
D/MyApplication: Got a didExitRegion call: null
D/MyApplication: Got a didDetermineStateForRegion call: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
D/MyApplication: Got a didEnterRegion call: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
D/MyApplication: Got a didDetermineStateForRegion call: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
D/MyApplication: Got a didExitRegion call: null
D/MyApplication: Got a didDetermineStateForRegion call: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
D/MyApplication: Got a didEnterRegion call: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
D/MyApplication: Got a didDetermineStateForRegion call: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
D/MyApplication: Got a didExitRegion call: null
D/MyApplication: Got a didDetermineStateForRegion call: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
D/MyApplication: Got a didEnterRegion call: 2f234454-cf6d-4a0f-adf2-f4911ba9ffa6
为什么会这样?该应用程序检测到该区域,但不会立即启动信标扫描 . 在这个循环或多或少一分钟之后,扫描通常开始(但不总是) .
更新 - - - - - -
我的代码:
public class BeaconApplication extends Application implements BootstrapNotifier, BeaconConsumer {
private String TAG = "MyApplication";
private BeaconManager beaconManager;
private Region region;
@Override
public void onCreate() {
super.onCreate();
startBeaconScan();
}
public void startBeaconScan() {
Log.d(TAG, "App started up");
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25")); //iBeacon layout
beaconManager.setRegionStatePeristenceEnabled(false);
beaconManager.bind(this);
region = new Region("myMonitoringUniqueId", Identifier.parse("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6"), null, null);
new RegionBootstrap(this, region);
}
@Override
public void didDetermineStateForRegion(int arg0, Region arg1) {
beaconManager.addRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection beacons, Region region) {
//CODE FOR BEACON DETECTION
}
});
try {
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
Log.d(TAG, "Got a didDetermineStateForRegion call: " + arg1.getId1());
}
@Override
public void didEnterRegion(Region arg0) {
Log.d(TAG, "Got a didEnterRegion call: " + arg0.getId1());
}
@Override
public void didExitRegion(Region arg0) {
Log.d(TAG, "Got a didExitRegion call: " + arg0.getBluetoothAddress());
}
@Override
public void onBeaconServiceConnect() {
try {
beaconManager.setBackgroundBetweenScanPeriod(500);
beaconManager.setBackgroundScanPeriod(500);
beaconManager.setForegroundBetweenScanPeriod(500);
beaconManager.setForegroundScanPeriod(500);
beaconManager.updateScanPeriods();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
我试过“beaconManager.startRangingBeaconsInRegion(region);”在“onBeaconServiceConnect()”回调中,但似乎它没有产生任何差异 .