Skip to content

A wrapper for sending/receiving messages and data changes between Android Wear and Android Mobile.

License

Notifications You must be signed in to change notification settings

jrejaud/WearSocket

Repository files navigation

WearSocket

Android Arsenal

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.

Installation

Use JitPack

allprojects {
	repositories {
		maven { url "https://jitpack.io" }
	}
}
dependencies {
	    compile 'com.github.jrejaud:WearSocket:1.0.+'
}

Setup

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, new onErrorListener() {
       	@Override
        public void onError(Throwable throwable) {
        	//Throws an error here if there is a problem connecting to the other device.
        }
});

Advertising Capabilities (copied from official Google documentation)

Create an XML configuration file in the res/values/ directory of your project and name it wear.xml. Add a resource named android_wear_capabilities to wear.xml. Define capabilities that the device provides. Note: Capabilities are custom strings that you define and must be unique within your app.

The following example shows how to add a capability named voice_transcription to wear.xml:

<resources>
    <string-array name="android_wear_capabilities">
        <item>voice_transcription</item>
    </string-array>
</resources>

Sending and Receiving Messages between Wear and Mobile

Sending Message

Pick a path (think of it like a subject) for your message.

wearSocket.sendMessage("myPath","myMessage");

Receiving a Message

Start your message listener and listen for the path that you are associating with your messages.

wearSocket.startMessageListener(context,"myPath");

Implement WearSocket.MessageListener and override messageReceived to start listening for messages

public class MyActivity implements WearSocket.MessageListener
    @Override
    public void messageReceived(String path, String message) {
        //Do things here!
    }

Synching Data Updates between Wear and Mobile

Updating a data item

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);

Receiving a data item update

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!
    }

License and Attribution

MIT License

WearSocket uses: Gson, Android Wearable APIs, Google Play Services

About

A wrapper for sending/receiving messages and data changes between Android Wear and Android Mobile.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages