【发布时间】:2018-01-13 19:06:50
【问题描述】:
[### 预期行为
只有在信标实际退出该区域时,该库才应触发退出。
实际行为
lib 触发退出并进入信标仍在范围内的区域方法。在 5-7 的所有 android 版本上,这种情况每隔 20 秒发生一次,有时频率更高,有时频率更低
重现此行为的步骤
只要打开蓝牙,我就会在后台连续运行信标应用程序 打开搜索信标,信标固定在车内,并且以 1000 秒的间隔广播在用户从未离开信标区域的情况下连续报告信标进入和退出。
我的代码sn-p
public class BackgroundBeaconScan extends Service implements BootstrapNotifier, BeaconConsumer {
@Override
public void onCreate() {
super.onCreate();
AppUtils.isBGServiceActive = true;
//AppUtils.appTerminated = false;
Log.e("Beacon", "Service Start");
Log.e("Bfpk", " *** Beacon Service is started *** ");
LoggingOperations logger = new LoggingOperations();
Thread.setDefaultUncaughtExceptionHandler(logger);
mBeaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
try {
Log.e("BG", "iam in mBeaconManager to unbind it");
mBeaconManager.unbind(this);
mBeaconManager.removeAllRangeNotifiers();
mBeaconManager.removeAllMonitorNotifiers();
mBeaconManager.removeMonitoreNotifier(this);
} catch (OutOfMemoryError e) {
Log.e("BG", "iam in mBeaconManager to unbind it excep ");
}
mBeaconManager.getBeaconParsers().clear();
//set Beacon Layout for Eddystone-UID packet
mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
// rideBDD = new RideBDD(getApplicationContext());
mBeaconManager.setForegroundScanPeriod(20001);
mBeaconManager.setForegroundBetweenScanPeriod(50001);
mBeaconManager.setBackgroundScanPeriod(20001);
mBeaconManager.setBackgroundBetweenScanPeriod(50001);
mBeaconManager.setBackgroundMode(true);
mBeaconManager.setDebug(true);
BeaconManager.setAndroidLScanningDisabled(true);
notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
namespace = DataHandler.getStringPreferences(AppConstants.UUID_NAME_SPACE);
instanceID = DataHandler.getStringPreferences(AppConstants.UUID_INSTANCE_ID);
Log.e("BG", "namespace :" + namespace);
Log.e("BG", "instanceID :" + instanceID);
Long currentTime = System.currentTimeMillis();
String time = formateLongToOnlyDateForServer(currentTime);
LoggingOperations.writeToFile(BackgroundBeaconScan.this,"UUID > " + time + " -- > " + namespace + " - " + instanceID);
try {
Log.e("BG", "iam in mBeaconManager.isBound");
mBeaconManager.bind(this);
Log.e("BG", "iam in mBeaconManager.bind");
} catch (OutOfMemoryError e) {
Log.e("BG", "iam in mBeaconManager.bindexce", e);
}
}
我的服务连接
@Override
public void onBeaconServiceConnect() {
// String preiBeaconUUID = DataHandler.getStringPreferences(AppConstants.UUID);
// postiBeaconUUID=AppUtils.addDashes(preiBeaconUUID);
Region region;
try {
Log.e("BG", "iam in onBeaconServiceConnect");
namespace = DataHandler.getStringPreferences(AppConstants.UUID_NAME_SPACE);
instanceID = DataHandler.getStringPreferences(AppConstants.UUID_INSTANCE_ID);
Identifier myBeaconNamespaceId = Identifier.parse(namespace);
Identifier myBeaconInstanceId = Identifier.parse(instanceID);
region = new Region("EdstUIDAdvertising", myBeaconNamespaceId, myBeaconInstanceId, null);
mBeaconManager.addMonitorNotifier(this);
Log.e("BG", "iam in startMonitoringBeaconsInRegion");
} catch (Exception e) {
Log.e("BG", "iam in startMonitoringBeaconsInRegionExce" + e);
e.printStackTrace();
namespace = DataHandler.getStringPreferences(AppConstants.UUID_NAME_SPACE);
instanceID = DataHandler.getStringPreferences(AppConstants.UUID_INSTANCE_ID);
Identifier myBeaconNamespaceId = Identifier.parse(namespace);
/* Identifier myBeaconInstanceId = Identifier.parse(instanceID);*/
region = new Region("EdstUIDAdvertising", myBeaconNamespaceId, null, null);
mBeaconManager.addMonitorNotifier(this);
Log.e("BG", "iam in startMonitoringBeaconsInRegion");
}
AppUtils.isBGServiceActive = true;
try {
mBeaconManager.startMonitoringBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
mBeaconManager.addMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
Log.e("Monitored", "entered"); this fires at the same time
enterCount++;
}
@Override
public void didExitRegion(Region region) {
Log.e("Monitored", "exited"); // this fires at the same time
}
@Override
public void didDetermineStateForRegion(int i, Region region) {
}
});
}
移动设备型号和操作系统版本
Samsung note 5 (7.0) s6 (6.0) s5 (5.0) 每台设备
Android Beacon 库版本
2.12.4 // 请帮我解决这个问题很久了
【问题讨论】:
标签: android bluetooth-lowenergy altbeacon background-service