关闭应用程序后,AltBeacon无法检测到信标 [英] AltBeacon not detect beacon when app is closed

查看:128
本文介绍了关闭应用程序后,AltBeacon无法检测到信标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种模式下,磁带库继续正确扫描,但未检测到信标.

The library continue scanning correctly but not detect the beacon in this mode.

我在很多地方都读过书,但无论是否可行,他们的看法都大相径庭. (我想是这样)

I have read in many places, but they are very different opinions between whether this is possible or not. (I want to think it is)

IOS中,可以使用本机sdk进行相同的实现而不会带来任何不便,在android中似乎有些棘手.

In IOS, this same implementation using the native sdk could be implemented without any inconvenience, it seems a little tricky in android.

当前景和背景完美时,效果很好.

Works perfect when is foreground and background.

这是我的beaconManager配置:

public void setUpBeaconManager() throws RemoteException {
  if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
    beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(MainActivity.this);
    beaconManager.getBeaconParsers().clear();
    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"));
    beaconManager.setBackgroundBetweenScanPeriod(2000);
    beaconManager.setForegroundBetweenScanPeriod(2000);
    beaconManager.updateScanPeriods();
    beaconManager.bind(MainActivity.this);
  }
}

@Override
public void onBeaconServiceConnect() {
  Log.e(Tags.MAIN_ACTIVITY, "Beacon "+beaconManager.checkAvailability() );
  beaconManager.setRangeNotifier(new RangeNotifier() {

    @Override
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
      if (beacons.size() > 0) {
        Beacon firstBeacon = beacons.iterator().next();
        if (finishedRequest) {
          Log.e("", "beacon id1: " + firstBeacon.getId1());
          processDetectBeacons(firstBeacon.getId2().toInt(), firstBeacon.getId3().toInt());
        }
        Log.e(Tags.MAIN_ACTIVITY, "Beacon " + firstBeacon.toString() + " is about " + firstBeacon.getDistance() + " meters away.");
      }
    }
  });
}

@Override
protected void onDestroy() {
  super.onDestroy();
  if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {beaconManager.unbind(this);}
}

包容性,我尝试实现BootstrapNotifier

public class ApplicationManager extends Application implements BootstrapNotifier

public void onCreate() {
  super.onCreate();
  initRegionBootstrap();
}

private void initRegionBootstrap() {
  Log.d("RegionBootstrap", "Init RegionBootstrap functionality!!!");
  Region region = new Region("xxxx-4xx2-4E98-xxx-Bx5B7xxxx893E", Identifier.parse("xxxx-4xx2-4E98-xxx-Bx5B7xxxx893E"), null, null);
  regionBootstrap = new RegionBootstrap(this, region);
}

@Override
public void didEnterRegion(Region region) {
  Log.d("", "didEnterRegion: " + region);
}


@Override
public void didExitRegion(Region region) {
  Log.d("", "didExitRegion");
}


@Override
public void didDetermineStateForRegion(int i, Region region) {
  Log.d("", "didDetermineStateForRegion");
}

感谢您的阅读,希望您能为您提供帮助.

Thanks for read, i hope you can help.

推荐答案

基于显示的代码,每次系统检测到信标出现在范围内时,均应调用ApplicationManagerdidEnterRegion(Region region).

Based on the code shown, the ApplicationManager's didEnterRegion(Region region) should be called each time the system detects that the beacon appears in range.

一些提示:

  • 要确保您的应用程序不在该区域中,请先关闭信标并验证您在前台运行时是否退出事件,之前,然后再开始进行后台检测测试.从库版本2.8开始,即使在应用程序重新启动后,直到退出事件发生,您都不会收到第二个区域进入事件.

  • Make sure your app doesn't think it is already in the region by first turning off your beacons and verifying you get an exit event when in the foreground, before starting background detection testing. As of library version 2.8, you don't get a second region entry event even after app restart until an exit event happens.

认识到扫描在4.x上的后台发生得更慢. Android 4.x的默认行为是每5分钟进行一次扫描.这意味着进行背景检测可能需要花费很长时间.根据MainActivity所示的代码,如果执行该代码,则会将两次扫描之间的背景时间增加到两秒.但是,如果应用被杀死,则该代码将不会在应用重启时执行(因为这是一个活动),并且扫描每隔5分钟就会进行一次.确保等待足够长的时间.

Recognize that scans happen more slowly in the background on 4.x. The default behavior on Android 4.x is for scans to happen every 5 minutes. This means it could take that long for a background detection. Based on the code shown for the MainActivity, if this is executed it will increase the background period between scans to two seconds. However, if the app is killed, this code won't execute on app restart (because it is an Activity) and the scans will happen every 5 minutes again. Make sure you wait long enough.

在5.x上,在后台使用低功耗扫描.低功耗扫描的实现是特定于设备的.在Nexus 5和5x等较新的设备上,会持续进行扫描并将结果在3-6秒钟内发送到应用程序.在Samsung Galaxy S5设备上,将每15分钟进行一次定期扫描.因此,最多可能需要15分钟才能进行背景检测.确保等待足够长的时间.

On 5.x low power scans are used when in the background. The implementation of low power scans is device-specific. On newer devices like the Nexus 5 and 5x, constant scans will happen and send results to the app within 3-6 seconds. On Samsung Galaxy S5 devices, a periodic scan will take place every 15 minutes. So it may take up to 15 minutes to get a background detection. Make sure you wait long enough.

杀死一个应用程序后,该库将尝试在5分钟内重新启动扫描服务,但是可能要花很长时间.确保等待足够长的时间.

After you kill an app, the library will try to restart the scanning service within 5 minutes, but it may take that long. Make sure you wait long enough.

有多种方法可以杀死应用程序,并且某些杀死应用程序的方法将不允许扫描过程自行重启.如果您从设置"->应用程序"->我的应用程序"->强制停止"中删除该应用程序,则操作系统将不允许Android Beacon Library重新启动.有一些报道称,从任务切换器中杀死一个应用程序会在某些设备上导致相同的行为,但是具有这种构造的手机很少见.在杀死该应用五分钟后,在命令行上使用adb logcatadb shell ps来查看该应用是否正在运行.

There are different ways to kill an app, and some ways of killing it won't allow the scanning process to restart itself. If you kill the app from Settings -> Applications -> My App -> Force Stop, the OS will not allow the Android Beacon Library to restart itself. There have been a few reports that killing an app from the task switcher causes this same behavior on some devices, but phones with such builds are rare. Use adb logcat or adb shell ps on the command line to check to see if the app is running five minutes after you kill it.

最好的方法可能是尝试对库使用参考应用并在杀死应用程序后查看它是否在后台检测到信标.这将消除任何可能的编码问题,而仅使测试方法和特定于设备的问题成为罪魁祸首.

The best approach may be to try using the Reference App for the library and see if it detects beacons in the background after killing the app. This will eliminate any possible coding issue, leaving only testing methodology and device-specific issues as possible culprits.

这篇关于关闭应用程序后,AltBeacon无法检测到信标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