I been following this tutorial to send a message from an Android wear to an Android phone, but it is not working. This is my code:
Wear
To start with the Minifest, I implemented the google play services metadata, like this:
<meta-data
android:name="com.google.android.wearable.standalone"
android:value="true" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
Second of all, I initialize the GoogleApiClient, like this:
googleClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
Then, I implement all the methods required from the interfaces: GoogleApiClient.ConnectionCallbacks and GoogleApiClient.OnConnectionFailedListener, like this:
@Override
public void onConnected(Bundle connectionHint) {
System.out.println("SESSION CONNECTED");
}
// Disconnect from the data layer when the Activity stops
@Override
protected void onStop() {
if (null != googleClient && googleClient.isConnected()) {
googleClient.disconnect();
}
super.onStop();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
protected void onStart() {
super.onStart();
googleClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
System.out.println("FAILED TO CONNECT: "+connectionResult.getErrorMessage());
}
Finally I implement the send the text to the phone:
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleClient).await();
for (Node node : nodes.getNodes()) {
MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(googleClient, node.getId(), path, message.getBytes()).await();
if (result.getStatus().isSuccess()) {
System.out.println("Message: {" + message + "} sent to: " + node.getDisplayName());
}
else {
// Log an error
System.out.println("ERROR: failed to send Message");
}
}
The thing is that it seems to work (I get the ""SESSION CONNECTED" string when I start the wear), but it can´t find any paired devices to send the string to, since the list of nodes is empty. Could it be a problem this the simulator? Both the phone and the wear may not be sycronized, and thus is why they can´t find each other.
Phone
Just to show the hole example, I´m also including the phone part:
Manifest.xml
<service android:name=".utils.ListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER"
tools:ignore="WearableBindListener" />
</intent-filter>
</service>
ListenerService
public class ListenerService extends WearableListenerService {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if (messageEvent.getPath().equals("/message_path")) {
final String message = new String(messageEvent.getData());
System.out.println("Message path received on watch is: " + messageEvent.getPath());
System.out.println("Message received on watch is: " + message);
}
else {
super.onMessageReceived(messageEvent);
}
}
Ok nevermind, it was a problem with the emulator, that they were not sycned. Just trying out with the real devices and it worked.