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

  3. Never Gonna GIF
    You Up

    View 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 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 Slide

  6. Wearable app

    View 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 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 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 Slide

  10. Mobile app

    View Slide

  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

    View Slide

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




    View Slide

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


    gif_me


    View Slide

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

    View Slide

  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.

    View Slide

  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.

    View Slide