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

UsefulAnd/WearSocket

 
 

Repository files navigation

WearSocket

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

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.

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

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.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

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

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%