For my masters thesis I’m currently trying to develop a wearable App with Android Studio for my Samsung Galaxy Watch 4 Classic. With this App I want to simply display a few Vitalparameters like heartrate, stressvalue, breathe frequency etc. (the more, the better)
Therefore I’ve already tried several ways to get access to the heartrate at first.
With
<uses-permission android:name="com.samsung.android.health.permission.read" />
<uses-permission android:name="android.permission.BODY_SENSORS" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
in my manifest, I initialized the permission for it.
package com.example.test;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import com.example.test.databinding.ActivityVitalsBinding;
import com.samsung.android.sdk.healthdata.HealthDataStore;
import com.samsung.android.sdk.healthdata.HealthConstants;
import com.samsung.android.sdk.healthdata.HealthDataResolver;
import com.samsung.android.sdk.healthdata.HealthDataResolver.Filter;
import com.samsung.android.sdk.healthdata.HealthPermissionManager;
import com.samsung.android.sdk.healthdata.HealthResultHolder;
import com.samsung.android.sdk.healthdata.HealthResultHolder.ResultListener;
import java.util.Arrays;
import java.util.HashSet;
public class VitalsActivity extends Activity {
private HealthDataStore healthDataStore;
private ActivityVitalsBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityVitalsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
healthDataStore = new HealthDataStore(this, new HealthDataStore.ConnectionListener() {
@Override
public void onConnected() {
System.out.println("Connected successfully");
}
@Override
public void onConnectionFailed(HealthConnectionErrorResult healthConnectionErrorResult) {
System.out.println("couldn't connect");
}
@Override
public void onDisconnected() {
}
});
healthDataStore.connectService();
}
private void requestHeartRatePermission() {
HealthPermissionManager.PermissionKey key =
new HealthPermissionManager.PermissionKey(HealthConstants.HeartRate.HEALTH_DATA_TYPE, HealthPermissionManager.PermissionType.READ);
HealthPermissionManager permissionManager = new HealthPermissionManager(healthDataStore);
permissionManager.requestPermissions(new HashSet<HealthPermissionManager.PermissionKey>(Arrays.asList(key)))
.setResultListener(new ResultListener<HealthPermissionManager.PermissionResult>() {
@Override
public void onResult(HealthPermissionManager.PermissionResult result) {
if (result.equals(true)) {
readHeartRateData();
} else {
System.out.println("access denied");
}
}
});
}
private void readHeartRateData() {
HealthDataResolver resolver = new HealthDataResolver(healthDataStore, null);
HealthDataResolver.ReadRequest request = new HealthDataResolver.ReadRequest.Builder()
.setDataType(HealthConstants.HeartRate.HEALTH_DATA_TYPE)
.build();
resolver.read(request)
.setResultListener(result -> {
try (Cursor cursor = result.getResultCursor()) {
if (cursor != null && cursor.moveToFirst()) {
int heartRateIndex = cursor.getColumnIndex(HealthConstants.HeartRate.HEART_RATE);
if (heartRateIndex != -1) {
int heartRate = cursor.getInt(heartRateIndex);
System.out.println("current heartrate: " + heartRate);
} else {
System.out.println("couldn't find heartrate-column");
}
}
} catch (Exception e) {
System.out.println("error while reading heartrate-data");
}
});
}
and that’s how I solved it. But it isn’t working at all.
I get the error:
E/HealthDataStore: disconnectService: Context instance is invalid
D/HealthDataStore: Trying to connect with Health Service fails (error code: 2)
D/HealthDataStore: Check SupportedDevice
D/HealthSDK-DeviceUtil: Server URL : https://hub.samsungapps.com/product/appCheck.as?appInfo=com.sec.android.app.shealth@0&deviceId=sdk_gwear_x86&mnc=260&csc=FAIL&openApi=30&mcc=310
D/HealthSDK-DeviceUtil: Downloading Samsung Health is unavailable (0)
so I’m currently looking for another way to get easy access to the needed values.
Can you help me out here?
If possible:
Since the App shall be useable without Smartphones, the solution also should be independent from any Smartphone. But if this is not possible, I’m also thankful for any other solution :).
Thanks in advance!