Background monitoring of Eddystone beacon using altbeacon library on android platform

两盒软妹~` 提交于 2019-12-01 22:31:25

问题


Is background monitoring of Eddystone beacon using altbeacon library on android platform possible? How can I achieve it?

Following is the code by which I can detect beacons with a specified UUID when the app is launched, but I want to achieve the same when the app is not running.

public class MainActivity extends ActionBarActivity implements BeaconConsumer,MonitorNotifier
{

private BeaconManager beaconManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


@Override
protected void onResume() {
    super.onResume();
    beaconManager = BeaconManager.getInstanceForApplication(this.getApplicationContext());
    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.bind(this);
}

@Override
public void onBeaconServiceConnect() {

    Identifier myBeaconNamespaceId = Identifier.parse("0xe2bfcc3cc2370789caef");
    Region region = new Region("my-beacon-region", myBeaconNamespaceId, null, null);
    beaconManager.setMonitorNotifier(this);
    try {
        beaconManager.startMonitoringBeaconsInRegion(region);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

@Override
public void didEnterRegion(Region region) {

        Log.d("radbeacon", "Beacon detected with namespace id " + region.getId1() +" and instance id: " + region.getId2());
}

@Override
public void didExitRegion(Region region) {

    Log.d("radbeacon", "Beacon out of region with namespace id " + region.getId1() +" and instance id: " + region.getId2());
}

@Override
public void didDetermineStateForRegion(int i, Region region) {
      //Ignore
}


}

回答1:


Yes, it is possible to detect Eddystone beacons in the background with the Android Beacon Library. You do so in the same manner as with AltBeacon or iBeacon. Details are described in the Starting App in the Background section of the samples.

EDIT: As fof Library version 2.7, support for hardware accelerated discovery of Eddystone frames has been added, meaning that on Android 5+ devices you can get background detections within about 5 seconds.

The basic idea is you need to create a central android Application class for your app, and create a RegionBootstrap object in the onCreate method of that class. It is important to remember that you must register this Application class in your manifest. The sample code linked above shows you how to do this.

So you'd end up with something like below:

public class MyApplication extends Application implements BootstrapNotifier {
    private static final String TAG = "MyApplication";
    private RegionBootstrap regionBootstrap;
    private BackgroundPowerSaver backgroundPowerSaver;
    private BeaconManager mBeaconManager;

    public void onCreate() {
        super.onCreate();
        mBeaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
        mBeaconManager.getBeaconParsers().clear();
        mBeaconManager.getBeaconParsers().add(new BeaconParser().
            setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
        Identifier myBeaconNamespaceId = Identifier.parse("0xe2bfcc3cc2370789caef");
        Region region = new Region("my-beacon-region", myBeaconNamespaceId, null, null);

        regionBootstrap = new RegionBootstrap(this, region);

        backgroundPowerSaver = new BackgroundPowerSaver(this);
    }

    @Override
    public void didEnterRegion(Region region) {

            Log.d("radbeacon", "Beacon detected with namespace id " + region.getId1() +" and instance id: " + region.getId2());
    }

    @Override
    public void didExitRegion(Region region) {

        Log.d("radbeacon", "Beacon out of region with namespace id " + region.getId1() +" and instance id: " + region.getId2());
    }

    @Override
    public void didDetermineStateForRegion(int i, Region region) {
          //Ignore
    }
...
}



回答2:


Another very important thing to consider is adding the Application class name to the AndroidManifest.xml, just add android:name=".yourApplication" to the <application> tag.

Is there an example available doing both monitoring and ranging in an Application class?



来源:https://stackoverflow.com/questions/33607410/background-monitoring-of-eddystone-beacon-using-altbeacon-library-on-android-pla

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