Skip to content

Instantly share code, notes, and snippets.

@Phocacius
Created January 7, 2015 09:53
Show Gist options
  • Save Phocacius/953cca18b51d0bf2c982 to your computer and use it in GitHub Desktop.
Save Phocacius/953cca18b51d0bf2c982 to your computer and use it in GitHub Desktop.
Simple to use communication handler between an Android handheld and an Android Wear device
public class WearableCommunicator {
private static WearableCommunicator communicator;
public static WearableCommunicator getInstance() {
if (communicator == null) {
communicator = new WearableCommunicator();
}
return communicator;
}
private GoogleApiClient apiClient;
private boolean apiClientConnected;
private int deviceConnectionStatus = 0;
public static int CONNECTION_STATUS_NOT_CHECKED = 0;
public static int CONNECTION_STATUS_CONNECTED = 1;
public static int CONNECTION_STATUS_DISCONNECTED = 2;
private Node node;
public GoogleApiClient getApiClient(final ApiClientConnectedInterface callback) {
if (apiClient == null) {
apiClient = new GoogleApiClient.Builder(Dipit.getAppContext())
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle connectionHint) {
apiClientConnected = true;
callback.onConnected(false);
}
@Override
public void onConnectionSuspended(int i) {
callback.onConnected(true);
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
callback.onConnected(true);
}
})
// Request access only to the Wearable API
.addApi(Wearable.API)
.build();
apiClient.connect();
}
return apiClient;
}
public GoogleApiClient getApiClient() {
if (apiClient == null || !apiClientConnected) {
return null;
}
return apiClient;
}
public void hasConnectedDevice(final WearablesScanFinishedInterface callback) {
if (deviceConnectionStatus == CONNECTION_STATUS_NOT_CHECKED) {
getApiClient(new ApiClientConnectedInterface() {
@Override
public void onConnected(boolean isError) {
if (isError) {
deviceConnectionStatus = CONNECTION_STATUS_DISCONNECTED;
callback.onScanFinished(false);
return;
}
Wearable.NodeApi.getConnectedNodes(apiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
List<Node> nodes = getConnectedNodesResult.getNodes();
deviceConnectionStatus = nodes.size() == 0 ? CONNECTION_STATUS_DISCONNECTED : CONNECTION_STATUS_CONNECTED;
if (nodes.size() > 0) {
node = nodes.get(0);
}
callback.onScanFinished(deviceConnectionStatus == CONNECTION_STATUS_CONNECTED);
}
});
}
});
} else {
callback.onScanFinished(deviceConnectionStatus == CONNECTION_STATUS_CONNECTED);
}
}
public void sendMessage(final String s, Serializable o) {
final byte[] data = SerializationUtils.serialize(o);
hasConnectedDevice(new WearablesScanFinishedInterface() {
@Override
public void onScanFinished(boolean hasConnectedDevice) {
if (hasConnectedDevice) {
Wearable.MessageApi.sendMessage(
getApiClient(), node.getId(), s, data).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
}
});
}
}
});
}
public Serializable interpretMessage(MessageEvent event) {
return (Serializable) SerializationUtils.deserialize(event.getData());
}
public void sendAsset(final Bitmap bitmap, final int position) {
hasConnectedDevice(new WearablesScanFinishedInterface() {
@Override
public void onScanFinished(boolean hasConnectedDevice) {
if (hasConnectedDevice) {
final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
Asset asset = Asset.createFromBytes(byteStream.toByteArray());
PutDataRequest request = PutDataRequest.create(ACTION_RESPONSE_IMAGE);
request.putAsset("profileImage", asset);
request.setData(SerializationUtils.serialize(position));
Wearable.DataApi.putDataItem(apiClient, request);
}
}
});
}
public interface WearablesScanFinishedInterface {
public void onScanFinished(boolean hasConnectedDevice);
}
public interface ApiClientConnectedInterface {
public void onConnected(boolean isError);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment