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.

Andy Dyer

August 12, 2015
Tweet

More Decks by Andy Dyer

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. Wearable app 1. UI library for displaying GIFs 2. Request

    GIF from mobile app 3. Listen for data change from phone 4. Update view
  5. 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(); } }
  6. 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 } } } }
  7. 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
  8. 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()); } }
  9. 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.
  10. 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.