我正在尝试从Android手机向可穿戴设备发送消息,但永远不会调用WearableListenerService中的onMessageReceived方法。我已经验证来自手机的消息成功发送到正确的节点,因此问题似乎出现在可穿戴应用程序的接收端。这是我在可穿戴项目中的监听器类的代码:
public class MessageListener extends WearableListenerService implements GoogleApiClient.ConnectionCallbacks {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if (messageEvent.getPath().equals("/message_path")) {
final String message = new String(messageEvent.getData());
// Broadcast message to wearable activity for display
Intent messageIntent = new Intent();
messageIntent.setAction(Intent.ACTION_SEND);
messageIntent.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(messageIntent);
}
else {
super.onMessageReceived(messageEvent);
}
}
@Override
public void onPeerConnected(Node peer) {
Log.v("myTag", "Peer connected: " + peer.getDisplayName());
}
@Override
public void onConnected(Bundle bundle) {
Wearable.MessageApi.addListener(MainActivityWear.client, this);
}
@Override
public void onConnectionSuspended(int i) {}
}
通过设置断点,我发现onMessageReceived方法和onConnected方法都没有被调用。
以下是可穿戴项目的MainActivity:
public class MainActivityWear extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private TextView mTextView;
private MessageReceiver messageReceiver;
public static GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_wear);
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);
}
});
client = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
// Register the local broadcast receiver
IntentFilter messageFilter = new IntentFilter(Intent.ACTION_SEND);
messageReceiver = new MessageReceiver();
LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, messageFilter);
}
// Connect to the data layer when the Activity starts
@Override
protected void onStart() {
super.onStart();
client.connect();
}
@Override
public void onStop() {
super.onStop();
unregisterReceiver(messageReceiver);
}
@Override
public void onConnected(@Nullable Bundle bundle) {}
@Override
public void onConnectionSuspended(int i) {}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {}
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("message");
// Display message in UI
mTextView.setText(message);
}
}
}
MessageReceiver是一个嵌套类。 一切都正确编译,但从未收到消息。提前谢谢!
答案 0 :(得分:0)
检查应用程序和设备中的GoogleServicesAPI版本。它们必须是一样的。
检查您的佩戴和手持设备是否与您的应用程序版本相同。如果不是 - 使用最旧的。
PS
不要使用onPeerConnected
和onConnectionSuspended
。这些方法已被弃用,不会被触发。