我从掌上电脑发送消息取得了成功,但是可穿戴设备没有收到消息:
我的掌上电脑在这里发送消息:
@Override
public void onConnected(Bundle arg0) {
new SendToDataLayerThread(IS_PAID_VALUE, String.valueOf(Constants.PackageProperties.IS_PAID)).start();
}
private class SendToDataLayerThread extends Thread {
String path;
String message;
// Constructor to send a message to the data layer
SendToDataLayerThread(String p, String msg) {
path = p;
message = msg;
}
public void run() {
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
for (Node node : nodes.getNodes()) {
MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), path, message.getBytes()).await();
if (result.getStatus().isSuccess()) {
Log.v("myTag", "Message: {" + message + "} sent to: " + node.getDisplayName());
}
else {
// Log an error
Log.v("myTag", "ERROR: failed to send Message");
}
}
}
}
public class ListenerService extends WearableListenerService
implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String IS_PAID_VALUE = "/is-paid-value";
private GoogleApiClient mGoogleApiClient;
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
mBroadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
try {
mGoogleApiClient.connect();
} catch (Exception e) {
Log.d(TAG, "onCreate: mGoogleApiClient.connect exception: " + e);
}
}
@Override
public void onMessageReceived(MessageEvent messageEvent) {
Log.d("#####", "onMessageReceived: " + messageEvent);
if (messageEvent.getPath().equals(IS_PAID_VALUE)) {
String isPaid = new String(messageEvent.getData());
Utilities.PackageProperties pp = new Utilities.PackageProperties();
pp.setIsPaid(Boolean.parseBoolean(isPaid));
}
}
}
我从不在听众中输入我的onMessageReceived。我把它在我的清单中声明:
答案 0 :(得分:2)
在为可穿戴设备开发时,我遇到了一个非常类似的问题。在我的情况下,我无法在移动设备上收到任何消息,即使它们已成功从可穿戴设备发送。
在AndroidManifest,build.gradle和yada yada yada(相同的包名,applicationId,权限等)中设置了所有内容之后,我发现移动设备上的应用程序有几种构建类型和风格。 所以......我设法解决了在可穿戴设备上配置风格和构建类型的问题。 之后,我可以在两台设备之间成功发送和接收消息。
希望它对你有所帮助。干杯!