4. How it works?
• Require android (>= 4.3) phone/tablet
(referred as handheld)
• Require “Android Wear” app on the handheld
• Handheld and wearable communicate each
other via Bluetooth (wearable itself does not
connect to internet)
Notifications
5. What you can do?
• Receive notifications and react them on the
wearable
• Control apps on the phone from the wearable
• Run wearable specific app (e.g. compass,
heart rate monitor)
• Voice search and voice action
etc…
10. Application packaging and publishing
• When publishing to user, package a wearable
app inside a handheld app
• Wearable app will be installed on user’s
wearable automatically when user install
handheld app from Google Play Store
• A handheld app has to have the same package
name as a wearable app
18. Notification gotcha
• Notification from the handheld can only open
an activity on the handheld
• To open/embed a custom activity on the
wearable, you need to send notification from
the wearable.
• Open a custom activity • Embed a custom activity
Custom
Activity
19. Send notification to open an activity
on the wearable
Intent viewIntent = new Intent(context, WatchActivity.class);
PendingIntent pendingViewIntent = PendingIntent.getActivity(context, 0, viewIntent,
0);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Wearableから送信")
.setContentText("Wearableから送信したNotificationです。")
.addAction(R.drawable.ic_launcher, "Open", pendingViewIntent)
.setLocalOnly(true)
.extend(new
NotificationCompat.WearableExtender().setContentAction(0).setHintHideIcon(true))
.build();
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(context);
notificationManager.notify(3000, notification);
WatchActivity.java (Wearable)
Specify the default
action to be
triggered when user
tap the notification
20. Send notification embedding an
activity on the wearable
Intent viewIntent = new Intent(context, NotificationEmbeddedActivity.class);
PendingIntent pendingViewIntent = PendingIntent.getActivity(context, 0, viewIntent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Wearableから送信")
.setContentText("Wearableから送信したNotificationです。")
.setLocalOnly(true)
.extend(new
NotificationCompat.WearableExtender().setDisplayIntent(pendingViewIntent))
.build();
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(context);
notificationManager.notify(4000, notification);
WatchActivity.java (Wearable)
Custom
Activity
Specify the Pending
Intent of activity to
be embedded in the
notification
21. Sending and syncing data
• Message API
– Fire and forgot, one way request
– You can send just a string
– Use case example
• Control media player from the wearable
• Data API
– Sync data between the handheld and wearable
– Payload is a byte array (100KB per item), but you can
use map interface to access data
– Use case example
• Send Photo preview from the handheld to wearable
22. Connect Google Api Client
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "Google Api Client connected");
}
@Override
public void onConnectionSuspended(int i) {
}
}).build();
mGoogleApiClient.connect();
}
DataActivity.java (Handheld)
24. Message API
public class DataLayerListenerService extends WearableListenerService {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if (START_ACTIVITY_PATH.equals(messageEvent.getPath())) {
Intent intent = new Intent(this, WatchActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
return;
}
}
}
<service android:name=".DataLayerListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
AndroidManifest.xml (Wearable)
DataLayerListenerService.java (Wearable)
25. Data API
send data from handheld
int count = 0;
private void sendCount() {
PutDataMapRequest dataMap = PutDataMapRequest.create(COUNT_PATH);
dataMap.getDataMap().putInt(COUNT_KEY, ++count);
PutDataRequest request = dataMap.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi
.putDataItem(mGoogleApiClient, request);
pendingResult.setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
@Override
public void onResult(DataApi.DataItemResult dataItemResult) {
Log.d(TAG, "count updated:" + count);
}
});
}
DataActivity.java (Handheld)
26. Data API
detect data change on wearable
public class DataLayerListenerService extends WearableListenerService {
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent event : dataEvents) {
DataItem dataItem = event.getDataItem();
if (COUNT_PATH.equals(dataItem.getUri().getPath())) {
DataMap dataMap = DataMapItem.fromDataItem(dataItem).getDataMap();
int count = dataMap.getInt(COUNT_KEY);
….
}
}
}
}
DataLayerListenerService.java (Wearable)
27. Data API
access to the existing data
private void restoreCurrentCount() {
String localNodeId = getLocalNodeId();
Uri uri = new
Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).authority(localNodeId).path(COUNT_PATH).buil
d();
Wearable.DataApi.getDataItem(mGoogleApiClient, uri).setResultCallback(new
ResultCallback<DataApi.DataItemResult>() {
@Override
public void onResult(DataApi.DataItemResult dataItemResult) {
DataItem dataItem = dataItemResult.getDataItem();
if (dataItem != null) {
DataMap dataMap = DataMapItem.fromDataItem(dataItemResult.getDataItem()).getDataMap();
count = dataMap.getInt(COUNT_KEY);
}
}
});
}
private String getLocalNodeId() {
NodeApi.GetLocalNodeResult nodeResult = Wearable.NodeApi.getLocalNode(mGoogleApiClient).await();
return nodeResult.getNode().getId();
}
DataActivity.java (Handheld)
The uri of data is
wear://<NODE_ID>/<PAT
H>
Node ID is required
to send message
28. Combination (1)
• Update embedded activity in notification from
the handheld
Data API
Notification with
DisplayIntent
Data API
Notification with
DisplayIntent
Data API
Notification with
DisplayIntent
Example: Google Map navigation on wearable
29. Combination (2)
• Send notification from handheld to wearable
to open a custom activity on wearable
Data API Notification Tap to open
Custom Activity
Message API
Example: Remote shutter for Google Camera app
31. Draw Watch for Android Wear
• https://play.google.com/store/apps/details?id
=com.polysfactory.drawwatch
33. Random stuff I leaned
• Prevent default behavior of wearable (right
swipe to dismiss app)
– Place close menu on the second screen, but it is
very confusing
• Starting app from cue card is a little bit
annoying, it’s better to use notification
• Using Message API to share bitmap on the
handheld works great
34. All sample code is available at github
https://github.com/thorikawa/AndroidWearSampleJp