Getting started with Fabric and Android Wear

By ‎@gpj‎
Tuesday, 9 December 2014

Using Fabric with Android Wear is a great way to bring stability and social data to your apps. It’s easy to get started – and quite fun.

Getting started with Fabric and Android Wear

One of the easiest ways to use Android Wear is for push notifications to your wear device. Let’s say we want to push Tweets. Here are the three simple steps:

Step 1: Retrieve a Tweet
Using Fabric and the Twitter Kit, it’s really easy to show a Tweet. First, integrate Fabric and set up the Twitter Kit. Once that’s done, use simple convenience methods to retrieve a Tweet. Here’s an example:

// MainActivity.java 
//
final long  tweetId = 524971209851543553L;
TweetUtils.loadTweet(tweetId, new LoadCallback() {
    @Override
    public void success(Tweet tweet) {
        sendMessage(WEAR_MESSAGE_PATH, tweet.text);
    }
    @Override
    public void failure(TwitterException e) {
    }
});

Step 2: Send Tweet to the Wearable Message API
Send the Tweet to the Wearable Message API for the Wear Device to receive. We look for connected “nodes” to our mobile app and push the Tweet to each connected node.

// MainActivity.java
//
private void sendMessage( final String path, final String tweetText ) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(client).await();
                for (Node node : nodes.getNodes()) {
                    MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
                            client, node.getId(), path, tweetText.getBytes()).await();
                }
            }
        }).start();
    }

Step 3: Create Listening Service on Wear
Now that you have Tweets being sent to the Message API, you need to create a ListeningService on the wear device app. This Service listens for the Message API and a specific URI call e.g. /new_tweet. Once a call is made to “/new_tweet” you can then create a new notification to display.

// ListenerService.java
//
public class ListenerService extends WearableListenerService {...
    ...
    @Override
    public void onMessageReceived( final MessageEvent messageEvent ) {
        int notificationId = 001;
        // Build intent for notification content
        Intent viewIntent = new Intent(this, NotificationActivity.class);
        String tweet = new String(messageEvent.getData());
        viewIntent.putExtra("tweet", tweet);
        PendingIntent viewPendingIntent =
                PendingIntent.getActivity(this, 0, viewIntent, 0);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("Tweet")
                        .setContentText(tweet)
                        .setContentIntent(viewPendingIntent);

        // Get an instance of the NotificationManager service
        NotificationManagerCompat notificationManager =
                NotificationManagerCompat.from(this);

        // Build the notification and issues it with notification manager.
        notificationManager.notify(notificationId, notificationBuilder.build());
    }
}

Here’s the fully functional demo:

These are the basics to get you started, and we think Fabric is a great source for building amazing wearable apps. The full source code for this project is available on Github. We suggest cloning the repo and following the code with this tutorial. If you’re looking for a primer on using Android Wear, check out this great guide to building wearable apps.