我想从手机发送信息到佩戴(安卓手表)。我不想发送通知。所以我使用MessageApi发送消息。出于某种原因,我的手表没有得到它。我以相反的顺序使用相同的代码(磨损到移动设备),它工作得非常好。从手机端,它正确识别手表并显示其名称但在观察时未到达消息(未调用onMessageReceived)。
非常感谢任何帮助。
Mobile中用于发送数据的代码:
private void resolveNode() {
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient)
.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(@NonNull NodeApi.GetConnectedNodesResult connectedNodes) {
for(Node connectedNode : connectedNodes.getNodes()) {
mNode = connectedNode;
Log.d(TAG,"Message Sender connected node"+mNode.getDisplayName());
sendMessage("Hello Hello");
}
}
});
}
private void sendMessage(final String message) {
if(mGoogleApiClient != null &&
mGoogleApiClient.isConnected() &&
mNode != null) {
Log.d(TAG,"Message is going to be sent to watch");
Wearable.MessageApi.sendMessage(mGoogleApiClient,
mNode.getId(),
"/mobile_data",
message.getBytes())
.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(@NonNull MessageApi.SendMessageResult sendMessageResult) {
if(sendMessageResult.getStatus().isSuccess()) {
Log.e(TAG,"Message Succesfully sent to watch=>"+message);
} else {
Log.e(TAG,"Message FAILED TO BE SENT to watch=>"+message);
}
}
});
}
}
可穿戴方面的代码
public class MessageReceiver extends WearableListenerService {
private static final String TAG = "MessageReceiver";
@Override
public void onMessageReceived(MessageEvent messageEvent) {
super.onMessageReceived(messageEvent);
Log.d(TAG,"pathpathpathpath");
}
}
在可穿戴方的AndroidManifest中注册了我的留言接收器
<service android:name=".MessageReceiver">
<intent-filter>
<action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
<data
android:host="*"
android:pathPrefix="/mobile_data"
android:scheme="mobile" />
</intent-filter>
</service>