Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Context: Nearby & Awareness

Context: Nearby & Awareness

How to make your application understand the world around it and become more contextual using the Nearby and Awareness APIs.

Video: https://vimeo.com/216079560

Brian Duff

April 20, 2017
Tweet

More Decks by Brian Duff

Other Decks in Programming

Transcript

  1. Proprietary + Confidential Proprietary + Confidential Proprietary + Confidential Agenda

    History & Background Nearby.Messages Nearby.Notifications Awareness Coming soon...
  2. Subscribing for messages MessageListener listener = new MessageListener() { void

    onFound(Message m) { byte[] bytes = m.getContent(); } }; Nearby.Messages.subscribe(mClient, listener);
  3. Subscribing for beacons in the background private void backgroundSubscribe() {

    SubscribeOptions options = new SubscribeOptions.Builder() .setStrategy(Strategy.BLE_ONLY) .build(); Nearby.Messages.subscribe(mClient, getPendingIntent(), options); } private PendingIntent getPendingIntent() { return PendingIntent.getBroadcast( this, 0, new Intent(this, BeaconMessageReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT); }
  4. Subscribing for beacons in the background private void backgroundSubscribe() {

    SubscribeOptions options = new SubscribeOptions.Builder() .setStrategy(Strategy.BLE_ONLY) .build(); Nearby.Messages.subscribe(mClient, getPendingIntent(), options); } private PendingIntent getPendingIntent() { return PendingIntent.getBroadcast( this, 0, new Intent(this, BeaconMessageReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT); }
  5. Receiving beacon messages @Override public void onReceive(Context context, Intent intent)

    { Nearby.Messages.handleIntent(intent, new MessageListener() { @Override public void onFound(Message message) { // do something with message.getBytes() } @Override public void onLost(Message message) { // Oh noes! My beacon is out of range. } }); }
  6. Advanced beacon features MessageFilter lets you scan for: • PUBLIC

    namespaced attachments in other namespaces • Raw BLE beacon IDs Special beacon callbacks: • onDistanceChanged • onBleSignalChanged
  7. Example: Scan for iBeacons MessageFilter messageFilter = new MessageFilter.Builder() .includeIBeaconIds(PROXIMITY_UUID,

    major, minor) .build(); SubscribeOptions options = new SubscribeOptions.Builder() .setStrategy(Strategy.BLE_ONLY) .setFilter(messageFilter) .build(); @Override public void onFound(Message message) { IBeaconId ibeacon = IBeaconId.from(message); }
  8. Notifications about nearby things Attach URLs, or app intents We'll

    send users to Play Store if app not installed Notifications can be targeted Notifications are a reward for "good behavior", not a guarantee
  9. Example: App Attachment { "title": "Get the app!", "url": "intent://host/path#Intent;scheme=scheme;package=com.yourapp.ui;end;",

    "targeting":[{ "anyOfAppInstallStates": ["NOT_INSTALLED"], "startTimeOfDay": "9:00", "endTimeOfDay": "17:00" }] }
  10. Example context fence Play my "waiting for coffee" playlist when

    I'm standing still in my local coffee shop on a Saturday afternoon with my headphones on.
  11. Example: Fencing the conditions AwarenessFence still = DetectedActivityFence.during(STILL); AwarenessFence inCoffeeShop

    = LocationFence.in( 37.421725, -122.096149, // lat / long 50, // radius in m TimeUnit.MINUTES.toMillis(5)); // dwell time AwarenessFence headphonesOn = HeadphoneFence.during(PLUGGED_IN);
  12. Example: Putting it all together AwarenessFence readyForTunes = AwarenessFence.and( still,

    inCoffeeShop, headphonesOn); Awareness.FenceApi.updateFences(apiClient, new FenceUpdateRequest.Builder() .addFence(fenceKey, readyForTunes, pendingIntent) .build());
  13. Help! Let us know what you're building Let us know

    what works & doesn't work Let us know what you'd like to see in the future