Android 在后台模式下开始扫描需要几分钟(使用AltBeacons)
我尝试在后台和前台模式下监视信标,只分配第一个ID,然后获取检测到的信标的完整UUID 使用didEnterRegion方法,该区域的第二个ID为空,因此我所做的是在进入一个区域时开始测距信标,以检测哪个是第二个IDAndroid 在后台模式下开始扫描需要几分钟(使用AltBeacons),android,ibeacon-android,altbeacon,android-ibeacon,Android,Ibeacon Android,Altbeacon,Android Ibeacon,我尝试在后台和前台模式下监视信标,只分配第一个ID,然后获取检测到的信标的完整UUID 使用didEnterRegion方法,该区域的第二个ID为空,因此我所做的是在进入一个区域时开始测距信标,以检测哪个是第二个ID public class BeaconListener extends Application implements BootstrapNotifier { private static final String TAG = "BEACON TEST"; private Regio
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) { }
}
公共类BeaconListener扩展应用程序实现BootstrapNotifier{
专用静态最终字符串标记=“信标测试”;
私有区域引导区域引导;
private MonitoringActivity MonitoringActivity=null;
私人区域;
私人BeaconManager BeaconManager;
私有字符串UUID;
public void onCreate(){
super.onCreate();
beaconManager=beaconManager.getInstanceForApplication(此应用程序);
beaconManager.GetBeaconParser().add(新建BeaconParser()。
立根布置(“s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19”);
beaconManager.setbackgroundscanneperiod(1100);
beaconManager.Setbackground在扫描周期之间(0);
beaconManager.SetAndRoidScanningDisabled(true);
beaconManager.setBackgroundMode(真);
mRegion=新区域(“信标”,Identifier.parse(“0xFFFFFFFFFFFFFFFF”),null,null);
regionBootstrap=新的regionBootstrap(this,mRegion);
}
@凌驾
公共区域(区域arg0){
//在本例中,每当出现信标时,此类都会向用户发送通知
//首先会看到匹配的区域(定义见上文)。
试一试{
StarTrangBeaconRegion(mRegion)信标管理器;
setRangeNotifier(新的RangeNotifier(){
@凌驾
公共无效范围信标区域(收集信标,区域arg0){
试一试{
用于(信标:信标){
站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长站长;
发送通知();
}
}捕获(例外e){
Log.i(标记,例如getMessage());
}
}
});
StarTrangBeaconRegion(mRegion)信标管理器;
beaconManager.setbackgroundscanneperiod(1100);
beaconManager.Setbackground在扫描周期之间(0);
beaconManager.SetAndRoidScanningDisabled(true);
}捕获(远程异常e){}
}
这工作正常,我可以获取检测到的信标的完整UUID,但是,当我关闭应用程序或将其置于后台模式时,需要几分钟(大约5分钟)重新启动监控服务。在进入后台或关闭应用程序后,有没有办法立即重新启动服务?当我将设备连接到充电器时,它会重新启动服务并快速找到信标
PS:当我谈到第一个ID和第二个ID时,我认为UUID=ID1+ID2您是正确的,在用户使用终止应用程序后,最多需要五分钟来重新启动扫描。无法配置此间隔,但可以根据您的使用情况在代码中更改此间隔 请理解,当您使用任务切换器终止应用程序时,操作系统将停止与该应用程序关联的所有进程,包括后台服务,如库用于扫描信标的后台服务。库使用两种不同的技术重新启动此扫描服务:
/**
* Created by Hiren Patel on 3/25/2017.
*/
public class MyApplication extends Application implements BootstrapNotifier {
private BeaconManager iBeaconManager;
private RegionBootstrap regionBootstrap;
@Override
public void onCreate() {
super.onCreate();
stopBeaconMonitoring();
startBeaconMonitoring();
}
public void stopBeaconMonitoring() {
Log.i("Application", "stopBeaconMonitoring");
if (iBeaconManager != null) {
iBeaconManager.removeAllMonitorNotifiers();
iBeaconManager.removeAllRangeNotifiers();
iBeaconManager = null;
}
}
private void startBeaconMonitoring() {
iBeaconManager = BeaconManager.getInstanceForApplication(this);
iBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
iBeaconManager.setBackgroundBetweenScanPeriod(5000l);
iBeaconManager.setBackgroundScanPeriod(1000l);
iBeaconManager.setDebug(true);
//Start the Services that will keep checking for beacons in the background, even if the application is closed (swiped away from the app-switcher).
Intent startServiceIntent = new Intent(this.getApplicationContext(), BeaconService.class);
this.getApplicationContext().startService(startServiceIntent);
startServiceIntent = new Intent(this.getApplicationContext(), BeaconIntentProcessor.class);
this.getApplicationContext().startService(startServiceIntent);
List<BeaconEntity> beaconEntityList = YourBeaconListHere;
if (beaconEntityList != null) {
for (BeaconEntity beaconEntity : beaconEntityList) {
Region region = new Region(beaconEntity.Name, Identifier.parse(beaconEntity.UUID), Identifier.parse(String.valueOf(beaconEntity.Major)), Identifier.parse(String.valueOf(beaconEntity.Minor)));
regionBootstrap = new RegionBootstrap(this, region);
Log.i("Beacons List", beaconEntity.Name + "-" + beaconEntity.UUID + "-" + String.valueOf(beaconEntity.Major) + "-" + String.valueOf(beaconEntity.Minor));
}
}else{
Log.i("Beacons List", "null");
}
}
@Override
public void didEnterRegion(Region region) {
// You entered in Region(beacon range)
Log.i("Application", "didEnterRegion called");
}
@Override
public void didExitRegion(Region region) {
// You exited from Region(beacon range)
Log.i("Application", "didExitRegion called");
}
@Override
public void didDetermineStateForRegion(int i, Region region) {
// Region state changed, just ignore it
Log.i("Application", "didDetermineStateForRegion called");
}
}
/**
*由Hiren Patel于2017年3月25日创建。
*/
公共类MyApplication扩展应用程序实现BootstrapNotifier{
私人BeaconManager iBeaconManager;
私有区域引导区域引导;
@凌驾
public void onCreate(){
super.onCreate();
stopBeaconMonitoring();
startBeaconMonitoring();
}
公共无效stopBeaconMonitoring(){
Log.i(“应用程序”、“stopBeaconMonitoring”);
if(iBeaconManager!=null){
IBMeaconManager.removeAllMonitorNotifiers();
IBMeaconManager.removeAllRangeNotifiers();
iBeaconManager=null;
}
}
私有void startBeaconMonitoring(){
iBeaconManager=BeaconManager.getInstanceForApplication(此应用程序);
iBeaconManager.getBeaconParsers().add(新的BeaconParser().setBeaconLayout(“m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24”);
iBeaconManager.扫描周期之间的挫折(5000l);
iBeaconManager.setbackgroundscanneperiod(1000l);
iBeaconManager.setDebug(true);
//启动将在后台持续检查信标的服务,即使应用程序已关闭(从应用程序切换器刷走)。
Intent startServiceIntent=新的Intent(this.getApplicationContext(),beacervice.class);
this.getApplicationContext().startService(startServiceIntent);
圣