The document provides an overview of how to build Android Wear applications. It discusses setting up an Android Wear project in Android Studio, establishing a connection between the mobile and wear modules using GoogleApiClient, and sending and receiving data and messages between the two modules via the Data Layer API and MessageAPI. It also presents an example application structure with a foreground activity on the mobile module and a WearableListenerService on the wear module to handle data and event handling.
31. GDE
React to Data Sync
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_DELETED) {
...
} else if (event.getType() == DataEvent.TYPE_CHANGED) {
...
}
}
}
32. GDE
Retrieve the DataMap
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
...
for (DataEvent event : events) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
DataMapItem dataMapItem =
DataMapItem.fromDataItem(event.getDataItem());
DataMap dataMap = dataMapItem.getDataMap();
String s = dataMap.getString("/itemID");
}
48. ...
private static final String START_ACTIVITY_PATH = "/start-activity";
public void sendMessageButtonClicked(View v){
Collection<String> nodes = getNodes();
for (String node : nodes) {
SendMessageResult result = Wearable.MessageApi.sendMessage(
mGoogleApiClient, node, START_ACTIVITY_PATH , null).await();
if (!result.getStatus().isSuccess()) {...}
}
}
}
MobileSendMessageActivity.java
49. public class StartWearActivityService extends WearableListenerService {
...
@Override
public void onCreate() {
super.onCreate();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.build();
mGoogleApiClient.connect();
}
...
StartWearActivityService.java
50. private static final String START_ACTIVITY_PATH = "/start-activity";
...
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if (messageEvent.getPath().equals(START_ACTIVITY_PATH)) {
Intent startIntent = new Intent(this, WearActivity.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startIntent);
}
}
}
StartWearActivityService.java
51. GDE
Add Service to Android Manifest
<service android:name=".StartWearActivityService">
<intent-filter>
<action
android:name="com.google.android.gms.wearable.BIND_LISTENER"/>
</intent-filter>
</service>
54. Teleport
Data Sync and Messaging Library
for Android Wear
Github
github.com/Mariuxtheone/Teleport
Gradle - Maven
compile 'Teleport:teleportlib:0.1.3'
55. GDE
TeleportClient
TeleportClient mTeleportClient = new TeleportClient(this);
mTeleportClient.connect();
Set Up:
Send Message
mTeleportClient.sendMessage("startActivity", null);
Sync Data
mTeleportClient.syncInt("myInt", 12345)