2016-06-07 134 views
0

我目前正在开发Android穿戴应用程序,但对于可穿戴应用程序来说我很初学,我有一个小疑问,我需要纠正。如何将android应用程序连接到可穿戴应用程序?

所以我有一个屏幕上的按钮在我的移动应用程序,所以如果我按下按钮,然后我想背景颜色应该改变穿。

移动应用

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

Wear应用

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

而且请到虽然屏幕截图,并检查我的项目继承人疼痛的,建议我一些解决方案。

回答

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

使用的数据图发送数据项

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' 
+0

哎磨损它总是要去“API客户端连接失败” – animation123

+0

确保您使用的播放服务的同一版本的移动和磨损。同时检查logcat是否有任何与播放服务版本相关的警告即将到来! – jitinsharma

+0

是的两者都是谷歌播放相同的版本,但它连接到谷歌播放后,它不会进入onDataChanged。通过WearableListenerService可以解决 – animation123

相关问题