A wrapper for socket communication between Android Wear and Android Mobile. There is a lot of boiler plate to send data between Android Wear and Android Mobile devices. This library (inspired by Socket.IO), aims to streamline both message sending and synching data updates.
Add this line to your dependencies
in your build.gradle
file:
compile 'com.github.jrejaud:wear-socket:0.1.2'
Note: WearSocket uses jCenter repository and is not on Maven Central atm.
First, get an instance of the WearSocket and set it up by passing Context and the wear app capabilities
WearSocket wearSocket = WearSocket.getInstance();
String androidWearCapability = ...;
wearSocket.setupAndConnect(context, androidWearCapability);
Sending and Receiving Messages between Wear and Mobile
Pick a path (think of it like a subject) for your message.
wearSocket.sendMessage("myPath","myMessage");
Start your message listener and listen for the path that you are associating with your messages.
wearSocket.startMessageListener(context,"myPath");
Implement WearSocket.DataListener and override messageReceived to start listening for messages
public class MyActivity implements WearSocket.DataListener
@Override
public void messageReceived(String path, String message) {
//Do things here!
}
Synching Data Updates between Wear and Mobile
Note: Data Path should begin with /, thus I recommend using one different from message sending. dataItem is an object (or collection of objects).
wearSocket.updateDataItem("myDataPath","myKey",dataItem);
Start your data change listener and listen for the path that you are associating with your data changes.
wearSocket.startDataListener(context,"myDataPath");
Additionally, you need to associate a Type with a specific Key. For example, if the key "myKey" is associated with a List<String>
object, set that Type accordingly:
wearSocket.setKeyDataType("myKey",new TypeToken<List<String>>() {}.getType());
Change List<String>
to whatever class (or collection of classes) that dataItem is.
Implement WearSocket.MessageListener and override messageReceived to start listening for messages
public class MyActivity implements WearSocket.MessageListener
@Override
public void dataChanged(String key, Object data) {
//Cast "data" as whatever class you sent it as
ArrayList<String> dataItem = (ArrayList<String>) data;
//Do things here!
}
MIT License
WearSocket uses: Gson, Android Wearable APIs, Google Play Services