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. Building an Android Wear
    app

    View full-size slide

  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

    View full-size slide

  3. Never Gonna GIF
    You Up

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  6. Wearable app

    View full-size slide

  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

    View full-size slide

  8. Request GIF from mobile app
    Set 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();
    }
    }

    View full-size slide

  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
    }
    }
    }
    }

    View full-size slide

  10. 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

    View full-size slide

  11. Registering a WearableListenerService
    - AndroidManifest.xml
    android:name=".GifRequestWearableListenerService"
    tools:ignore="ExportedService" >




    View full-size slide

  12. Registering a WearableListenerService:
    res/values/wear.xml


    gif_me


    View full-size slide

  13. 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());
    }
    }

    View full-size slide

  14. 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.

    View full-size slide

  15. 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.

    View full-size slide