guy-keller / cordova-wearos-plugin

Simple plugin that establishes a session with a WearOs Smartwach and helps exchange of messages between a cordova application and its WearOs Watch application and vice-versa.
3 stars 3 forks source link

can you please provide android wear side code #1

Closed SureshMangal closed 5 years ago

SureshMangal commented 5 years ago

I am new to phonegap and need to establish communication between phone app and wearOS app. I have added plugin you provide and i am not aware what to write in android Wear to handle the messages could you please provide an example on communication.

guy-keller commented 5 years ago

Hi @SureshMangal

Step 1: You will need to create a Listener that will receive the messages from the mobile app:

public class MyMessageListener implements MessageClient.OnMessageReceivedListener {

    private static final String TAG = MyMessageListener.class.getSimpleName();
    private static final String MESSAGE_PATH = "/cordova/plugin/wearos";

    private Context context;

    public MyMessageListener(Context context) {
        this.context = context;
        Log.i(TAG, "constructor");
    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        Log.i(TAG,"onMessageReceived");
        if(messageEvent != null && MESSAGE_PATH.equals(messageEvent.getPath())){
            String message = new String(messageEvent.getData(), StandardCharsets.UTF_8);
            System.out.println("Message received: " + message);
        }
    }

}

Step 2: In the main Activity, you can now register to receive updates, eg:

    protected void listenToMessages(Context context) {
        Log.i(TAG,"listenToMessages");
        if (this.messageListener == null) {
            this.messageListener = new MyMessageListener(context);
            Wearable.getMessageClient(context).addListener(this.messageListener);
        }
    }

I will add this to the code example/sample usage. Note: This is more of a StackOverflow question though,

SureshMangal commented 5 years ago

Hi @SureshMangal

Step 1: You will need to create a Listener that will receive the messages from the mobile app:

public class MyMessageListener implements MessageClient.OnMessageReceivedListener {

    private static final String TAG = MyMessageListener.class.getSimpleName();
    private static final String MESSAGE_PATH = "/cordova/plugin/wearos";

    private Context context;

    public MyMessageListener(Context context) {
        this.context = context;
        Log.i(TAG, "constructor");
    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        Log.i(TAG,"onMessageReceived");
        if(messageEvent != null && MESSAGE_PATH.equals(messageEvent.getPath())){
            String message = new String(messageEvent.getData(), StandardCharsets.UTF_8);
            System.out.println("Message received: " + message);
        }
    }

}

Step 2: In the main Activity, you can now register to receive updates, eg:

    protected void listenToMessages(Context context) {
        Log.i(TAG,"listenToMessages");
        if (this.messageListener == null) {
            this.messageListener = new MyMessageListener(context);
            Wearable.getMessageClient(context).addListener(this.messageListener);
        }
    }

I will add this to the code example/sample usage. Note: This is more of a StackOverflow question though,

Thank you @guikeller for the message listener. Could you please add the code of sendMessage from Wear app to Phone app. Thanks InAdvance.

guy-keller commented 5 years ago

Hi @SureshMangal

Following below how to send a message from the WearOS to the Android app:

    private static final String TAG = MyMessageListener.class.getSimpleName();
    private static final String MESSAGE_PATH = "/cordova/plugin/wearos";

    public void sendMsgToAllNodes() {
        this.sendMessage("Hello World from WearOS!");
    }

    protected void sendMessage(final String msg) {
        Log.i(TAG, "sendMessage: " + msg);
        NodeClient nodeClient = Wearable.getNodeClient(context);
        Task<List<Node>> connectedNodes = nodeClient.getConnectedNodes();
        connectedNodes.addOnCompleteListener(new OnCompleteListener<List<Node>>() {
            @Override
            public void onComplete(@NonNull Task<List<Node>> task) {
                List<Node> nodes = task.getResult();
                for(Node node : nodes) {
                    sendMessageToNode(node.getId(), msg);
                }
            }
        });
    }

    protected void sendMessageToNode(String nodeId, String msg) {
        Log.i(TAG, "sendMessageToNode");
        byte[] message = msg.getBytes();
        Wearable.getMessageClient(context).sendMessage(nodeId, MESSAGE_PATH, message);
    }

Best of luck with your project!

SureshMangal commented 5 years ago

Hi @SureshMangal

Following below how to send a message from the WearOS to the Android app:

    private static final String TAG = MyMessageListener.class.getSimpleName();
    private static final String MESSAGE_PATH = "/cordova/plugin/wearos";

    public void sendMsgToAllNodes() {
        this.sendMessage("Hello World from WearOS!");
    }

    protected void sendMessage(final String msg) {
        Log.i(TAG, "sendMessage: " + msg);
        NodeClient nodeClient = Wearable.getNodeClient(context);
        Task<List<Node>> connectedNodes = nodeClient.getConnectedNodes();
        connectedNodes.addOnCompleteListener(new OnCompleteListener<List<Node>>() {
            @Override
            public void onComplete(@NonNull Task<List<Node>> task) {
                List<Node> nodes = task.getResult();
                for(Node node : nodes) {
                    sendMessageToNode(node.getId(), msg);
                }
            }
        });
    }

    protected void sendMessageToNode(String nodeId, String msg) {
        Log.i(TAG, "sendMessageToNode");
        byte[] message = msg.getBytes();
        Wearable.getMessageClient(context).sendMessage(nodeId, MESSAGE_PATH, message);
    }

Best of luck with your project!

Hi @guikeller Thank you for the code provided. I am able to get message from Mobile to wear, but no message received when sent from wear to mobile. Could you please help by uploading an example project.

guy-keller commented 5 years ago

Hi @SureshMangal

Following below some suggestions:

I will not be creating a sample project. Once again, best of luck.

guy-keller commented 5 years ago

Updated the 'README' please look at the 'Extra Info' part. Also improved slightly the code sample