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

Building an Android Wear app

Building an Android Wear app

A brief talk about building an Android Wear app presented at one of our weekly tech talks.

E0ac1256093c733a5d5a26085b90966f?s=128

Andy Dyer

August 12, 2015
Tweet

Transcript

  1. Building an Android Wear app

  2. Anatomy of an Android Wear app 4 Wearable app 4

    Mobile app, with wearable app packaged inside 4 Communicate via notifications and/or Google Play Services APIs
  3. Never Gonna GIF You Up

  4. Loading a random GIF on the watch 1. UI library

    for displaying GIFs 2. Request random GIF from Giphy API 3. Make API request, update view
  5. Loading a random GIF on the watch 1. UI library

    for displaying GIFs 2. Request random GIF from Giphy API 3. Make API request, update view Most wearables don't have internet...yet
  6. Wearable app

  7. Wearable app 1. UI library for displaying GIFs 2. Request

    GIF from mobile app 3. Listen for data change from phone 4. Update view
  8. Request GIF from mobile app Set<Node> nodes = Wearable.CapabilityApi .getCapability(googleApiClient,

    "gif_me", CapabilityApi.FILTER_REACHABLE) .await() .getCapability() .getNodes(); if (!nodes.isEmpty()) { for (Node node : nodes) { Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), "gif/random", null).await(); } }
  9. Listen for data changes @Override public void onDataChanged(DataEventBuffer dataEvents) {

    for (DataEvent event : dataEvents) { if (event.getType() == DataEvent.TYPE_CHANGED) { DataItem item = event.getDataItem(); if (item.getUri().getPath().compareTo("/image") == 0) { Asset asset = DataMapItem.fromDataItem(item).getDataMap().get("random_gif"); // Do something with asset } } } }
  10. Mobile app

  11. Mobile app 1. WearableListenerService to listen for GIF requests 2.

    Request random GIF from Giphy API 3. Push file bytes to wearable via Data API
  12. Registering a WearableListenerService - AndroidManifest.xml <service android:name=".GifRequestWearableListenerService" tools:ignore="ExportedService" > <intent-filter>

    <action android:name="com.google.android.gms.wearable.BIND_LISTENER" /> </intent-filter> </service>
  13. Registering a WearableListenerService: res/values/wear.xml <resources> <string-array name="android_wear_capabilities"> <item>gif_me</item> </string-array> </resources>

  14. WearableListenerService public class GifRequestWearableListenerService extends WearableListenerService { @Override public void

    onMessageReceived(MessageEvent messageEvent) { if (messageEvent.getPath().equals("gif/random")) { bytes[] imageBytes = getRandomGif(); Asset asset = Asset.createFromBytes(imageBytes) pushAsset(asset); } } private void pushAsset(Asset asset) { PutDataMapRequest request = PutDataMapRequest.create("/image"); request.getDataMap().putAsset("random_gif", asset); Wearable.DataApi.putDataItem(googleApiClient, request.asPutDataRequest()); } }
  15. Achtung! 4 Enable developer options and debugging on the wearable

    4 Packaged wearable apps aren't automatically deployed for debug builds 4 Deploy the right app to the right device 4 It's difficult to debug end-to-end. Focus on one end at a time.
  16. Weird stuff 4 Retrolambda doesn't currently work with Android Wear.

    It seems to break the packaging of the wearable app into the mobile app.