android app 与android wear 通过MessageApi 通讯

类容:手持端app 点击按钮,手表端显示点击次数

1. 设备间通过GoogleApiClient建立连接,若需监听连接是否成功,需

继承GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,

此处采用MessageApi发送,继承DataApi.DataListener,MessageApi.MessageListener监听。

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
2. 在Activity 中启动连接

    @Override
    protected void onStart() {
        //start connect the node you want
        mGoogleApiClient.connect();
        super.onStart();
    }

3.获取连接的设备

    /**
     * Get Nodes that connected with phone,such as your android watch
     * this method can't be used in UI thread
     * @return nodes
     */
    private List getNodes() {
        ArrayList results= new ArrayList();
        //get all connected nodes
        NodeApi.GetConnectedNodesResult nodes =
                Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
        for (Node node : nodes.getNodes()) {
            results.add(node);
        }
        return results;
    }
4. 若连接建立成功,会调用onConnected方法

    @Override
    public void onConnected(Bundle bundle) {
        Log.d(TAG, "onConnected:" + bundle);
        //register client listener
        Wearable.DataApi.addListener(mGoogleApiClient, this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                mConnectedNodes = getNodes();
                if (mConnectedNodes.size() > 0) {
                    for (Node node : mConnectedNodes) {
                        Log.d(TAG, "Connected Node->ID:" +node.getId()+" Name:"+node.getDisplayName());
                    }
                } else {
                    Log.d(TAG, "No Connected Nodes");
                }
            }
        }).start();
    }

5.发送端发送消息

    @Override
    public void onClick(View v) {
        mClickTimes++;
        mButton.setText(String.valueOf(mClickTimes));

        if ((null != mConnectedNodes)&&(mConnectedNodes.size()>0)) {
            //the message send to wear
            byte[] sendDatas = ("Alvin:"+mClickTimes).getBytes();
            //deliver message
            Wearable.MessageApi.sendMessage(mGoogleApiClient,mConnectedNodes.get(0).getId(),WEARABLE_CLICK_PATH,sendDatas)
                    .setResultCallback(new ResultCallback() {
                        @Override
                        public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                            if (!sendMessageResult.getStatus().isSuccess()) {
                                Log.d(TAG, "sendMessage Fail");
                            } else {
                                Log.d(TAG, "sendMessage Success");
                            }

                        }
                    });
        }
    }
6. 接收端注册接收事件,否则接收不到消息

    @Override
    public void onConnected(Bundle bundle) {
        Log.d(TAG, "onConnected:" + bundle);
        Wearable.DataApi.addListener(mGoogleApiClient, this);
        //don't forget to register message listener
        Wearable.MessageApi.addListener(mGoogleApiClient, this);
        mTextView.setText("Connect Success");
    }
7. 处理接受的数据

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        Log.d(TAG, "MessageEvent");
        if (messageEvent.getPath().equals(WEARABLE_CLICK_PATH)) {
            String receiver = new String(messageEvent.getData());
            mTextView.setText(receiver);
        }
    }


注:接收端和发送端在连接和事件处理上方法是一样的

完整代码,请在此处下载:  点击打开链接

具体用法说明参见下述网站:

1. https://developers.google.com/android/

2. http://developer.android.com/intl/zh-cn/wear/index.html

你可能感兴趣的:(Android,Android,Wear,android,android,wear,Android,Studio,MessageApi,GoogleApiClient)