如何将Android应用程序连接到可穿戴应用程序?

时间:2016-06-07 09:53:46

标签: android wear-os android-wear-notification

我目前正在研究Android Wearable应用程序,但我对可穿戴应用程序非常初学,我有一个小疑问,我需要纠正。

所以我的移动应用程序上有一个带按钮的屏幕,所以如果我按下按钮,那么我希望背景颜色在佩戴时会改变。

移动应用

mButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       //How to send request to Wear Apps
    }
});

磨损应用

final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            //How to receive request from Android mobile app
        }
    });

enter image description here

请通过屏幕截图查看我的项目继承人并建议我解决方案。

1 个答案:

答案 0 :(得分:2)

活动/片段内的

从移动端,创建连接

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(getContext())
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {
                    Log.d(LOG_TAG, "API client connected");
                }

                @Override
                public void onConnectionSuspended(int i) {
                    Log.d(LOG_TAG, "API client connection suspended");
                }
            })
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult connectionResult) {
                    Log.d(LOG_TAG, "API client connection failed");
                }
            })
            .addApi(Wearable.API)
            .build();
    mGoogleApiClient.connect();

使用DataMap发送数据项

    PutDataMapRequest putDataMapRequest = PutDataMapRequest.create("/app");
    putDataMapRequest.getDataMap().putString("DATA", data); // your data
    PutDataRequest putDataRequest = putDataMapRequest.asPutDataRequest();
    PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi.putDataItem(mGoogleApiClient, putDataRequest);
    pendingResult.setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
        @Override
        public void onResult(DataApi.DataItemResult dataItemResult) {
            if (dataItemResult.getStatus().isSuccess()){
                Log.d(LOG_TAG, "Sent weather data to watch");
            }
            else{
                Log.d(LOG_TAG, "Unable to send data to watch");
            }
            mGoogleApiClient.disconnect();
        }
    });

依赖

compile 'com.google.android.support:wearable:1.1.0'
compile 'com.google.android.gms:play-services-wearable:7.8.0'

从磨损面,接收活动中的数据

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(getBaseContext())
                .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                    @Override
                    public void onConnected(Bundle bundle) {
                        Log.d(LOG_TAG, "API client connected");
                        final DataApi.DataListener dataListener = new DataApi.DataListener() {
                            @Override
                            public void onDataChanged(DataEventBuffer dataEventBuffer) {
                                Log.e(LOG_TAG, "onDataChanged(): " + dataEventBuffer);
                                for (DataEvent event: dataEventBuffer){
                                    if (event.getType() == DataEvent.TYPE_CHANGED){
                                        DataItem dataItem = event.getDataItem();
                                        if (dataItem.getUri().getPath().equals("/app")){
                                            DataMap dataMap = DataMapItem.fromDataItem(dataItem).getDataMap();
                                            String data = dataMap.getString("DATA"); // You have received the data, do your stuff
                                            Log.d(LOG_TAG, "data " + data);
                                        }
                                    }
                                }
                            }
                        };
                        Wearable.DataApi.addListener(mGoogleApiClient, dataListener);
                    }

                    @Override
                    public void onConnectionSuspended(int i) {
                        Log.d(LOG_TAG, "API client connection suspended");
                    }
                })
                .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                        Log.d(LOG_TAG, "API client connection failed");
                    }
                })
                .addApi(Wearable.API)
                .build();
        mGoogleApiClient.connect();

依赖

compile 'com.google.android.support:wearable:1.3.0'
compile 'com.google.android.gms:play-services-wearable:7.8.0'