【问题标题】:How to connect android apps to wearable apps?如何将安卓应用连接到可穿戴应用?
【发布时间】:2016-06-07 09:53:46
【问题描述】:

我目前正在开发 Android 可穿戴应用程序,但我是可穿戴应用程序的初学者,我有一个小疑问需要纠正。

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

移动应用

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
        }
    });

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

【问题讨论】:

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


    【解决方案1】:

    从移动端在您的活动/片段中,创建连接

    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'
    

    从 Wear 端,接收活动中的数据

    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'
    

    【讨论】:

    • 嘿,戴上它总是会出现“API客户端连接失败”
    • 确保您在移动设备和 Wear 上使用相同版本的游戏服务。如果有任何与播放服务版本相关的警告,还要检查 logcat!
    • 是的,两者都是同一版本的 google play,但在连接到 Google play 后,它不会进入 onDataChanged。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多