Sending messages from Android Wear to host device

前端 未结 7 1956
忘了有多久
忘了有多久 2020-12-02 19:27

I\'m writing a custom Android Wear application that\'s supposed to fire a one-off message to the connected host device (the phone). Digging through the API, I found the foll

7条回答
  •  情话喂你
    2020-12-02 19:54

    Try add await() at the end of your sendMessage() method.

    PendingResult messageResult = Wearable.MessageApi.sendMessage(googleApiClient, node.getId(),
                                    PATH, null).await();
    

    The previous answer below is for sending message only when the Activity in Android device (mobile) is active.

    Since you are trying to send message from Android Wear to Android device, the message listener should be added in the Activity in the Android device not in Android Wear, the following codes should be added into MainActivity in Android (mobile)

    final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .build();
    
    googleApiClient.connect();
    
    Wearable.MessageApi.addListener(googleApiClient, new MessageApi.MessageListener() {
             @Override
             public void onMessageReceived(MessageEvent messageEvent) {
                      Log.d(TAG, "Message received: " + messageEvent.getPath());
             }
    });
    

    You can also try the simpler SendMessage() method

    SendMessageResult result = Wearable.MessageApi.sendMessage(
            mGoogleApiClient, node.getId(), "STRING TO BE SENT", null).await();
    if (!result.getStatus().isSuccess()) {
        Log.e(TAG, "ERROR: failed to send Message: " + result.getStatus());
    }
    

提交回复
热议问题