我尝试使用google Fit API从sony smartband2获得心灵感受。问题是,我没有得到任何读数(即没有调用onDataPoint方法)。正确找到心率传感器,如果我试图寻找步骤数据,整个代码也能正常工作。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
authInProgress = savedInstanceState.getBoolean(AUTH_PENDING);
}
mApiClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.SENSORS_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
.addScope(new Scope(Scopes.FITNESS_BODY_READ))
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
protected void onStart() {
super.onStart();
mApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
final DataSourcesRequest dataSourceRequest = new DataSourcesRequest.Builder()
.setDataTypes(DataType.TYPE_HEART_RATE_BPM)
.setDataSourceTypes(DataSource.TYPE_RAW)
.build();
Fitness.SensorsApi.findDataSources(mApiClient, dataSourceRequest)
.setResultCallback(new ResultCallback<DataSourcesResult>() {
@Override
public void onResult(DataSourcesResult dataSourcesResult) {
for(DataSource dataSource : dataSourcesResult.getDataSources())
{
Log.i(TAG, "Data source found: " + dataSource.toString());
Log.i(TAG, "Data Source type: " + dataSource.getDataType().getName());
registerFitnessDataListener(dataSource, DataType.TYPE_HEART_RATE_BPM);
}
}
});
// dataSourceRequest = new DataSourcesRequest.Builder()
// .setDataTypes(DataType.TYPE_STEP_COUNT_CUMULATIVE)
// .setDataSourceTypes(DataSource.TYPE_RAW)
// .build();
// Fitness.SensorsApi.findDataSources(mApiClient, dataSourceRequest)
// .setResultCallback(dataSourcesResultCallback);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (!authInProgress) {
try {
authInProgress = true;
connectionResult.startResolutionForResult(MainActivity.this, REQUEST_OAUTH);
} catch (IntentSender.SendIntentException e) {
}
} else {
Log.e("GoogleFit", "authInProgress");
}
}
@Override
public void onDataPoint(DataPoint dataPoint) {
for (final Field field : dataPoint.getDataType().getFields()) {
final Value value = dataPoint.getValue(field);
Log.i("DATASOURCE", field.getName());
Log.i("DATASOURCE", value.toString());
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView tv = (TextView) findViewById(R.id.tv1);
tv.setText("Field: " + field.getName() + " Value: " + value);
Toast.makeText(getApplicationContext(), "Field: " + field.getName() + " Value: " + value, Toast.LENGTH_SHORT).show();
}
});
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_OAUTH) {
authInProgress = false;
if (resultCode == RESULT_OK) {
if (!mApiClient.isConnecting() && !mApiClient.isConnected()) {
mApiClient.connect();
} else {
TextView tv = (TextView) findViewById(R.id.tv1);
tv.setText("connected");
}
} else if (resultCode == RESULT_CANCELED) {
Log.e("GoogleFit", "Result_Canceled");
}
} else {
Log.e("GoogleFit", "Request not OAUTH");
}
}
private void registerFitnessDataListener(final DataSource dataSource, DataType dataType) {
SensorRequest request = new SensorRequest.Builder()
.setDataSource(dataSource)
.setDataType(dataType)
.setSamplingRate(2, TimeUnit.SECONDS)
.build();
Fitness.SensorsApi.add(mApiClient, request, this)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (!status.isSuccess()) {
Log.e("DATASOURCES", "register " + dataSource.getName() + " failed");
} else {
Log.i("DATASOURCES", "register " + dataSource.getName() + " succeed");
}
}
});
}
}
您有什么想法我无法获取数据吗?
答案 0 :(得分:2)
好的,在我发现此代码正在处理某人手机后,我发现您需要启用SmartBand2应用程序选项才能与Google Fit共享数据。出于某种原因,smartband 2无法独立于其应用程序连接到Google Fit。