igodie
igodie

Reputation: 537

WearOS app does not receive data from android phone

I have a simple program where I just send a message from my phone to my wearos emulator and vice versa but the onReceive function app never gets called on both devices. (and yes, I have paired my phone with the emulator)

This is my android phone code:

public class MainActivity extends AppCompatActivity {

Button talkbutton;
TextView textview;
protected Handler myHandler;
int receivedMessageNumber = 1;
int sentMessageNumber = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    talkbutton = findViewById(R.id.talkButton);
    textview = findViewById(R.id.textView);

    //Create a message handler//
    myHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            Bundle stuff = msg.getData();
            messageText(stuff.getString("messageText"));
            return true;
        }
    });

    IntentFilter messageFilter = new IntentFilter(Intent.ACTION_SEND);
    Receiver messageReceiver = new Receiver();
    LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, messageFilter);

}

public void messageText(String newinfo) {
    if (newinfo.compareTo("") != 0) {
        textview.append("\n" + newinfo);
    }
}

//Define a nested class that extends BroadcastReceiver//
public class Receiver extends BroadcastReceiver {
    @Override

    public void onReceive(Context context, Intent intent) {
        String message = "I just received a message from the wearable " + receivedMessageNumber++;;
        textview.setText(message);
    }
}

public void talkClick(View v) {
    String message = "Sending message.... ";
    textview.setText(message);
    new NewThread("/my_path", message).start();

}

//Use a Bundle to encapsulate the message//
public void sendmessage(String messageText) {
    Bundle bundle = new Bundle();
    bundle.putString("messageText", messageText);
    Message msg = myHandler.obtainMessage();
    msg.setData(bundle);
    myHandler.sendMessage(msg);

}

class NewThread extends Thread {
    String path;
    String message;

    //Constructor for sending information to the Data Layer//
    NewThread(String p, String m) {
        path = p;
        message = m;
    }

    public void run() {

    //Retrieve the connected devices
        Task<List<Node>> wearableList =
                Wearable.getNodeClient(getApplicationContext()).getConnectedNodes();
        try {

            List<Node> nodes = Tasks.await(wearableList);
            for (Node node : nodes) {
                Task<Integer> sendMessageTask =

                //Send the message//
                Wearable.getMessageClient(MainActivity.this).sendMessage(node.getId(), path, message.getBytes());
                Integer result = Tasks.await(sendMessageTask);
                sendmessage("I just sent the wearable a message " + sentMessageNumber++);
            }

        } catch (Exception exception) {
            Toast.makeText(getApplicationContext(),exception.getMessage(),Toast.LENGTH_LONG);
        }
    }
}}

MessageService which sends the message to the wear os app:

public class MessageService extends WearableListenerService {

@Override
public void onMessageReceived(MessageEvent messageEvent) {

    if (messageEvent.getPath().equals("/my_path")) {

        final String message = new String(messageEvent.getData());

        Intent messageIntent = new Intent();
        messageIntent.setAction(Intent.ACTION_SEND);
        messageIntent.putExtra("message", message);
        LocalBroadcastManager.getInstance(this).sendBroadcast(messageIntent);
    }
    else {
        super.onMessageReceived(messageEvent);
    }
}

Since the receive and send function from both apps are very identical i think its enough to show you the code from the android device but if you like here is the receive method from the wearos app:

    IntentFilter newFilter = new IntentFilter(Intent.ACTION_SEND);
    Receiver messageReceiver = new Receiver();

    LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, newFilter);



    public class Receiver extends BroadcastReceiver {
     @Override
     public void onReceive(Context context, Intent intent) {

     //Display the following when a new message is received//

     String onMessageReceived = "I just received a message from the handheld " + receivedMessageNumber++;
     textView.setText(onMessageReceived);

     }
    }

Upvotes: 1

Views: 482

Answers (1)

James Allen
James Allen

Reputation: 7169

Have you added the required intent filter in your Manifest.xml for the WearableListenerService? Something like this:

     <service android:name="your.package.YourWatchListenerService">
            <intent-filter>
                <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
                <data
                    android:host="*"
                    android:pathPrefix="/my_path"
                    android:scheme="wear" />
            </intent-filter>
        </service>

Upvotes: 0

Related Questions