sonysony-smartwatchsony-lifelog-api

Retrieving Heart Rate Data from Sony Band SWR12


I am working on retrieving heart rate data from the Sony Band 2 (SWR12). I would like to do this using the Google Fit Sensors API. I followed the sample and tutorials by google here:https://github.com/googlesamples/android-fit/blob/master/BasicSensorsApi/app/src/main/AndroidManifest.xml

Having enabled the Fit API and created my Authentication token I can now read te device location.

However, when I change the data source to: .setDataTypes(DataType.AGGREGATE_HEART_RATE_SUMMARY) the app crashes.

Anyone that has previously worked with the Fit API and the Sony Band, please help me connect to the band.


Solution

  • I managed to record and read the grouped data with the following code:

    private void suscribeDataFitness(){
        Fitness.RecordingApi.subscribe(mApiClient, DataType.AGGREGATE_HEART_RATE_SUMMARY)
                .setResultCallback(mSubscribeResultCallback);
    
    }
    
    
    private void readDataFitnessHistory()
    {
        // Setting a start and end date using a range of 1 week before this moment.
        Calendar cal = Calendar.getInstance();
        Date now = new Date();
        cal.setTime(now);
        long endTime = cal.getTimeInMillis();
    
        cal.add(Calendar.DAY_OF_YEAR, -1);
        long startTime = cal.getTimeInMillis();
    
        java.text.DateFormat dateFormat = getDateInstance();
        Log.d(TAG, "Range Start: " + dateFormat.format(startTime) + " Millis : " + startTime);
        Log.d(TAG, "Range End: " + dateFormat.format(endTime)  + " Millis : " + endTime);
    
       final DataReadRequest readRequest = new DataReadRequest.Builder()
                //.read(DataType.TYPE_STEP_COUNT_DELTA)
                //.read(DataType.TYPE_HEART_RATE_BPM)
                //.read(ESTIMATED_STEP_DELTAS)
                .aggregate(DataType.AGGREGATE_HEART_RATE_SUMMARY)
                .enableServerQueries()
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .bucketByTime(1, TimeUnit.HOURS)
                .build();
    
    
        // Invoke the History API to fetch the data with the query and await the result of
        // the read request.
        DataReadResult dataReadResult =
                Fitness.HistoryApi.readData(mApiClient, readRequest).await(1, TimeUnit.MINUTES);
        showDataBuckets(dataReadResult);
    
    }
    
    final DataReadRequest readRequest = new DataReadRequest.Builder()
                //.read(DataType.TYPE_STEP_COUNT_DELTA)
                //.read(DataType.TYPE_HEART_RATE_BPM)
                //.read(ESTIMATED_STEP_DELTAS)
                .aggregate(DataType.AGGREGATE_HEART_RATE_SUMMARY)
                .enableServerQueries()
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .bucketByTime(1, TimeUnit.HOURS)
                .build();
    
    
    private void showDataBuckets(DataReadResult dataReadResult) {
        //Used for aggregated data
        if (dataReadResult.getBuckets().size() > 0) {
            Log.d(TAG,"History: "+ "Number of buckets: " + dataReadResult.getBuckets().size());
            for (Bucket bucket : dataReadResult.getBuckets()) {
                List<DataSet> dataSets = bucket.getDataSets();
                for (DataSet dataSet : dataSets) {
                    showDataSet(dataSet);
                }
            }
        }
    }
    

    I hope I can help you. A greeting