Samsung Health SDK · Samsung Health SDK SDK Download App Development ... Partner App approval...
Transcript of Samsung Health SDK · Samsung Health SDK SDK Download App Development ... Partner App approval...
Samsung Health Server SDK
Samsung Health
Overview
Health
Data Store
Samsung Health Server
Health Data Store
Data Sync (Samsung Account)
Android Apps (Partner’s App)
BLE Devices (Partner’s
Accessories)
Health Android
SDK
Data Service
Health Device
SDK
BLE
Android
API
BLE
GATT
Data
REST
API
Other Partner’s App Platform REST
API
Oauth2
Apps and Services
Samsung Health SDK
SDK Download
App Development
Test
Apply for Partner Apps
Partner App approval
Publish your Application
Android SDK
Prerequisite
Android 4.4 KitKat (API level 19) or above. Android smartphones including non-Samsung devices. A partner app runs with Samsung Health 4.0 or higher.
http://img-developer.samsung.com/onlinedocs/health/android/data/index.html
Android SDK - Data
Download and install Health SDK Library
Turn on Developer mode in Samsung
Health App
Health Data Service Initialization
HealthDataService healthDataService = new HealthDataService();
try {
healthDataService.initialize(this);
} catch (Exception e) {
e.printStackTrace();
}
Health Data Store Connection
// Create a HealthDataStore instance and set its listener
mStore = new HealthDataStore(this, mConnectionListener);
// Request the connection to the health data store
mStore.connectService();
You can end the health data store connection when the activity is destroyed.
public void onDestroy() {
mStore.disconnectService();
super.onDestroy();
}
Android SDK - Data
http://img-developer.samsung.com/onlinedocs/health/android/data/index.html
Set the listener private final HealthDataStore.ConnectionListener mConnectionListener = new
HealthDataStore.ConnectionListener() {
@Override
public void onConnected() {
Log.d(APP_TAG, "Health data service is connected.");
mReporter = new StepCountReporter(mStore);
if (isPermissionAcquired()) {
mReporter.start(mStepCountObserver);
} else {
requestPermission();
}
}
@Override
public void onConnectionFailed(HealthConnectionErrorResult error) {
Log.d(APP_TAG, "Health data service is not available.");
showConnectionFailureDialog(error);
}
@Override
public void onDisconnected() {
Log.d(APP_TAG, "Health data service is disconnected.");
if (!isFinishing()) {
mStore.connectService();
}
}
};
onConnected(); onConnection Failed(); onDisconnected();
Android SDK - Data
// Create a HealthDataStore instance and set its listener
mStore = new HealthDataStore(this, mConnectionListener);
http://img-developer.samsung.com/onlinedocs/health/android/data/index.html
Connection Failed Handler
Android SDK - Data
http://img-developer.samsung.com/onlinedocs/health/android/data/index.html
Handle the connection error
The connection to the health data store can fail and you can check its error result in onConnectionFailed(). If there is an error, an application checks whether the health framework provides a solution with hasResolution() and calls resolve(). If the health framework provides its solution, resolve() makes an application move to one of the following page without a dialog message: - App market’s Samsung Health page
to install or update it. - Device’s Settings page to
make Samsung Health available. - Samsung Health user’s agreement
page. An application needs to show a proper message for each error case and call resolve().
private void showConnectionFailureDialog(final HealthConnectionErrorResult error) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
if (error.hasResolution()) {
switch (error.getErrorCode()) {
case HealthConnectionErrorResult.PLATFORM_NOT_INSTALLED:
alert.setMessage(R.string.msg_req_install);
break;
case HealthConnectionErrorResult.OLD_VERSION_PLATFORM:
alert.setMessage(R.string.msg_req_upgrade);
break;
case HealthConnectionErrorResult.PLATFORM_DISABLED:
alert.setMessage(R.string.msg_req_enable);
break;
case HealthConnectionErrorResult.USER_AGREEMENT_NEEDED:
alert.setMessage(R.string.msg_req_agree);
break;
default:
alert.setMessage(R.string.msg_req_available);
break;
}
} else { alert.setMessage(R.string.msg_conn_not_available); }
alert.setPositiveButton(R.string.ok, (dialog, id) -> {
if (error.hasResolution()) { error.resolve(MainActivity.this); }
});
if (error.hasResolution()) { alert.setNegativeButton(R.string.cancel, null); }
alert.show();
}
Android SDK - Data
Check Permission
http://img-developer.samsung.com/onlinedocs/health/android/data/index.html
private boolean isPermissionAcquired() {
PermissionKey permKey = new PermissionKey(HealthConstants.StepCount.HEALTH_DATA_TYPE,
PermissionType.READ);
HealthPermissionManager pmsManager = new HealthPermissionManager(mStore);
try {
// Check whether the permissions that this application needs are acquired
Map<PermissionKey, Boolean> resultMap =
pmsManager.isPermissionAcquired(Collections.singleton(permKey));
return resultMap.get(permKey);
} catch (Exception e) {
Log.e(APP_TAG, "Permission request fails.", e);
}
return false;
}
@Override
public void onConnected() {
Log.d(APP_TAG, "Health data service is connected.");
mReporter = new StepCountReporter(mStore);
if (isPermissionAcquired()) {
mReporter.start(mStepCountObserver);
} else {
requestPermission();
}
}
Upon service connection, you may need to check if the required permission acquired from user or not. Request permission method, to be called when required permission is not acquired.
Android SDK - Data
Setup Permission
Set meta-data in Android manifest Build permission request method *you can use: HashSet<permissionKey>() for multiple permission request requestPermission method should be called after service connected: onConnected() – only if permission is not acquired setResultListener and Handle it.
http://img-developer.samsung.com/onlinedocs/health/android/data/index.html
<application>
<meta-data
android:name="com.samsung.android.health.permission.read"
android:value="com.samsung.health.step_count" />
</application>
private void requestPermission() {
PermissionKey permKey = new
PermissionKey(HealthConstants.StepCount.HEALTH_DATA_TYPE, PermissionType.READ);
HealthPermissionManager pmsManager = new HealthPermissionManager(mStore);
try {
// Show user permission UI for allowing user to change options
pmsManager.requestPermissions(Collections.singleton(permKey),
MainActivity.this)
.setResultListener(result -> {
Log.d(APP_TAG, "Permission callback is received.");
Map<PermissionKey, Boolean> resultMap = result.getResultMap();
if (resultMap.containsValue(Boolean.FALSE)) {
updateStepCountView("");
showPermissionAlarmDialog();
} else {
// Get the current step count and display it
mReporter.start(mStepCountObserver);
}
});
} catch (Exception e) { Log.e(APP_TAG, "Permission setting fails.", e); }
}
Android SDK - Data
Let’s everything together
http://img-developer.samsung.com/onlinedocs/health/android/data/index.html
Prerequisite
Android 4.4 KitKat (API Level 19) or above. Android smartphones including non-Samsung devices. Samsung Health's partner app runs with Samsung Health 4.0 or above.
Android SDK - Service
Download and install Health SDK Library
*in most case you may need to load Samsung Health Data library as well
Turn on Developer mode in Samsung
Health App
http://img-developer.samsung.com/onlinedocs/health/android/service/index.html
Android SDK - Service
http://img-developer.samsung.com/onlinedocs/health/android/service/index.html
Health Data Service Initialization
Shealth shealth = new Shealth();
try {
shealth.initialize(this);
if (shealth.isFeatureEnabled(Shealth.FEATURE_TRACKER_TILE, Shealth.FEATURE_TRACKER_LAUNCH_EXTENDED))
{
mTrackerManager = new TrackerManager(this);
mTrackerTileManager = new TrackerTileManager(this);
} else {
Log.d(LOG_TAG, "SHealth should be upgraded");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(STORE_URL));
this.startActivity(intent);
finish();
return;
}
} catch (IllegalStateException e) {
Log.e(LOG_TAG, e.toString());
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
finish();
return;
} catch (IllegalArgumentException e) {
Log.e(LOG_TAG, e.toString());
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
finish();
return;
}
Check Feature availability
private static final String STORE_URL = "market://details?id=com.sec.android.app.shealth";
Android SDK - Service
http://img-developer.samsung.com/onlinedocs/health/android/service/index.html
Create Tracker Manager try {
mTrackerManager = new TrackerManager(this);
}catch (IllegalArgumentException e) {
Log.e(LOG_TAG, e.toString());
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
finish();
return;
}
Create a TrackerManager's instance first. Launching the Samsung Health's tracker is supported in Samsung Health 4.8 or above.
TrackerInfo trackerInfo = mTrackerManager.getTrackerInfo(TrackerManager.TrackerId.WATER);
if(trackerInfo == null) {
Log.d(APP_TAG, "trackerInfo == null");
return null;
}
if(trackerInfo.isAvailable() == false) {
Log.d(APP_TAG, "trackerInfo is not available.");
return null;
}
return trackerInfo;
((ImageView)findViewById(R.id.water_icon_img)).setImageDrawable(trackerInfo.getIcon());
((TextView)findViewById(R.id.water_service_name)).setText(trackerInfo.getDisplayName());
Get Tracker Info
The interesting tracker's information can be retrieved by TrackerManager. Check the tracker's availability also.
Retrieve Tracker Info
Android SDK - Service
http://img-developer.samsung.com/onlinedocs/health/android/service/index.html
Launching available Tracker
If the tracker is available, you can launch it.
try {
mTrackerManager.startActivity((Activity)v.getContext(), TrackerManager.TrackerId.WATER,
TrackerManager.Destination.TRACK);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
Android SDK - Service
http://img-developer.samsung.com/onlinedocs/health/android/service/index.html
Posting Your App's Tracker
If you cannot find a wanted tracker on Samsung Health, you can define your app's own tracker.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="sample_tracker_manifest">
{
\"tracker\" : {
\"id\" : \"tracker.sample\",
\"display-name\" : \"tracker_display_name\",
\"icon\" : \"tracker_icon\",
\"controller\" : \"local.sapl.dev.shealth.service.MyTracker\"
}
}
</string>
</resources>
Manifest for Tracker Definition
Plugin Service Declaration and Registration of Plugin Service
Plugin service must be declared in the app’s manifest from Android Oreo (API Level 26). Use your app’s package name in the android authorities.
<provider
android:name="com.samsung.android.sdk.shealth.PluginContentProvider"
android:authorities="com.samsung.android.app.sampleservice.pluginservice"
android:exported="true">
</provider>
<service
android:name="com.samsung.android.sdk.shealth.PluginService"
android:exported="true" >
<meta-data
android:name="tracker.sample" android:value="@string/sample_tracker_manifest"/>
</service>
android:name - the tracker ID of the tracker manifest file. android:value - the resource name of the tracker manifest file.
Android SDK - Service
http://img-developer.samsung.com/onlinedocs/health/android/service/index.html
Handling Event from Samsung Health
Creating a TrackerTileManager's Instance
Creating Tracker Tile's Intent
Posting a Tracker Tile
Updating the Posted Tracker Tile
Let’s everything together