我正在使用应用程序从手表中获取传感器数据。我开始使用手机进行收购,但有些情况下,手表和手机似乎完全连接,但是当我发送“开始采集”时。命令没有任何反应必须重新启动手表才能开始接受命令。
这就是我通过手机建立连接的方式:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(MainActivity.this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
这就是我从手机发送数据的方式:
if (mGoogleApiClient != null && mGoogleApiClient.isConnected() ) {
final PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/toWear");
putDataMapReq.getDataMap().putInt("duration", duration); // Duration of data, selected in TimeSelector
putDataMapReq.getDataMap().putString("type", type);
putDataMapReq.getDataMap().putString("name", name);
putDataMapReq.getDataMap().putString("Starting Time", startingTime); // Starting Time of data
putDataMapReq.getDataMap().putBoolean("Stop", false); //Stop is set True when Phone has to stop watch from taking data
putDataMapReq.setUrgent();
PutDataRequest request = putDataMapReq.asPutDataRequest();
Wearable.DataApi.putDataItem(mGoogleApiClient, request).setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
@Override
public void onResult(DataApi.DataItemResult dataItemResult) {
}
});
}
else
{
Toast.makeText(context, "Not Connected properly", Toast.LENGTH_LONG).show();
}
这就是我在智能手表中接受数据的方式:
public void onDataChanged(DataEventBuffer dataEvents) {
count=0;
broadcaster = LocalBroadcastManager.getInstance(this);
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
// DataItem changed
DataItem item = event.getDataItem();
if (item.getUri().getPath().compareTo("/toWear") == 0) {
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
int duration = dataMap.getInt("duration");
stop=dataMap.getBoolean("Stop");
String type = dataMap.getString("type");
String name = dataMap.getString("name");
String startingTime= dataMap.getString("Starting Time");
// updateCount(dataMap.getInt(COUNT_KEY));
Bundle b= new Bundle();
b.putInt("duration",duration);
b.putString("Starting Time",startingTime);
if(!stop) {
AcquisitionStatus=true;
setAcquisitionStatus(true);
Intent sensorServ = new Intent(MyService.this, SensorService.class);
sensorServ.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
sensorServ.putExtras(b);
startService(sensorServ);
}
else {
AcquisitionStatus = false;
setAcquisitionStatus(false);
}
}
} else if (event.getType() == DataEvent.TYPE_DELETED) {
// DataItem deleted
}
}
}
我想知道,为什么有时候,尽管建立了正确的连接,但智能手表无法从手机接收数据。
答案 0 :(得分:0)
Android Wear Data API可确保100%的数据传输。如果两个数据相同,则数据不会被发送,因为它不会更新屏幕上的任何内容。检查数据是否相同。