Get Heart Rate from Android Wear using Google fit

断了今生、忘了曾经 提交于 2019-12-08 03:28:28

问题


I'm developing an app for Android (only for smartphone, not for wear) and I want to get the heart rate using googleApiClient (Google Fit) from smartwatch (Huawei Watch f.e.). I tried using Sensor API but wear is not listed as DataSource in Google Fit API.

REQUEST_INTERVAL = 5;  
Fitness.SensorsApi.findDataSources(client, new DataSourcesRequest.Builder()
                .setDataTypes(DataType.TYPE_LOCATION_SAMPLE,
                        DataType.TYPE_STEP_COUNT_CUMULATIVE,
                        DataType.TYPE_DISTANCE_DELTA,
                        DataType.TYPE_HEART_RATE_BPM)
                .setDataSourceTypes(DataSource.TYPE_RAW, DataSource.TYPE_DERIVED)
                .build())
                .setResultCallback(new ResultCallback<DataSourcesResult>() {
                    @Override
                    public void onResult(DataSourcesResult dataSourcesResult) {

                        for (DataSource dataSource : dataSourcesResult.getDataSources()) {
                            // There isn't heart rate source here
                            final DataType dataType = dataSource.getDataType();
                            Fitness.SensorsApi.add(client,
                                    new SensorRequest.Builder()
                                            .setDataSource(dataSource)
                                            .setDataType(dataType)
                                            .setSamplingRate(REQUEST_INTERVAL, TimeUnit.SECONDS)
                                            .build(),
                                    listener)
                                    .setResultCallback(new ResultCallback<Status>() {
                                        @Override
                                        public void onResult(Status status) {
                                            ...
                                        }
                                    });
                            }
                        }
                    }
                });

Othrer data(like steps, location and distance) are came. My client:

googleApiClient = new GoogleApiClient.Builder(activity)
                .addApi(Plus.API)
                .addApi(Fitness.RECORDING_API)
                .addApi(Fitness.HISTORY_API)
                .addApi(Fitness.SENSORS_API)
                .addApi(LocationServices.API)
                .addApi(Fitness.BLE_API)
                .addScope(Plus.SCOPE_PLUS_LOGIN)
                .addScope(Plus.SCOPE_PLUS_PROFILE)
                .addScope(new Scope(Scopes.PROFILE))
                .addScope(new Scope(Scopes.PLUS_LOGIN))
                .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
                .addScope(new Scope(Scopes.FITNESS_BODY_READ))
                .addScope(new Scope(Scopes.FITNESS_LOCATION_READ))
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

I spent some hours reading docs and answers but can't find a solution. Maybe I do something wrong. Data from smartphone came, but from wear it doesn't. Google fit is installed on wear.

Thanks for your answer!

来源:https://stackoverflow.com/questions/40177237/get-heart-rate-from-android-wear-using-google-fit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!