Franzi Notz
Franzi Notz

Reputation: 31

Sending message from wearable to phone: onMessageRecieved not called

So I have a connected Wear Emulator and a android phone to test. Sending data maps from phone to wearable works fine.

Now I want to send a message from the wearable to the phone. I've tried this with AsyncTask and without it. Messages are being successfully sent in both cases, but the message never reaches my phone.

My application IDs are the same.

Here's my code:

//Wear

    //Try with Async
inner class requestTokenTask : AsyncTask<Void?, Void?, Void?>() {
            override fun doInBackground(vararg params: Void?): Void? {
                    Wearable.getMessageClient(this).sendMessage(_connectedNode.toString(), "/requesttoken", null)
                return null
            }

        override fun onPostExecute(aVoid: Void?) {
            super.onPostExecute(aVoid)
            Log.d(TAG, "Message sent: $aVoid")
        }

    }

    //Try without async
    fun requestToken() {
        if(_connectedNode?.id != null){
            val sendTask: Task<*> = Wearable.getMessageClient(this).sendMessage(
                    _connectedNode!!.id!!,
                    "/requesttoken",
                    null
            ).apply {
                addOnSuccessListener {
                    Log.d(TAG, "Message sent: $it")
                }
                addOnFailureListener {
                    Log.d(TAG, "Message NOT sent, error: $it")
                }
            }
        }

}

Handheld code:

    public override fun onResume() {
        super.onResume()
        Wearable.getDataClient(this).addListener(this)
        Wearable.getMessageClient(this).addListener(this)
        Wearable.getCapabilityClient(this)
                .addListener(this, Uri.parse("wear://"), CapabilityClient.FILTER_REACHABLE)

    }

override fun onMessageReceived(messageEvent: MessageEvent) {
    Log.d(TAG, "onMessageReceived()")
    //Receive the message from wear
   if (messageEvent.path.equals("/requesttoken")) {
  //Do stuff
    }
}

Manifest part:

<activity
            android:name=".wear.WearableActivity"
            android:theme="@style/Theme.Transparent">
            <intent-filter>
                <action
                    android:name="com.google.android.gms.wearable.BIND_LISTENER"/>
                <!-- listeners receive events that match the action and data filters -->
                <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
                <data android:scheme="wear" android:host="*" android:pathPrefix="/token"/>
            </intent-filter>

        </activity>

UPDATE: Just found out that "BIND_LISTENER" is deprecated, tried again with removing it and adding "MESSAGE_RECIEVED" instead, but it's still not working.

Upvotes: 1

Views: 1570

Answers (1)

Franzi Notz
Franzi Notz

Reputation: 31

I tried another way with Broadcast Recievers with this tutorial and the communication now works in both ways

Upvotes: 1

Related Questions