Reputation: 19436
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?
Upvotes: 2
Views: 3162
Reputation: 72321
Ok, so this was very painful but I finally managed to detect the beacons using the Nearby Messages API.
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());
Upvotes: 1
Reputation: 1694
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
Upvotes: 0
Reputation: 64941
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
}
}
Upvotes: 5