我的信标检测存在问题。
有时应用程序循环遍历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
为什么会这样?该应用程序检测到该区域,但不会立即启动信标扫描。 在这个循环或多或少一分钟之后,扫描通常开始(但不总是)。
要测试应用程序,我使用此信标软件模拟器作为发送器(https://github.com/WebBluetoothCG/ble-test-peripheral-android)
--------------更新------------
我的代码:
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<Beacon> 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()”回调中,但似乎没有产生任何差异。
答案 0 :(得分:0)
有两件事可能导致这种情况:
设置如此短的扫描周期可能会有问题,除非您肯定您正在测试的信标是以10Hz或更高的频率进行传输。这是因为500ms没有太多时间来检测数据包,当您在扫描打开和关闭之间循环时,无线电会关闭一段时间。我会让扫描周期更长(至少1100毫秒),以确定问题是否消失。
库2.10版中引入了一个错误,可能导致后台退出。我怀疑它会在如此短的扫描周期内解决问题。尝试定位库版本2.9,或者您可以使用instructions to try 2.11-beta1.
其他一些提示:
使用RegionBootstrap时没有理由调用bind(),如果这样做,可能会导致后台模式切换出现问题。您可以将自定义扫描周期代码放在您设置BeaconParser的位置旁边,然后只需将呼叫取出updateScanPeriods()
,因为只有在扫描开始后更改扫描周期才需要它。
除非您激活BackgroundPowerSaver
类或以编程方式更改beaconManager的后台模式,否则不会使用后台扫描周期。除非您执行其中一项操作,否则将始终使用前景扫描周期。