无法从Android Wear向主机设备发送消息

Awe*_*uce 5 java android android-studio android-wear-data-api wear-os

我正在编写一个简单的Android应用程序,我需要使用MessageApi从Wear设备发送消息到我的手机.最终,我希望能够从我的衣服发送消息到手机,然后再用同一节点发回消息.

首先,我只想点击Wear应用程序上的一个按钮,该按钮将在我的移动应用程序中生成一个Toast.我正在使用Android Wear模拟器和实际的Android手机对此进行测试.

我的Android移动应用程序有一个单独的ListenerService类;

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

而且我在我的清单中将这种情况搞砸了:

<service
  android:name=".ListenerService" >
    <intent-filter>
       <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
    </intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)

在我的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's 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

调试时,nodeId始终为null,因此run()命令永远不会在sendToast()函数内运行.然而,当我插入一个断点Wearable.NodeApi.getConnectedNodes(client).await()retrieveDeviceNode(),我的应用程序只是冻结,并似乎不能够连接到任何节点.

有什么我错过或做错了吗?