Background monitoring of Eddystone beacon using altbeacon library on android platform

后端 未结 2 1793
旧巷少年郎
旧巷少年郎 2021-01-21 02:30

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 ca

相关标签:
2条回答
  • 2021-01-21 03:09

    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
        }
    ...
    }
    
    0 讨论(0)
  • 2021-01-21 03:19

    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?

    0 讨论(0)
提交回复
热议问题