我想向两个不同的 Android 智能手表发送消息。在移动端Wearable.MessageApi.sendMessage
被调用并且sendMessageResult.getStatus().isSuccess()
是真的,所以消息的发送应该工作。现在的问题是,该onMessageReceived
方法MyWearableListenerService
从不接收消息。我究竟做错了什么?这是我正在使用的代码:
Android 应用程序的 MainActivity。
左/右方法正在发送消息
public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
private static final String TAG_CONNECTION = "Connection";
private static final String TAG_NODES = "Nodes";
private static final String START_ACTIVITY_PATH = "/start_MainActivity";
private String leftWatch;
private String rightWatch;
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setting up the Wearable API Client
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Wearable.API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.e(TAG_CONNECTION, "Connection established");
}
@Override
public void onConnectionSuspended(int i) {
Log.e(TAG_CONNECTION, "Connection suspended");
}
})
.build();
mGoogleApiClient.connect();
// Find the connected watches and store their UUIDs to distinguish at a later moment
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(@NonNull NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
List<Node> nodes = getConnectedNodesResult.getNodes();
if (nodes.isEmpty()) {
Log.e(TAG_NODES, "No Nodes found");
return;
}
rightWatch = nodes.get(0).getId();
Log.e(TAG_NODES,"Node is nearby: "+nodes.get(0).isNearby());
Log.e(TAG_NODES, rightWatch);
leftWatch = nodes.get(1).getId();
Log.e(TAG_NODES,"Node is nearby: "+nodes.get(1).isNearby());
Log.e(TAG_NODES, leftWatch);
}
});
}
@Override
public void onConnectionFailed(ConnectionResult result) {
// Executed upon failed connection to Wearable API
// e.g. when Android Wear App is missing
Log.e(TAG_CONNECTION, "Connection failed");
}
public void left(View view) {
Log.e(TAG_CONNECTION, "Trying to send message to: " + leftWatch);
if (!mGoogleApiClient.isConnected()) mGoogleApiClient.connect();
Wearable.MessageApi.sendMessage(mGoogleApiClient, leftWatch, START_ACTIVITY_PATH, new byte[0]).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
if (!sendMessageResult.getStatus().isSuccess()) {
Log.e(TAG_CONNECTION, "Failed to send message with status code: "
+ sendMessageResult.getStatus().getStatusCode());
return;
}
Log.e(TAG_CONNECTION, "Message successfully sent");
}
});
}
public void right(View view) {
Log.e(TAG_CONNECTION, "Trying to send message to: " + rightWatch);
if (!mGoogleApiClient.isConnected()) mGoogleApiClient.connect();
Wearable.MessageApi.sendMessage(mGoogleApiClient, rightWatch, START_ACTIVITY_PATH, new byte[0]).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
if (!sendMessageResult.getStatus().isSuccess()) {
Log.e(TAG_CONNECTION, "Failed to send message with status code: "
+ sendMessageResult.getStatus().getStatusCode());
return;
}
Log.e(TAG_CONNECTION, "Message successfully sent");
}
});
}
public void both(View view) {
Log.e(TAG_CONNECTION, "Trying to send message to: All currently connected watches");
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(@NonNull NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
List<Node> nodes = getConnectedNodesResult.getNodes();
for (final Node node : nodes) {
Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), START_ACTIVITY_PATH, new byte[0]).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
if (!sendMessageResult.getStatus().isSuccess()) {
Log.e(TAG_CONNECTION, "Failed to send message with status code: "
+ sendMessageResult.getStatus().getStatusCode());
return;
}
Log.e(TAG_CONNECTION, "Message successfully sent to: " + node.getId());
}
});
}
}
});
}
@Override
protected void onDestroy() {
mGoogleApiClient.disconnect();
super.onDestroy();
}
}
MyWearableListenerService
public class MyWearableListenerService extends WearableListenerService {
private static final String TAG_SIGNAL = "Signal";
private static final String TAG_CONNECTION = "Connection";
private static final String START_ACTIVITY_PATH = "/start_MainActivity";
@Override
public void onMessageReceived(MessageEvent messageEvent) {
Log.e(TAG_SIGNAL, "Message received: " + messageEvent.getPath());
if (messageEvent.getPath().equals(START_ACTIVITY_PATH)) {
Intent intent = new Intent(this, MainWearActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else super.onMessageReceived(messageEvent);
}
@Override
public void onPeerConnected(Node node) {
Log.e(TAG_CONNECTION, "Peer connected: " + node.getId());
super.onPeerConnected(node);
}
@Override
public void onPeerDisconnected(Node node) {
Log.e(TAG_CONNECTION, "Peer disconnected: " + node.getId());
super.onPeerDisconnected(node);
}
}
穿戴端的 AndroidManifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="laufnavigation.awp.hska.de.androidwearapp">
<uses-feature android:name="android.hardware.type.watch" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.DeviceDefault">
<uses-library
android:name="com.google.android.wearable"
android:required="false" />
<activity android:name=".MainWearActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyWearableListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
<data android:scheme="wear" android:host="*" />
</intent-filter>
</service>
</application>
</manifest>