How to access Android Heart Rate Sensor RAW DATA? (reflected light, not the heart beat)

后端 未结 2 829
温柔的废话
温柔的废话 2021-02-05 17:18

The android sdk on heart rate sensor only returns the calculated bpm which I have no interest.

I need to access to android heart rate sensor RAW data, e.g. in terms of

2条回答
  •  一整个雨季
    2021-02-05 17:57

    You can use Google Fit's Sensor API to get the raw heartbeat data, if Google Fit is an option. See Google Fit Guide for details.

    private void trackHeartRate() {
        SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder()
                .setDataTypes(DataType.TYPE_HEART_RATE_BPM)
                // Can specify whether data type is raw or derived.
                .setDataSourceTypes(DataSource.TYPE_RAW)
                .build())
                .setResultCallback(new ResultCallback() {
                    @Override
                    public void onResult(DataSourcesResult dataSourcesResult) {
                        Log.i(TAG, "Result: " + dataSourcesResult.getStatus().toString());
                        for (DataSource dataSource : dataSourcesResult.getDataSources()) {
                            Log.i(TAG, "Data source found: " + dataSource.toString());
                            Log.i(TAG, "Data Source type: " + dataSource.getDataType().getName());
    
                            if (dataSource.getDataType().equals(DataType.TYPE_HEART_RATE_BPM)
                                    && mListener == null) {
                                Log.i(TAG, "Data source for heart rate found!  Registering.");
                                registerFitnessDataListener(dataSource,
                                        DataType.TYPE_HEART_RATE_BPM);
                            }
                        }
                    }
                });
        mListener = new OnDataPointListener() {
            @Override
            public void onDataPoint(DataPoint dataPoint) {
                for (Field field : dataPoint.getDataType().getFields()) {
                    Value val = dataPoint.getValue(field);
                    Log.i(TAG, "Detected DataPoint field: " + field.getName());
                    Log.i(TAG, "Detected DataPoint value: " + val);
                }
            }
        };
    }
    
    private void registerFitnessDataListener(DataSource dataSource, DataType dataType) {
        Fitness.SensorsApi.add(
                mClient,
                new SensorRequest.Builder()
                        .setDataSource(dataSource) // Optional but recommended for custom data sets.
                        .setDataType(dataType) // Can't be omitted.
                        .setSamplingRate(10, TimeUnit.SECONDS)
                        .build(),
                mListener)
                .setResultCallback(new ResultCallback() {
                    @Override
                    public void onResult(Status status) {
                        if (status.isSuccess()) {
                            Log.i(TAG, "Listener registered!");
                        } else {
                            Log.i(TAG, "Listener not registered.");
                        }
                    }
                });
    }
    

    Hope this helps.

提交回复
热议问题