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
Hi Don,
Your library is working great!
Can you re-compile it with the logging disabled? I've tried to import it into Android Studio to remove those, but can't seem to get the project setup correctly.
The service I created polls the Samsung Health periodically to count steps and use the information at near real-time and the logs fill the B4a logging panel so that it is pretty much unusable when the service is running:
CursorSize=0
** Service (svcshealth) Start **
lib:Raising.. health_onconnected()
SHEALTH is connected. 2016/12/13
lib:Raising.. resolver_onreadresult()
CursorSize=0
** Service (svcshealth) Start **
lib:Raising.. health_onconnected()
SHEALTH is connected. 2016/12/13
lib:Raising.. resolver_onreadresult()
CursorSize=0
** Service (svcshealth) Start **
lib:Raising.. health_onconnected()
SHEALTH is connected. 2016/12/13
lib:Raising.. resolver_onreadresult()
CursorSize=0
** Service (svcshealth) Start **
lib:Raising.. health_onconnected()
SHEALTH is connected. 2016/12/13
lib:Raising.. resolver_onreadresult()
CursorSize=0
** Service (svcshealth) Start **
lib:Raising.. health_onconnected()
SHEALTH is connected. 2016/12/13
lib:Raising.. resolver_onreadresult()
CursorSize=0
** Service (svcshealth) Start **
lib:Raising.. health_onconnected()
SHEALTH is connected. 2016/12/13
lib:Raising.. resolver_onreadresult()
CursorSize=0
Thanks,
Rusty
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
See Attached new compilation
 

Attachments

  • SamsungDigitalHealthV1.11.zip
    194.4 KB · Views: 209
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Hi Don,
I've downloaded Eclipse for Android and installed the latest Java and have tried to update/re-create the fabulous library you created but am finding no happiness...
I need to incorporate the acquisition of the HealthConstants.Exercise.HEALTH_DATA_TYPE.
According to the Shealth website, the below is needed. WebURL

