Getting Started
getting started to track galaxy watch's health sensor data with the samsung health sensor sdk, do the following import library to project to use the library, you have to add it to the project you can do that by adding implementation in a build gradle file implementation files 'path/to/library/samsung-health-sensor-api aar' request permission the following permissions are related to the sdk activity_recognition body_sensors you can check required permissions for each healthtrackertype in order to perform measurements, application requires the sensors permission make sure to include it in your application's androidmanifest xml <uses-permission android name="android permission activity_recognition" /> and request the permission in your application if activitycompat checkselfpermission getapplicationcontext , "android permission activity_recognition" == packagemanager permission_denied { requestpermissions this, new string[]{manifest permission activity_recognition, 0 ; } connect to health platform the health platform provides the health data tracking service to track sensor data, your watch application needs to connect with the health platform include the samsung health sensor sdk library in your application project and prepare a connection listener private final connectionlistener connectionlistener = new connectionlistener { @override public void onconnectionsuccess { //process connection activities here } @override public void onconnectionended { //process disconnection activities here } @override public void onconnectionfailed healthtrackerexception e { if e geterrorcode == healthtrackerexception old_platform_version || e geterrorcode == healthtrackerexception package_not_installed toast maketext getapplicationcontext , "health platform version is outdated or not installed", toast length_long show ; if e hasresolution { e resolve mainactivity this ; } } }; it's worth noting that onconnectionfailed provides healthtrackerexception that may be able to fix a connection problem by itself if the installed health platform version is outdated or is not installed on a watch, calling resolve is helpful, as it will redirect the user to the health platform download or update page before jumping to the page, we recommend displaying a guide message with the error information and what will happen connect to the health platform healthtrackingservice healthtrackingservice = new healthtrackingservice connectionlistener, getapplicationcontext ; healthtrackingservice connectservice ; check capabilities before tracking sensor data with the sdk, it's good to know if the device you are using can perform the measurement you want to make you can check its capabilities with healthtrackercapability getsupporthealthtrackertypes list<healthtrackertype> availabletrackers = healthtrackingservice gettrackingcapability getsupporthealthtrackertypes ; the list returned by the function includes all the available health tracker types from the watch if the list does not contain a desired health tracker type, you can inform the user that it is not possible to perform the desired measurement on the watch for example, a capability check for healthtrackertype accelerometer_continuous can be the following if !availabletrackers contains healthtrackertype accelerometer_continuous { toast maketext getapplicationcontext , "accelerometer tracking not supported on device", toast length_long show ; log e app_tag, "this watch does not support accelerometer tracking " ; } getting health tracker type as healthtrackertype accelerometer_continuous is available on the watch, create a healthtracker instance private healthtracker tracker = healthtrackingservice gethealthtracker healthtrackertype accelerometer_continuous ; set a tracker event listener to receive sensor data, set up a tracker event listener an example of generic listener healthtracker trackereventlistener listener = new healthtracker trackereventlistener { @override public void ondatareceived @nonnull list<datapoint> list { //process your data } @override public void onflushcompleted { //process flush completion } @override public void onerror healthtracker trackererror trackererror { log i app_tag, " onerror called" ; if trackererror == healthtracker trackererror permission_error { runonuithread -> toast maketext getapplicationcontext , "permissions check failed", toast length_short show ; } if trackererror == healthtracker trackererror sdk_policy_error { runonuithread -> toast maketext getapplicationcontext , "sdk policy denied", toast length_short show ; } } }; sensor data is received in ondatareceived and its datapoint has valuekey objects the value key can be one of the following sets depending on the health tracker type accelerometerset biaset ecgset heartrateset ppgset skintemperatureset spo2set sweatlossset error handling is available in the onerror event of the listener if sdk_policy_error is received, and if you are testing the application without the application's release key, enable the health platform's developer mode once we have a listener ready, tracking sensor data is available by setting an event listener an example of a tracker that is going to read accelerometer data private healthtracker tracker = healthtrackingservice gethealthtracker healthtrackertype accelerometer_continuous ; tracker seteventlistener accelerometerlistener ; if you would like to get another kind of sensor data, create a new healthtracker instance and add a separate listener to it flushing data if the watch display is turned off, the sdk starts collecting data in batches we can use the flush function to force the data to be sent immediately tracker flush ; the flushed data is received by the listener's ondatareceived the flush also resets the counter for sampling before data is automatically pushed for example, if data is normally sent every 300 samples and flush is used after collecting 200 samples, next push happens 300 samples after calling the flush this functionality is best used with batching data types that collect many samples before pushing them finishing data tracking unsetting listeners from trackers, as well as disconnecting from the sdk, is mandatory it is important to stop unnecessary sensor measurements to prevent battery drain after we are done reading data, we need to unset the listener tracker unseteventlistener ; after your work with the api is done, you need to disconnect from the health platform if healthtrackingservice != null healthtrackingservice disconnectservice ; that's everything you need to know before starting to develop you application and making use of the samsung health sensor sdk in case you want to further develop your knowledge about sdk, we encourage you to get familiar with our sample applications