Unable to send message from Android Wear to host device

I am writing a simple Android application and I need to send a message from my Wear device to my mobile phone using MessageApi. Ultimately, I want you to be able to send a message from my wear to the phone to send the message back with the same node.

To get started, I just want to press a button in the Wear app that will generate a toast in my mobile app. I am testing this with the Android Wear emulator and a real Android phone.

My Android mobile app has a separate ListenerService class;

public class ListenerService extends WearableListenerService { String nodeId; @Override public void onMessageReceived(MessageEvent messageEvent) { nodeId = messageEvent.getSourceNodeId(); showToast(messageEvent.getPath()); } private void showToast(String message) { Toast.makeText(this, message, Toast.LENGTH_LONG).show(); } } 

And I initialize this in my manifest:

 <service android:name=".ListenerService" > <intent-filter> <action android:name="com.google.android.gms.wearable.BIND_LISTENER" /> </intent-filter> </service> 

In my Wear MainActivity.java;

  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initApi(); final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub); stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() { @Override public void onLayoutInflated(WatchViewStub stub) { setupWidgets(); } }); } /** * Initializes the GoogleApiClient and gets the Node ID of the connected device. */ private void initApi() { client = getGoogleApiClient(this); retrieveDeviceNode(); } /** * Sets up the button for handling click events. */ private void setupWidgets() { findViewById(R.id.btn_toast).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { sendToast(); } }); } /** * Returns a GoogleApiClient that can access the Wear API. * @param context * @return A GoogleApiClient that can make calls to the Wear API */ private GoogleApiClient getGoogleApiClient(Context context) { return new GoogleApiClient.Builder(context) .addApi(Wearable.API) .build(); } /** * Connects to the GoogleApiClient and retrieves the connected device Node ID. If there are * multiple connected devices, the first Node ID is returned. */ private void retrieveDeviceNode() { new Thread(new Runnable() { @Override public void run() { client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS); NodeApi.GetConnectedNodesResult result = Wearable.NodeApi.getConnectedNodes(client).await(); List<Node> nodes = result.getNodes(); if (nodes.size() > 0) { nodeId = nodes.get(0).getId(); } client.disconnect(); } }).start(); } /** * Sends a message to the connected mobile device, telling it to show a Toast. */ private void sendToast() { if (nodeId != null) { new Thread(new Runnable() { @Override public void run() { client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS); Wearable.MessageApi.sendMessage(client, nodeId, MESSAGE, null); client.disconnect(); } }).start(); } } } 

When debugging, nodeId is always zero, and therefore the run() command never runs in the sendToast() function. However, when I insert a breakpoint in Wearable.NodeApi.getConnectedNodes(client).await() in retrieveDeviceNode() , my application just freezes and does not seem to be able to connect to any node.

Is there something I am missing or something is wrong?

+5
source share

Source: https://habr.com/ru/post/1236371/


All Articles