AltBeacon not detect beacon when app is closed

僤鯓⒐⒋嵵緔 提交于 2020-01-06 20:18:08

问题


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)

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.

This is my beaconManager configuration:

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);}
}

Inclusive i try implement 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.


回答1:


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.

A few tips:

  • 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.

  • 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.

  • 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.

  • 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.

  • 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.



来源:https://stackoverflow.com/questions/38064280/altbeacon-not-detect-beacon-when-app-is-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!