introduction to android wear
TRANSCRIPT
What is Android Wear ?
Android Wear extends the Android platform to a new generation of
wearable devices
How does it work ?
● Phone and wearable communicate with each other over BT
● Require Android 4.3+ on phone/tablet
● Wearable itself doesn’t connect to internet
● Limited function when connection is lost
● Current available sensors are Compass, Accelerometer, Gyroscope,
Heart Rate Sensor(*)
Android Wear Project Wizard
Use Android Studio!
http://goo.gl/ZSJH5O
Android Studio is optimized for Wear
JAVA
Notification : basic example (1)
// Notification
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(“Notification Title”)
.setContentText(“Notification Text”)
.setContentIntent(mPendingIntent);
JAVA
Notification : basic example (2)
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(mContext);
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());
JAVA
Notification : long text
//BigText
NotificationCompat.BigTextStyle bigStyle = new
NotificationCompat.BigTextStyle();
bigStyle.setBigContentTitle(“Big Title”);
bigStyle.bigText(“Lorem ipsum……..”);
// Notification
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_launcher).setStyle(bigStyle)
.setContentIntent(mPendingIntent);
JAVA
Notification : big picture
//BigPicture NotificationCompat.BigPictureStyle bigStyle = new NotificationCompat.BigPictureStyle(); bigStyle.bigPicture
(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.large_image));
bigStyle.setBigContentTitle(“Big Title”);
bigStyle.setSummaryText(“Notification Text”);
// Notification
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_launcher).setStyle(bigStyle)
.setContentIntent(mPendingIntent);
JAVA NotificationCompat.BigPictureStyle bigStyle = new NotificationCompat.BigPictureStyle();
bigStyle.bigPicture
(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.large_image));
bigStyle.setBigContentTitle(“Second Page”);
// Create second page notification
Notification secondPageNotification =
new NotificationCompat.Builder(mContext)
.setStyle(bigStyle)
.build();
Notification: pages
JAVA // Notification
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(“Notification Title”)
.setContentText(“Notification Text”)
.extend(
new NotificationCompat.WearableExtender().addPage(secondPageNotification))
.setContentIntent(mPendingIntent);
Notification: pages
JAVA // Notification
NotificationCompat.Builder notificationBuilder1 =
new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(“Notification Title”)
.setContentText("Alex New meeting")
.setGroup(GROUP_KEY_EMAILS)
.setContentIntent(mPendingIntent)
.setSortKey("0");
notificationManager.notify(notificationId, notificationBuilder1.build());
Notification: stacks (1)
JAVA NotificationCompat.Builder notificationBuilder2 =
new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(“Notification Title”)
.setContentText("Peter Next week")
.setGroup(GROUP_KEY_EMAILS)
.setContentIntent(mPendingIntent)
.setSortKey("1");
notificationManager.notify(notification2Id, notificationBuilder2.build());
Notification: stacks (2)
JAVA // Create an InboxStyle notification
Notification summaryNotificationWithBackground =
new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_launcher).setLargeIcon(largeIcon)
.setStyle(new NotificationCompat.InboxStyle()
.addLine("Alex New meeting")
.addLine("Peter Next week")
.setBigContentTitle("2 new messages")
.setSummaryText("[email protected]"))
.setGroup(GROUP_KEY_EMAILS).setGroupSummary(true).build();
notificationManager.notify(notification3Id, summaryNotificationWithBackground);
Notification: stacks (3)
JAVA // Notification
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(“Notification Title”)
.setContentText(“Notification Text”)
.addAction(R.drawable.ic_full_action, “Simple Action”,mPendingActionIntent)
.setContentIntent(mPendingIntent);
Notification: simple action
JAVANotificationCompat.Action action = new NotificationCompat.Action.Builder(
R.drawable.ic_full_reply,“Reply”,mPendingIntent)
.addRemoteInput(mNotificationUtil.getReplyAction())
.build();
public RemoteInput getReplyAction(){
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel(“Reply”).build();
return remoteInput;
}
Notification: reply action (1)
JAVA // Notification
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(“Notification Title”)
.setContentText(“Notification Text”)
.extend(new NotificationCompat.WearableExtender().addAction(action));
Notification: reply action (2)
JAVA public RemoteInput getReplyActionWithPredefinedTextResponses(){
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel(“Reply”)
.setChoices(mContext.getResources().getStringArray(R.array.reply_choices))
.build();
return remoteInput;
}
<string-array name="reply_choices">
<item>Yes</item>
<item>No</item>
</string-array>
Notification: reply action with choices
JAVA
GoogleApiClient
Connection
//Connect the GoogleApiClient
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
JAVA
GoogleApiClient
Connection
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Overrire
protected void onStop(){
super.onStop();
mGoogleApiClient.disconnect();
}
JAVA
Connected Nodes
Connection
private void resolveNode() {
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient)
.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult nodes) {
for (Node node : nodes.getNodes()) {
//…..
}
}
});
}
JAVA
Node - Listeners
Connection
public interface NodeApi.NodeListener {
void onPeerConnected(com.google.android.gms.wearable.Node node);
void onPeerDisconnected(com.google.android.gms.wearable.Node node);
}
Example:
public class MyActivity extends Activity implements NodeApi.NodeListener {
public void onConnected(Bundle bundle) {
Wearable.NodeApi.addListener(mGoogleApiClient, this);
}
}
JAVA Wearable.MessageApi.sendMessage(
mGoogleApiClient, nodeId, PATH, payload).setResultCallback(
new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
//……….
}
}
);
MessageMessageAPI
JAVA
Message - Listener
Message
interface MessageApi.MessageListener {
void onMessageReceived(com.google.android.gms.wearable.MessageEvent messageEvent);
}
Example:
public class MyActivity extends Activity implements MessageApi.MessageListener {
public void onConnected(Bundle bundle) {
Wearable.MessageApi.addListener(mGoogleApiClient, this);
}
}
JAVAPutDataMapRequest dataMap = PutDataMapRequest.create("/count");
dataMap.getDataMap().putInt(COUNT_KEY, count++);
//dataMap.getDataMap().putString(“/myItemId”, “Hello World”);
PutDataRequest request = dataMap.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult =
Wearable.DataApi.putDataItem(mGoogleApiClient, request);
DataDataAPI
JAVA
Data - Listener
Data
interface DataApi.DataListener {
void onDataChanged(com.google.android.gms.wearable.DataEventBuffer dataEventBuffer);
}
Example:
public class MyActivity extends Activity implements DataApi.DataListener {
public void onConnected(Bundle bundle) {
Wearable.DataApi.addListener(mGoogleApiClient, this);
}
}
JAVA
Activity
Receiving Message
public classs MyActivity extends Activity implements MessageApi.MessageListener {
//Add the listener in onConnected() method
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if (messageEvent.getPath().equals(PATH)) {
byte[] bytes = messageEvent.getData();
}
}
}
JAVA
Service
Receiving Message
<service android:name=".MyListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
public classs MyListenerService extends WearableListenerService {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
//…………..
}
JAVA
Activity
Receiving Data
public classs MyActivity extends Activity implements DataApi.DataListener {
//Add the listener in onConnected() method
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_DELETED) {
Log.d(TAG, "DataItem deleted: " + event.getDataItem().getUri());
} else if (event.getType() == DataEvent.TYPE_CHANGED) {
Log.d(TAG, "DataItem changed: " + event.getDataItem().getUri());
}
}
}
JAVA
Service
Receiving Data
<service android:name=".MyListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
public classs MyListenerService extends WearableListenerService {
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
//…………..
}
JAVA
Example: launch Activity on mobile from wear
SendMessageActivity extends Activity {
…
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.build();
}
wear/SendMessageActivity.java
JAVA
Example: launch Activity on mobile from wear
…
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
mGoogleApiClient.disconnect();
}
wear/SendMessageActivity.java
JAVA
Example: launch Activity on mobile from wear
private void resolveNode() {
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient)
.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult nodes) {
for (Node node : nodes.getNodes()) {
sendMessage(node);
}
}
});
}
wear/SendMessageActivity.java
JAVA
Example: launch Activity on mobile from wear
static final String HELLO_WORLD_WEAR_PATH = "/hello-world-wear";
private void sendMessage(Node node){
Wearable.MessageApi.sendMessage(
mGoogleApiClient, node.getId(), HELLO_WORLD_WEAR_PATH,null)
.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
if (!sendMessageResult.getStatus().isSuccess()) {
Log.e("TAG", "Failed to send message with status code: "
+ sendMessageResult.getStatus().getStatusCode());
}
}
}
);
}
wear/SendMessageActivity.java
JAVA
Example: launch Activity on mobile from wear
public class ListenerServiceFromWear extends WearableListenerService {
private static final String WEAR_PATH = "/hello-world-wear";
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if (messageEvent.getPath().equals(WEAR_PATH)) {
Intent startIntent = new Intent(this, EmptyActivity.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startIntent);
}
}
}
mobile/ListenerService.java
JAVA
Example: launch Activity on mobile from wear
You can find the full code here:
https://gist.github.com/gabrielemariotti/117b05aad4db251f7534
References and Credits
- Getting Started with Android Wearhttp://developer.android.com/wear
- Build apps for Wearableshttp://developer.android.com/training/building-wearables.html
- Thanks to Mario Viviani for some slides and ideas from Kick-Start Your Experience with Android Wear