With Google's new Eddystone standard they will be providing support for android in Google Play services Nearby Api. Can we register for eddystone beacons and have our app receive an intent even if the app is not running?
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.
- First create a Google Developer Console project
- Enable the "Nearby Messages API" for the project you just created
- Create an API Key for this project, as described here and add it to the Manifest
- 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
- 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"
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
来源:https://stackoverflow.com/questions/31414108/can-i-listen-for-eddystone-beacons-when-my-app-is-not-running