【发布时间】:2015-11-23 11:10:43
【问题描述】:
我正在尝试在后台和前台模式下监控信标,方法是仅分配第一个 ID,然后获取检测到的信标的完整 UUID。
使用方法 didEnterRegion,该区域的第二个 ID 为空,所以我所做的是在进入一个区域时开始测距信标以检测哪个是第二个 ID。
public class BeaconListener extends Application implements BootstrapNotifier {
private static final String TAG = "BEACON TEST";
private RegionBootstrap regionBootstrap;
private MonitoringActivity monitoringActivity = null;
private Region mRegion;
private BeaconManager beaconManager;
private String UUID;
public void onCreate() {
super.onCreate();
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));
beaconManager.setBackgroundScanPeriod(1100);
beaconManager.setBackgroundBetweenScanPeriod(0);
beaconManager.setAndroidLScanningDisabled(true);
beaconManager.setBackgroundMode(true);
mRegion = new Region("Beacon", Identifier.parse("0xffffffffffffffffffff"), null, null);
regionBootstrap = new RegionBootstrap(this, mRegion);
}
@Override
public void didEnterRegion(Region arg0) {
// In this example, this class sends a notification to the user whenever a Beacon
// matching a Region (defined above) are first seen.
try {
beaconManager.startRangingBeaconsInRegion(mRegion);
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region arg0) {
try {
for (Beacon beacon : beacons) {
beaconManager.stopRangingBeaconsInRegion(mRegion);
sendNotification();
}
} catch (Exception e) {
Log.i(TAG, e.getMessage());
}
}
});
beaconManager.startRangingBeaconsInRegion(mRegion);
beaconManager.setBackgroundScanPeriod(1100);
beaconManager.setBackgroundBetweenScanPeriod(0);
beaconManager.setAndroidLScanningDisabled(true);
} catch (RemoteException e) { }
}
这很好用,我可以获得检测到的信标的完整 UUID,但是当我终止应用程序或将其置于后台模式时,需要几分钟(大约 5 分钟)才能重新启动监控服务.有什么方法可以在进入后台或杀死应用程序后立即重新启动服务?当我将设备连接到充电器时,它会重新启动服务并再次快速找到信标。
PS:当我谈到第一个 ID 和第二个 ID 时,我假设 UUID = ID1 + ID2
【问题讨论】:
标签: android ibeacon-android altbeacon android-ibeacon