Can I listen for Eddystone beacons when my app is not running?

狂风中的少年 提交于 2019-11-30 07:44:34

Yes, it is possible to do exactly this using the Android Beacon Library, which has full support for Eddystone.

The mechanism for background launching of your app works the same way on Eddystone as it does for other kinds of beacons supported by the library. You use a RegionBootstrap object in a custom Application class. You can read details about how this works here.

The only difference with Eddystone is that you have to set up a BeaconParser that decodes the Eddystone-UID frame, and then set up a Region that will match your Eddystone namespace id:

public class MyApplicationName extends Application implements BootstrapNotifier {
    private static final String TAG = ".MyApplicationName";
    private RegionBootstrap regionBootstrap;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "App started up");
        BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
        // Detect the main identifier (UID) frame:
        beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));

        // wake up the app when a beacon matching myEddystoneNamespaceId is seen 
        myEddystoneNamespaceId = Identifier.parse("0x2f234454f4911ba9ffa6");
        Region region = new Region("com.example.myapp.boostrapRegion", myEddystoneNamespaceId, null, null);
        regionBootstrap = new RegionBootstrap(this, region);
    }

    @Override
    public void didDetermineStateForRegion(int arg0, Region arg1) {
        // Don't care
    }

    @Override
    public void didEnterRegion(Region arg0) {
        Log.d(TAG, "Got a didEnterRegion call");
        // This call to disable will make it so the activity below only gets launched the first time a beacon is seen (until the next time the app is launched)
        // if you want the Activity to launch every single time beacons come into view, remove this call.  
        regionBootstrap.disable();
        Intent intent = new Intent(this, MainActivity.class);
        // IMPORTANT: in the AndroidManifest.xml definition of this activity, you must set android:launchMode="singleInstance" or you will get two instances
        // created when a user launches the activity manually and it gets launched from here.
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(intent);
    }

    @Override
    public void didExitRegion(Region arg0) {
       // Don't care
    }        
}

Ok, so this was very painful but I finally managed to detect the beacons using the Nearby Messages API.

  1. First create a Google Developer Console project
  2. Enable the "Nearby Messages API" for the project you just created
  3. Create an API Key for this project, as described here and add it to the Manifest
  4. Configure and register your beacons as described here; what I had to do was install the Android app, open it, select the project I just created, discover the beacon and register it with my app
  5. You will have to attach a message to your beacon, to get it detected. I did this using the Beacon Tools app, click the registered beacon, and tap on "Attachments"
  6. Now you can follow the code sample here and it should detect your beacon

    Nearby.Messages.subscribe(googleApiClient, new MessageListener() {
        @Override
        public void onFound(Message message) {
            Log.i(TAG, "Found : " + message);
        }
    
        @Override
        public void onLost(Message message) {
            Log.i(TAG, "Lost : " + message);
        }
    }, new SubscribeOptions.Builder()
            .setStrategy(Strategy.BLE_ONLY)
            .build());
    

This has been possible since the release of Google play services v8.4 SDK (December 2015).

See the following link for more: http://android-developers.blogspot.com/2015/12/google-play-services-84-sdk-is-available_18.html

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