Request
B4X:
public void readExerciseData() {
    HealthDataResolver resolver = new HealthDataResolver(mStore, null);

    // Set time range for last 24 hours
    long endTime = System.currentTimeMillis();
    long startTime = endTime - 24*60*60*1000;

    Filter filter = Filter.and(Filter.greaterThanEquals(HealthConstants.Exercise.START_TIME, startTime),
    Filter.lessThanEquals(HealthConstants.Exercise.END_TIME, endTime));

    HealthDataResolver.ReadRequest request = new ReadRequest.Builder()
        .setDataType(HealthConstants.Exercise.HEALTH_DATA_TYPE)
        .setProperties(new String[]{HealthConstants.Exercise.LIVE_DATA, HealthConstants.Exercise.END_TIME})
        .setFilter(filter)
        .build();

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

Retrieve
B4X:
private final HealthResultHolder.ResultListener<ReadResult> mListener = new HealthResultHolder.ResultListener<ReadResult>() {
    @Override
    public void onResult(ReadResult result) {
        Cursor c = null;
        try {
            c = result.getResultCursor();
            if (c != null) {
                while (c.moveToNext()) {
                    byte[] data = c.getBlob(c.getColumnIndex(HealthConstants.Exercise.LIVE_DATA));
                    if (data != null) {
                        // If data is not null, it means there is LIVE_DATA
                        // Refer an example in API Reference’ HealthConstants.Exercise interface
                        // and decompress live data
                    } else {
                        Log.e(MainActivity.APP_TAG, "there is no live data.");
                    }
                }
            }
        } catch(Exception e) {
            Log.e(MainActivity.APP_TAG, e.getClass().getName() + " - " + e.getMessage());
        } finally {
            if (c != null) {
                c.close();
        }
    }
}};

Can you incorporate this into the lib or instruct me on what I'm doing wrong in trying to compile your source with the additions?
Thanks in advance,
Rusty
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
or instruct me on what I'm doing wrong in trying to compile your source with the additions?
i dont see a error you got.

I need to go to work soon. So for now i only can provide my Eclipse-Project-Folder

1. Extract
2. Start eclipse and use IMPORT
import10125.png



import10126.png


import10127.png

check the Buildpaths if all files are found....

import10128.png


import10129.png
 

Attachments

  • SamsungDigitalHealth.zip
    238.6 KB · Views: 233
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Don,
I'm not seeing these menus.
Is the version of the Eclipse "Eclipse for Android Developers" Version: Neon.2 Release (4.6.2)?
Rusty
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Don,
I downloaded and installed it and opened your source code folder.
It can't resolve ba
How do you compile/create a .jar file. There is no MAIN or entry point.
(Forgive me, as i'm not familiar with Java or Eclipse).
Rustyupload_2017-1-3_14-36-29.png
upload_2017-1-3_14-36-29.png
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
do you now have the BUILD Paths Options?
t can't resolve ba
Check Build Options as i wrote earlier and adapt the path to the core.jar and B4Ashared.jar from b4x.
import10129.png

Make sure to have the core.jar and B4Ashared.jar correctly listed here. Both files are inside your B4X installation\library folder. The paths may differ on your machine. You need to set correct references to the files.... Doubleclick the item, select the correct path for it

Do this for core.jar, b4ashared.jar and android.jar

The Samsung sdks should be listed correctly, aren´t they?
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
How do you compile/create a .jar file.
I use SLC

slcvompilecode0131.png


Sorry for the typo in the screenshot.... i wrote the libname wrong...

i for myself are using SLC directly from eclipse

slcvompilecode0132.png


slcvompilecode0133.png


Finally i just click the Icon in the ide to compile....

slcvompilecode0134.png


Based on the parameters set in the tools-config it is using my projectname as libname

slcvompilecode0135.png
 
Last edited:
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Hi Don,
I've followed your advice and have gotten as far as the SLC compile which fails.
However, I notice for some reason my project has a "ViewBadger" head. I don't know where this came from and wonder if this isn't part of my problem.
I don't know how to start a NEW project from scratch as when I import the SamsungDigtalHealth project, Eclipse seems to add Viewbadger to the top. (see post #47)
Do you know why this would be put there by Eclipse? Can you tell me how to import the project without having that inserted?
Rusty
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
the SLC compile which fails
Fails with which error????

However, I notice for some reason my project has a "ViewBadger" head.
All my libs are based on a copy of the viewbadger project... all of them shows a viewbadger somewhere. But it is not the name for the library SLC will create.

but it does not matter as we do not use eclipse to compile....


Maybe i am doing it wrong all the years and i dont know it .... :D
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Starting step: Compiling Java code.
javac 1.7.0_21
D:\Survey Projects\ANDROID LIBRARIES\SamsungDigitalHealth\src\de\donmanfred\DeleteRequestBuilderWrapper.java:11: error: package anywheresoftware.b4a does not exist
import anywheresoftware.b4a.AbsObjectWrapper;
^
1 error


Error.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Hmmm. Sounds like SLC does not find the basic jars from B4A.

Maybe create a new thread (as this issues has nothing to do with the threads subject (Samsung Digital Health)) and ask for setup help.

Last check; the active path when running SLC is the folder where SLC is installed, right?
SLC is running on the same machine where B4A is installed too, right? I think SLC will read the ini file from b4a to know where b4a is installed and where the additional libs folder is...

Maybe this fails on your machine somehow.
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Yes, I start the compiler in it's own folder, on the same machine and hard-drive that B4a is installed on.

...Any chance you can compile those two items in Post #43 into the jar until I can get my environment sorted out?
Anything further on Eclipse, I'll start a new thread.
thanks for all the help, Don,
Rusty
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
compile those two items
Note that the result listener already exists in the HealthDataResolver (it is the result event for the read-method) so in fact this is the added code...
B4X:
    public void readExerciseData() {
    // Set time range for last 24 hours
    long endTime = System.currentTimeMillis();
    long startTime = endTime - 24*60*60*1000;

    Filter filter = Filter.and(Filter.greaterThanEquals(HealthConstants.Exercise.START_TIME, startTime),
    Filter.lessThanEquals(HealthConstants.Exercise.END_TIME, endTime));

    HealthDataResolver.ReadRequest request = new ReadRequest.Builder()
        .setDataType(HealthConstants.Exercise.HEALTH_DATA_TYPE)
        .setProperties(new String[]{HealthConstants.Exercise.LIVE_DATA, HealthConstants.Exercise.END_TIME})
        .setFilter(filter)
        .build();

    try {
        getObject().read(request);
    } catch (Exception e) {
        ba.Log(e.getClass().getName() + " - " + e.getMessage());
    }
    }
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Hi Don,
I've been on other tasks for a while.
Can you tell me how I should call readexercisedata within my B4a app?
Thanks,
Rusty
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Don,
I am having problems obtaining permission to READ EXERCISE data.
I still can't get Eclipse to cooperate and I was looking in HealthPermissionManager.java and find the code:
B4X:
    public HashSet<PermissionKey> getStepCountPermission(){
        mKeySet = new HashSet<PermissionKey>();
    mKeySet.add(new PermissionKey(HealthConstants.StepCount.HEALTH_DATA_TYPE, PermissionType.READ));
    return mKeySet;
    }
I don't see any other permissions defined, but maybe I'm just confused.
I'm thinking we need to have the permissions defined. Most importantly (for the Exercise function), we need HealthConstants.Exercise.HEALTH_DATA_TYPE (I think)
Can you advise me where i might have missed this in the code?
Thanks
Rusty
 
Upvote 0
Top