Android Question Samsung S-Health

Rusty

Well-Known Member
Licensed User
Longtime User
I am trying to use the Samsung S-Health application to monitor activity etc.
I've got the .jar file samsung-digital-health-healthdata-1.2.1.jar in my additional libraries folder.
I also have ported the Java code:
B4X:
#if java
import com.samsung.android.sdk.healthdata.HealthConstants;
import com.samsung.android.sdk.healthdata.HealthDataObserver;
import com.samsung.android.sdk.healthdata.HealthDataResolver;
import com.samsung.android.sdk.healthdata.HealthDataResolver.Filter;
import com.samsung.android.sdk.healthdata.HealthDataResolver.ReadRequest;
import com.samsung.android.sdk.healthdata.HealthDataResolver.ReadResult;
import com.samsung.android.sdk.healthdata.HealthDataStore;
import com.samsung.android.sdk.healthdata.HealthResultHolder;

import android.database.Cursor;
import android.util.Log;

import java.util.Calendar;

public class StepCountReporter {
    private final HealthDataStore mStore;

    public StepCountReporter(HealthDataStore store) {
        mStore = store;
    }

    public void start() {
        // Register an observer to listen changes of step count and get today step count
        HealthDataObserver.addObserver(mStore, HealthConstants.StepCount.HEALTH_DATA_TYPE, mObserver);
        readTodayStepCount();
    }

    // Read the today's step count on demand
    private void readTodayStepCount() {
        HealthDataResolver resolver = new HealthDataResolver(mStore, null);

        // Set time range from start time of today to the current time
        long startTime = getStartTimeOfToday();
        long endTime = System.currentTimeMillis();
        Filter filter = Filter.and(Filter.greaterThanEquals(HealthConstants.StepCount.START_TIME, startTime),
                                   Filter.lessThanEquals(HealthConstants.StepCount.START_TIME, endTime));

        HealthDataResolver.ReadRequest request = new ReadRequest.Builder()
                                                        .setDataType(HealthConstants.StepCount.HEALTH_DATA_TYPE)
                                                        .setProperties(new String[] {HealthConstants.StepCount.COUNT})
                                                        .setFilter(filter)
                                                        .build();

        try {
            resolver.read(request).setResultListener(mListener);
        } catch (Exception e) {
            Log.e(MainActivity.APP_TAG, e.getClass().getName() + " - " + e.getMessage());
            Log.e(MainActivity.APP_TAG, "Getting step count fails.");
        }
    }

    private long getStartTimeOfToday() {
        Calendar today = Calendar.getInstance();

        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);

        return today.getTimeInMillis();
    }

    private final HealthResultHolder.ResultListener<ReadResult> mListener = new HealthResultHolder.ResultListener<ReadResult>() {
        @Override
        public void onResult(ReadResult result) {
            int count = 0;
            Cursor c = null;

            try {
                c = result.getResultCursor();
                if (c != null) {
                    while (c.moveToNext()) {
                        count += c.getInt(c.getColumnIndex(HealthConstants.StepCount.COUNT));
                    }
                }
            } finally {
                if (c != null) {
                    c.close();
                }
            }
            MainActivity.getInstance().drawStepCount(String.valueOf(count));
        }
    };

    private final HealthDataObserver mObserver = new HealthDataObserver(null) {

        // Update the step count when a change event is received
        @Override
        public void onChange(String dataTypeName) {
            Log.d(MainActivity.APP_TAG, "Observer receives a data changed event");
            readTodayStepCount();
        }
    };

}
    #end if
into my new application.
However, when I refresh my libraries, the Samsung jar doesn't show.
Any ideas why it doesn't show? (Also, any advice on making the Java code above work would be GREATLY appreciated!)
Rusty
 

Rusty

Well-Known Member
Licensed User
Longtime User
...sorry Don, I didn't mean to insult you.
I should have looked into the code and have seen that the permissions were not all there, my fault.
Thanks anyway.
Rusty
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i guess the code you posted does not do anything. At least it does NOT SET any permission....
It reads the value of a specific permission...

I guess it must be placed inside manifest like the STEP Data permission

Search the documentation for the value of this permission and add it like the step permission in manifest.
 
Last edited:
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Don,
I've added:
AddApplicationText(<meta-data
android:name="com.samsung.android.health.permission.read"
android:value="com.samsung.health.step_count"/>)
AddApplicationText(<meta-data
android:name="com.samsung.android.health.permission.read"
android:value="com.samsung.health.exercise" />)

The Code is as follows:
B4X:
Try
        pms.Initialize("HealthPermission",hds)
        pms.checkandrequestPermissionAcquired(pms.StepCountPermission)    <-----<<< This line uses PMS which only has reference to StepCountPermissions
                                                                                                                                 I think we need pms to include the request for Exercise
        If pms.getPermissionGranted(pms.StepCountPermission) Then
            Dim f As Filter           
            f.initialize(Midnight, MidnightTonight)
'            Dim x As Long = DateTime.Add(Midnight, 0, 0, -1)
'            Dim y As Long = DateTime.Add(MidnightTonight, 0, 0, -1)
'            f.initialize(x, y)
       
            Dim readreq As ReadRequestBuilder    'set the current filter values
            readreq.Initialize.setResultCount(0, 1000000).setDataType("com.samsung.health.step_count").setFilter(f)
'            readreq.Initialize.setResultCount(0, 1000000).setDataType("com.samsung.health.exercise").setFilter(f)     <-----<< Not sure about this.
            resolv.read(readreq.build)
            resolv.readExerciseData
        Else
'            Msgbox("Please request permission from SHealth", "PERMISSION")       
            n.SetInfo2("SHealth", "Request Permission", "Permission", Main)
            n.Notify(1)
        End If
    Catch
        LogColor("****    SHEALTH connected but has not cleared the white list. Either activate debug or register the app", Colors.Red)
        Log(LastException)
        If isConnected = False Then         'we haven't already told them
            isConnected = True
            ToastMessageShow("When the S Health screen appears:" & CRLF & _
                        "       Turn ON And set Step Count To ON." & CRLF & _
                        "       Then touch DONE.", True)
            n.SetInfo2("SHealth", "Request Permission Whitelist", "WhiteList", Main)
            n.Notify(2)
        End If
    End Try
Please see the <-----<< arrow in the code section

When I add the manifest entry to the working (stepcount) code I get the following error:
SHEALTH is connected. 2017-01-17
**** SHEALTH connected but has not cleared the white list. Either activate debug or register the app
(IllegalArgumentException) java.lang.IllegalArgumentException: com.samsung.health.step_count (READ) is invalid. (e.g. wrong type or not described in AndroidManifest file)

Sorry to be a pest...any ideas/suggestions?
Rusty
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
<-----<<< This line uses PMS which only has reference to StepCountPermissions
I think we need pms
to include the request for Exercise
Good catch ;)

I added a method to get the exercise permission
B4X:
pms.ExerciseDataPermission

Try again with this version and make sure to change your permissionrequest with the new permission
 

Attachments

  • SamsungDigitalHealthV1.22.zip
    21.9 KB · Views: 414
Upvote 0
Top