Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yesidlazaro/6cc6245ee0cf09855f36 to your computer and use it in GitHub Desktop.
Save yesidlazaro/6cc6245ee0cf09855f36 to your computer and use it in GitHub Desktop.
<service android:name=".ListenerServiceFromWear">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
wearApp project(':wear')
compile 'com.google.android.gms:play-services-wearable:+'
}
public class ListenerServiceFromWear extends WearableListenerService {
private static final String HELLO_WORLD_WEAR_PATH = "/hello-world-wear";
@Override
public void onMessageReceived(MessageEvent messageEvent) {
/*
* Receive the message from wear
*/
if (messageEvent.getPath().equals(HELLO_WORLD_WEAR_PATH)) {
Intent startIntent = new Intent(this, MyActivity.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startIntent);
}
}
}
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "com.google.android.support:wearable:1.0.+"
compile 'com.google.android.gms:play-services-wearable:+'
}
public class LaunchActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
Node mNode; // the connected device to send the message to
GoogleApiClient mGoogleApiClient;
private static final String HELLO_WORLD_WEAR_PATH = "/hello-world-wear";
private boolean mResolvingError=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
//Connect the GoogleApiClient
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
//UI elements with a simple CircleImageView
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
CircledImageView mCircledImageView = (CircledImageView) stub.findViewById(R.id.circle);
//Listener to send the message (it is just an example)
mCircledImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage();
}
});
}
});
}
/**
* Send message to mobile handheld
*/
private void sendMessage() {
if (mNode != null && mGoogleApiClient!=null && mGoogleApiClient.isConnected()) {
Wearable.MessageApi.sendMessage(
mGoogleApiClient, mNode.getId(), HELLO_WORLD_WEAR_PATH, null).setResultCallback(
new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
if (!sendMessageResult.getStatus().isSuccess()) {
Log.e("TAG", "Failed to send message with status code: "
+ sendMessageResult.getStatus().getStatusCode());
}
}
}
);
}else{
//Improve your code
}
}
@Override
protected void onStart() {
super.onStart();
if (!mResolvingError) {
mGoogleApiClient.connect();
}
}
/*
* Resolve the node = the connected device to send the message to
*/
private void resolveNode() {
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult nodes) {
for (Node node : nodes.getNodes()) {
mNode = node;
}
}
});
}
@Override
public void onConnected(Bundle bundle) {
resolveNode();
}
@Override
public void onConnectionSuspended(int i) {
//Improve your code
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
//Improve your code
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment