我为Android Wear开发了一些表盘(minSdkVersion = 21和targetSdkVersion = 23)并整合了Google Fit API for Android。
我使用Fitness.SensorsApi.add从华为手表和Fitness.RecordingApi.subscribe读取心率传感器数据,以将传感器数据同步到Google Fit。 它的工作原理:更新后的数据在Google健身应用和Google健身网页(https://fit.google.com/)中正确显示。
当我尝试在同一块手表上使用Fitness.HistoryApi.readData读取数据时,它完美无缺。 这是logcat输出:
Range Start: Mar 7, 2017
Range End: Mar 8, 2017
Number of returned DataSets is: 1
Data returned for Data type: com.google.heart_rate.bpm
Data point:
Type: com.google.heart_rate.bpm
Start: 08.03 - 02:01:38
End: 08.03 - 02:01:38
Field: bpm Value: 82.0
Field: bpm
Field: 2
Field: com.google.heart_rate.bpm
mLastHeartBPM: 82
然而,当我尝试从另一只手表访问相同的心率数据时,一台Sony Smartwatch 3运行与华为手表(Android Wear 1.5 - Google Play Services 10.2.98)相同的软件,它会不断阅读Google的旧数据适合。 这是logcat输出:
Range Start: Mar 7, 2017
Range End: Mar 8, 2017
Number of returned DataSets is: 1
Data returned for Data type: com.google.heart_rate.bpm
Data point:
Type: com.google.heart_rate.bpm
Start: 07.03 - 04:31:13
End: 07.03 - 04:31:13
Field: bpm Value: 70.0
Field: bpm
Field: 2
Field: com.google.heart_rate.bpm
mLastHeartBPM: 70
正如您所看到的,同样的startTime和endTime,但结果完全不同。 返回的数据是Google健身中存在的实际值,但它是旧的。
这是我用来访问Google健身数据的功能
private void getHeartBpm() {
if(LOG_ENABLED) Log.d(TAG, "getHeartBpm()");
if (ActivityCompat.checkSelfPermission(VeertualiaFit.this, Manifest.permission.BODY_SENSORS) != PackageManager.PERMISSION_GRANTED)
return;
if ((mGoogleApiClient != null)
&& (mGoogleApiClient.isConnected())) {
mHeartBpmRequested = true;
if(LOG_ENABLED) Log.d(TAG, "=============== getHeartBpm(): QUERYING =========================");
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.DATE, -1);
long startTime = cal.getTimeInMillis();
java.text.DateFormat dateFormat = getDateInstance();
if(LOG_ENABLED) Log.d(TAG, "---------------------------------");
if(LOG_ENABLED) Log.d(TAG, "Range Start: " + dateFormat.format(startTime));
if(LOG_ENABLED) Log.d(TAG, "Range End: " + dateFormat.format(endTime));
if(LOG_ENABLED) Log.d(TAG, "---------------------------------");
DataReadRequest readRequest = new DataReadRequest.Builder()
.read(DataType.TYPE_HEART_RATE_BPM)
.enableServerQueries()
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.setLimit(1)
.build();
Fitness.HistoryApi.readData(mGoogleApiClient, readRequest).setResultCallback(new ResultCallback<DataReadResult>() {
@Override
public void onResult(DataReadResult dataReadResult) {
if (dataReadResult.getBuckets().size() > 0) {
if(LOG_ENABLED) Log.d(TAG, "---------------------------------");
if(LOG_ENABLED) Log.d(TAG, "Number of returned buckets of DataSets is: "
+ dataReadResult.getBuckets().size());
for (Bucket bucket : dataReadResult.getBuckets()) {
List<DataSet> dataSets = bucket.getDataSets();
for (DataSet dataSet : dataSets) {
if(LOG_ENABLED) Log.d(TAG, "Data returned for Data type: " + dataSet.getDataType().getName());
java.text.DateFormat dateFormat = getTimeInstance();
for (DataPoint dataPoint : dataSet.getDataPoints()) {
if(LOG_ENABLED) Log.d(TAG, "Data point:");
if(LOG_ENABLED) Log.d(TAG, "\tType: " + dataPoint.getDataType().getName());
if(LOG_ENABLED) Log.d(TAG, "\tStart: " + dateFormat.format(dataPoint.getStartTime(TimeUnit.MILLISECONDS)));
if(LOG_ENABLED) Log.d(TAG, "\tEnd: " + dateFormat.format(dataPoint.getEndTime(TimeUnit.MILLISECONDS)));
for(Field field : dataPoint.getDataType().getFields()) {
if(LOG_ENABLED) Log.d(TAG, "\tField: " + field.getName() +
" Value: " + dataPoint.getValue(field));
if(LOG_ENABLED) Log.d(TAG, "Field: " + field.getName());
if(LOG_ENABLED) Log.d(TAG, "Field: " + field.getFormat());
if(LOG_ENABLED) Log.d(TAG, "Field: " + dataPoint.getDataType().getName());
mLastHeartBPM = (int)dataPoint.getValue(field).asFloat();
if(LOG_ENABLED) Log.d(TAG, "\tmLastHeartBPM: " + mLastHeartBPM);
if(LOG_ENABLED) Log.d(TAG, "---------------------------------");
}
}
}
}
} else if (dataReadResult.getDataSets().size() > 0) {
if(LOG_ENABLED) Log.d(TAG, "---------------------------------");
if(LOG_ENABLED) Log.d(TAG, "Number of returned DataSets is: "
+ dataReadResult.getDataSets().size());
for (DataSet dataSet : dataReadResult.getDataSets()) {
if(LOG_ENABLED) Log.d(TAG, "Data returned for Data type: " + dataSet.getDataType().getName());
java.text.DateFormat dateFormat = new SimpleDateFormat("dd.MM - HH:mm:ss");
for (DataPoint dataPoint : dataSet.getDataPoints()) {
if(LOG_ENABLED) Log.d(TAG, "Data point:");
if(LOG_ENABLED) Log.d(TAG, "\tType: " + dataPoint.getDataType().getName());
if(LOG_ENABLED) Log.d(TAG, "\tStart: " + dateFormat.format(dataPoint.getStartTime(TimeUnit.MILLISECONDS)));
if(LOG_ENABLED) Log.d(TAG, "\tEnd: " + dateFormat.format(dataPoint.getEndTime(TimeUnit.MILLISECONDS)));
for(Field field : dataPoint.getDataType().getFields()) {
if(LOG_ENABLED) Log.d(TAG, "\tField: " + field.getName() +
" Value: " + dataPoint.getValue(field));
if(LOG_ENABLED) Log.d(TAG, "Field: " + field.getName());
if(LOG_ENABLED) Log.d(TAG, "Field: " + field.getFormat());
if(LOG_ENABLED) Log.d(TAG, "Field: " + dataPoint.getDataType().getName());
mLastHeartBPM = (int)dataPoint.getValue(field).asFloat();
if(LOG_ENABLED) Log.d(TAG, "\tmLastHeartBPM: " + mLastHeartBPM);
if(LOG_ENABLED) Log.d(TAG, "---------------------------------");
}
}
}
}
}
});
if(LOG_ENABLED) Log.d(TAG, "=================================================================");
}
}
我找不到解释。 代码似乎是正确的,它基于Google样本。
有人可以给我一个线索吗?
更新 这些是调试器中的DataReadResult值:
SONY
DataReadResult{status=Status{statusCode=SUCCESS, resolution=null}, dataSets=[DataSet{d:heart_rate.bpm:gms:default_heart_rate_bpm []}], buckets=[]}
HUAWEI
DataReadResult{status=Status{statusCode=SUCCESS, resolution=null}, dataSets=[DataSet{d:heart_rate.bpm:gms:default_heart_rate_bpm [RawDataPoint{[70.0]@[1488981101916291248, 1488981101916291248](0,1)}]}], buckets=[]}
DataPoint{[70.0]@[1488981101916291248, 1488981101916291248,raw=0,insert=0](d:heart_rate.bpm:gms:default_heart_rate_bpm r:heart_rate.bpm:HUAWEI WATCH:c528846c:ADPD153GGRI)}
Sony SmartWatch 3输入代码的else if (dataReadResult.getDataSets().size() > 0)
部分,但Datapoint为空。