无法从 google fit API with smartband 2 获取心率数据

Can't get heart rate data from google fit API with smartband 2

我正在尝试使用 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");
                    }
                }
            });
}
}

你知道为什么我无法获取数据吗?

好的,在我发现此代码在某人的 phone 上运行后,我发现您需要启用 SmartBand2 应用程序选项才能将数据共享到 Google Fit。出于某种原因,智能手环 2 无法独立于其应用程序连接到 Google Fit。