Hello forum, I am struggling with this for a day. I have tried all I can find on the internet, but obviously I am not getting it.
I have a watch app that once you press a button should sent a "pice of text" to the phone. However I can not get it to work.
All the code and log statements return good from the watch - it finds the mobile and does not return an error. I dont get any of the log statements from the mobile code and nothing happens (no toast).
Any help will be very gratefully received as I am at the end of my tether !!!!
Kind Regards,
Harold Clements
Listener Class (mobile)
Manifest.xml
Button Press Code on the Watch
I have a watch app that once you press a button should sent a "pice of text" to the phone. However I can not get it to work.
All the code and log statements return good from the watch - it finds the mobile and does not return an error. I dont get any of the log statements from the mobile code and nothing happens (no toast).
Any help will be very gratefully received as I am at the end of my tether !!!!
Kind Regards,
Harold Clements
Listener Class (mobile)
Code:
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.WearableListenerService;
/**
* Created by haroldjclements on 05/09/2016.
*/
public class ListenerServiceFromWear extends WearableListenerService {
private static final String CLASS_NAME = "ListenerServiceFromWear";
private final static String W_PATH = "/from-ware";
@Override
public void onMessageReceived(MessageEvent messageEvent) {
Log.e(CLASS_NAME, "onMessageReceived");
super.onMessageReceived(messageEvent);
if(messageEvent.getPath().equals(W_PATH)) {
Log.e(CLASS_NAME, "onMessageReceived : " + W_PATH);
String msg = new String(messageEvent.getData());
Log.e(CLASS_NAME, "msg : " + msg);
String toastMsg = msg;
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
}
}
}
Manifest.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="innovation.sita.aero.metime">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"/>
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SelectionActivity" />
<activity android:name=".DetailsActivity" />
<activity android:name=".DetailsWaitActivity"></activity>
<service android:name=".ListenerServiceFromWear">
<intent-filter>
<action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
<action android:name="com.google.android.gms.wearable.CAPABILITY_CHANGED" />
<action android:name="com.google.android.gms.wearable.CHANNEL_EVENT" />
<data android:scheme="wear" android:host="*" android:pathPrefix=".*" />
</intent-filter>
</service>
</application>
</manifest>
Button Press Code on the Watch
Code:
public class OfferViewActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static final String CLASS_NAME = "W_OfferViewActivity";
private TextView mTextView;
private Node node;
private GoogleApiClient api;
private final static String W_PATH = "/from-ware";
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(CLASS_NAME, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_offer_view);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mTextView = (TextView) stub.findViewById(R.id.text);
}
});
api = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.d(CLASS_NAME, "onConnected");
Wearable.NodeApi.getConnectedNodes(api)
.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(@NonNull NodeApi.GetConnectedNodesResult nodesResult) {
Log.d(CLASS_NAME, "onConnected : onResult");
for (Node n : nodesResult.getNodes()) {
if (n != null && n.isNearby()) {
Log.d(CLASS_NAME, "onConnected : onResult : " + n.getDisplayName());
node = n;
}
if (n == null) {
Log.d(CLASS_NAME, "onConnected : onResult : cannot connect");
}
}
}
});
}
@Override
public void onConnectionSuspended(int i) {
Log.d(CLASS_NAME, "onConnectionSuspended");
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.d(CLASS_NAME, "onConnectionFailed");
}
@Override
protected void onStart() {
Log.d(CLASS_NAME, "onStart");
super.onStart();
api.connect();
}
@Override
protected void onStop() {
Log.d(CLASS_NAME, "onStop");
super.onStop();
api.disconnect();
}
private void sendMessageToNode(String msg, Node node) {
Log.d(CLASS_NAME, "sendMessageToNode : " + msg);
if(node != null && api !=null) {
Log.d(CLASS_NAME, "sendMessageToNode : " + node.getDisplayName());
Wearable.MessageApi.sendMessage(api, node.getId(), W_PATH, msg.getBytes())
.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(@NonNull MessageApi.SendMessageResult sendMessageResult) {
Log.d(CLASS_NAME, "sendMessageToNode : sendMessage :: onResult");
if(!sendMessageResult.getStatus().isSuccess()) {
Log.d(CLASS_NAME, "sendMessageToNode : sendMessage :: onResult - Failed - " + sendMessageResult.getStatus().getStatusMessage());
} else {
Log.d(CLASS_NAME, "sendMessageToNode : sendMessage :: onResult - Success");
}
}
});
}
}
public void openOnDevice(View view) {
sendMessageToNode("getQRCodes", node);
}
}