OnMessageReceived not called in WearableListenerService
Asked Answered
M

3

15

I am working on android wear app using Eclipse IDE.I am using same package names for wear app and mobile app and i am packing wearable app manually according to google documentation.Everything is working fine.it is installed on Android wear emulator using usb debugging with phone.

My problem is when i am sending a message to wearable using following code

List<Node> nodeList=getNodes();
for(Node node : nodeList) {
    Log.v(" ", "telling " + node.getId() );

    PendingResult<MessageApi.SendMessageResult> result = Wearable.MessageApi.sendMessage(
        mGoogleApiClient,
        node.getId(),
        START_ACTIVITY_PATH,
        null
    );

    result.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
        @Override
        public void onResult(MessageApi.SendMessageResult sendMessageResult) {
            Log.v(" ", "Phone: " + sendMessageResult.getStatus().getStatusMessage());
        }
    });
}

the OnPeerConnected method is running when devices are peered but OnMessageReceived never called in WearableListenerService.This is my WearableListenerService code:

public class DataLayerListenerService extends WearableListenerService {

    private static final String TAG = "DataLayerSample";
    private static final String START_ACTIVITY_PATH = "/start/MainActivity";
    private static final String DATA_ITEM_RECEIVED_PATH = "/data-item-received";
    private static final String LOG_TAG = "log";
    @Override
    public void onPeerConnected(Node peer) {
        super.onPeerConnected(peer);

        String id = peer.getId();
        String name = peer.getDisplayName();

        Log.d(LOG_TAG, "Connected peer name & ID: " + name + "|" + id);
    }
    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        System.out.println("Recevive message3");
    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        System.out.println("service watch message1");
        if (messageEvent.getPath().equals(START_ACTIVITY_PATH)) {
            System.out.println("service watch message2");
            Intent startIntent = new Intent(this, MainActivity.class);
            startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startIntent);
        }
    }
}

Also a warning message in Logcat always appears :

app does not match record's app key: AppKey[com.myapp,c3f31717fa35401056c20a2798907f1232efa75e] != AppKey[com.myapp,f36e726eefc7e528db26a1c25f6fbf2f93dacd70]

If app key for both apps should be same then how can i create same app key for both the apps.

Any help is highly appreciated, Thanks.

Merry answered 25/7, 2014 at 14:16 Comment(2)
What are com.myphoneapp and com.wearableapp?Polysynthetic
@Maciej. It was by mistake.Now i have corrected my post. one "com.myapp" statement is for package name of phone app and second one is for wear app.Anyway thanks for reply.Merry
G
13

The error message you have:

app does not match record's app key: AppKey[com.myapp,c3f31717fa35401056c20a2798907f1232efa75e] != AppKey[com.myapp,f36e726eefc7e528db26a1c25f6fbf2f93dacd70]

Indicated that your apps are signed with the different keys.
Package names of phone and wearable apps are the same - that is good, but they also need to share the same signature. This is the reason why messages cannot be delivered - wearable apps are recognized as "part of the same app" based on the package name and signature.

Please make sure that you have both apps signed with the same key. If you are testing the autoinstallation feature please make sure to uninstall the debug version of wearable app from watch emulator.

Grande answered 28/7, 2014 at 7:7 Comment(5)
Thanks @Maciej .Now i can see the problem.Wearable app is signed but android app is installed directly on phone(through usb debugging).If i am right then what should i do to make it signed? or According to you,if uninstalling the debug version can solve this issue,then how can i do this.Please guide..Merry
You say your wearable app is signed - how have you signed it? You need to sign the phone app too in order to let them communicate with each other. "Uninstalling only" will not resolve the issue:) - you need to replace the it with signed version (this means: uninstall unsigned one + install new signed one). Please notice that for tests and development you can use both unsigned apps. Only while doing some final testing (before publishing on Store) you can test the final result on signed apps. Also please tell me more details about your instllation, why do you have one app signed and other not?Polysynthetic
I package wearable app into mobile app using signed keystore certificates.Now i am testing it on my mobile using usb debugging.When I test using usb,there is no option for signing my phone app i.e. i am not creating its apk file and installing it directly on phone.So i think that my phone app is unsigned.it becomes a confusing situation for me atleast.Merry
If you want to test a signed phone apk you need to package phone apk and sign it with your keystore. After that step you'll have to install this apk using adb command: adb -d install path/to/your/phone_signed.apk. -d means that this command will be directed to any device connected cia adb (not to emulator instances - if you have Android Wear emulator launched on your PC). If you not familiar with adb you can do a "easy way" and just copy the signed apk to phone's memory (from pc) and instlal it using some file browser on your phone. You will need to uninstall the unsigned version first.Polysynthetic
Thanks Maciej.Finally your solution works for me.Thnnks a bunch for your valuable time.Merry
S
11

I had the same error, my fault was that the "wear" module's package name was not the same as the app's.

BAD:
[module: app] es.voghdev.myapp
[module: wear] es.voghdev.myapp.wear

GOOD:
[module: app] es.voghdev.myapp
[module: wear] es.voghdev.myapp

Made me waste so much time!! >:-(

Shoulder answered 19/3, 2015 at 10:20 Comment(0)
D
2

Use an asyntask to send messages as they will block the ui thread. Also you need to call the await method. To get the apps to have the same key, you need to use build variants with gradle.

public class SendMessageTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... voids) {
        NodeApi.GetConnectedNodesResult nodes =
                Wearable.NodeApi.getConnectedNodes(apiClient).await();
        for (Node node : nodes.getNodes()) {
            Wearable.MessageApi
                    .sendMessage(apiClient, node.getId(), "/start/MainActivity", null)
                    .await();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        Toast.makeText(MainActivity.this, "Message Sent", Toast.LENGTH_SHORT).show();
    }
}
Decortication answered 25/7, 2014 at 17:11 Comment(1)
Using an AsyncTask is not necessary as the MessageApi already has Async support. Just replace the .await() call with one to setResultCallback() passing the callback to be made when the call is complete. using AsyncTask will just create an additional thread, which is not needed.Severin

© 2022 - 2024 — McMap. All rights reserved.