问题
I'm trying to use google Fit API to get heartrate from sony smartband2. Problem is, I don't get any readings(i.e. onDataPoint method is not called). Heart Rate sensor is being found properly, whole code also is working properly if I try to look for step data.
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");
}
}
});
}
}
Do You have any ideas why i can't get the data?
回答1:
Ok, After I found that this code was working on someone's phone, I found out that You need to enable SmartBand2 application option to share data to Google Fit. For some reason smartband 2 can't connect to Google Fit independently from it's application.
来源:https://stackoverflow.com/questions/36308062/cant-get-heart-rate-data-from-google-fit-api-with-smartband-2