Measure blood oxygen and heart rate on Galaxy Watch
measure blood oxygen and heart rate on galaxy watch objective create a health app for galaxy watch, operating on wear os powered by samsung, utilizing samsung health sensor sdk to trigger and obtain results of simultaneous blood oxygen spo2 and heart rate measurements overview samsung health sensor sdk provides means of accessing and tracking health information contained in the health data storage its tracking service gives raw and processed sensor data such as accelerometer and body composition data sent by the samsung bioactive sensor the latest bioactive sensor of galaxy watch runs powerful health sensors such as photoplethysmogram ppg , electrocardiogram ecg , bioelectrical impedance analysis bia , sweat loss, and spo2 see samsung health sensor sdk descriptions for detailed information set up your environment you will need the following galaxy watch4 or newer android studio latest version recommended java se development kit jdk 11 or later sample code here is a sample code for you to start coding in this code lab download it and start your learning experience! measuring blood oxygen and heart rate sample code 190 1 kb set up your galaxy watch click the following links to set up your device connect to your galaxy watch enable the developer mode of health sensor service start your project in android studio, click open to open existing project locate the downloaded android project from the directory and click select folder establish service connection and check capabilities for the device to track data with the samsung health sensor sdk, it must connect to the service by healthtrackingservice api after establishing a connection, verify if the required tracker type is available to check this, get the list of available tracker types and verify that the tracker is on the list in this code lab, you're going to utilize blood oxygen and heart rate trackers the healthtrackingservice api usage is in the table below healthtrackingservicehealthtrackingservice initiates a connection to samsung's health tracking service and provides a healthtracker instance to track a healthtrackertype public void connectservice establish connection with samsung's health tracking service public void disconnectservice release connection for samsung's health tracking service public healthtrackercapability gettrackingcapability provide a healthtrackercapability instance to get a supported health tracker type list initialize multiple trackers before the measurement starts, initialize the spo2 tracker by obtaining the proper health tracker object in the trackingmanager kt file, navigate to the initspo2 method and create a blood oxygen healthtracker instance retrieve the healthtracker object using the healthtrackingservice gethealthtracker api use the healthtrackertype spo2_on_demand type as parameter healthtrackingservicehealthtrackingservice initiates a connection to samsung's health tracking service and provides a healthtracker instance to track a healthtrackertype public healthtracker gethealthtracker healthtrackertype healthtrackertype provide a healthtracker instance for the given healthtrackertype /******************************************************************************************* * [practice 1] create a spo2 healthtracker object * - retrieve the healthtracker object * - use the provided variable 'spo2tracker' ------------------------------------------------------------------------------------------- * * hint replace todo 1 with the code as follows * - retrieve the healthtracker object using healthtrackingservice gethealthtracker * - use the healthtrackertype spo2_on_demand type as a parameter * - assign it to the 'spo2tracker' variable ******************************************************************************************/ fun initspo2 healthtracker? { var spo2tracker healthtracker? = null //todo 1 return spo2tracker } next, in the initheartrate function, create a heart rate healthtracker instance retrieve the healthtracker object using the healthtrackingservice gethealthtracker api use the healthtrackertype heart_rate_continuous type as a parameter /******************************************************************************************* * [practice 2] create a heart rate healthtracker object * - retrieve the healthtracker object * - use the provided variable 'heartratetracker' ------------------------------------------------------------------------------------------- * * hint replace todo 2 with the code as follows * - retrieve the healthtracker object using healthtrackingservice gethealthtracker * - use the healthtrackertype heart_rate_continuous type as a parameter * - assign it to the 'heartratetracker' variable ******************************************************************************************/ fun initheartrate healthtracker? { var heartratetracker healthtracker? = null //todo 2 return heartratetracker } start and stop trackers for the client app to start obtaining the data through the sdk, set a listener method on healthtracker this method is called every time there is new data after the measurement completes, the listener has to be disconnected to start measurement in the trackingmanager kt file, navigate to the starttracker function and set the trackereventlistener as the listener of the healthtracker object set the event listener on the healthtracker object using healthtracker seteventlistener use the healthtracker trackereventlistener object instance as a parameter healthtrackerhealthtracker enables an application to set an event listener and get tracking data for a specific healthtrackertype public void seteventlistener healthtracker trackereventlistener listener set an event listener to this healthtracker instance /******************************************************************************************* * [practice 3] start tracking by setting an event listener on the healthtracker object ------------------------------------------------------------------------------------------- * * hint replace todo 3 with the code as follows * - use the provided variables 'healthtracker' and 'trackereventlistener' * - set the event listener on the healthtracker object using healthtracker seteventlistener * - use the 'trackereventlistener' object as a parameter ******************************************************************************************/ fun starttracker healthtracker healthtracker, trackereventlistener trackereventlistener { log i logtags app_tag, "starttracker called" log d logtags app_tag, "healthtracker $healthtracker" log d logtags app_tag, "trackereventlistener $trackereventlistener" //todo 3 } to stop measurement, unset the trackereventlistener from the healthtracker object in the stoptracker function unset the event listener on the healthtracker object using the healthtracking unseteventlistener function healthtrackerhealthtracker enables an application to set an event listener and receive the galaxy watch's sensor data for a specific healthtrackertype public void unseteventlistener stop the registered event listener for this healthtracker instance /******************************************************************************************* * [practice 4] stop tracking by removing the event listener from the healthtracker object ------------------------------------------------------------------------------------------- * * hint replace todo 4 with the code as follows * - use the provided variable 'healthtracker' * - unset the event listener on the healthtracker object using healthtracker unseteventlistener ******************************************************************************************/ fun stoptracker healthtracker healthtracker { log i logtags app_tag, "stoptracker called" log d logtags app_tag, "healthtracker $healthtracker" //todo 4 } process obtained data the response from the platform is asynchronous with the results you want to obtain follow the steps below to get blood oxygen and heart rate data navigate to the readspo2 function and retrieve spo2 data from the datapoint retrieve the blood oxygen status using the datapoint getvalue function with valuekey spo2set status as a parameter retrieve the blood oxygen value using the datapoint getvalue function with valuekey spo2set spo2 as a parameter datapointdatapoint provides a map of valuekey and value with a timestamp public <t>t getvalue valuekey<t> type get a data value for the given key /******************************************************************************************* * [practice 5] read values from the datapoint object * - retrieve the spo2 status * - retrieve the spo2 value ------------------------------------------------------------------------------------------- * * hint replace todo 5 with the code as follows * - set the provided local variable 'status' using datapoint getvalue valuekey spo2set status * - set the provided local variable 'spo2value' using datapoint getvalue valuekey spo2set spo2 ******************************************************************************************/ fun readspo2 datapoint datapoint spo2 { var status int? = null var spo2value int? = null //todo 5 return spo2 status, spo2value } next, navigate to the readheartrate function and read heart rate data from the datapoint retrieve the heart rate status using the datapoint getvalue function with valuekey heartrateset heart_rate_status as a parameter retrieve the heart rate value using the datapoint getvalue function with valuekey heartrateset heart_rate as a parameter retrieve the heart rate ibi value list using the datapoint getvalue function with valuekey heartrateset ibi_list as a parameter retrieve the ibi status list using the datapoint getvalue function with valuekey heartrateset ibi_status_list as a parameter /******************************************************************************************* * [practice 6] read values from the datapoint object * - retrieve the heart rate status * - retrieve the heart rate value * - retrieve the list of ibi for the heart rate data * - retrieve the list of ibi statuses for the heart rate data ------------------------------------------------------------------------------------------- * * hint replace todo 6 with the code as follows * - set the provided local variable 'heartratestatus' using datapoint getvalue valuekey heartrateset heart_rate_status * - set the provided local variable 'heartrate' using datapoint getvalue valuekey heartrateset heart_rate * - set the provided local variable 'ibilist' using datapoint getvalue valuekey heartrateset ibi_list * - set the provided local variable 'ibistatuslist' using datapoint getvalue valuekey heartrateset ibi_status_list ******************************************************************************************/ fun readheartrate datapoint datapoint heartrate { var heartratestatus int? = null var heartrate int? = null var ibilist list<int>? = null var ibistatuslist list<int>? = null //todo 6 log i logtags app_tag, "heart rate $heartrate, hr status $heartratestatus, ibi $ibilist, ibi status $ibistatuslist" return heartrate heartrate, heartratestatus, ibilist, ibistatuslist } run unit tests for your convenience, you can find an additional unit tests package this lets you verify your code changes even without using a physical watch see instructions below on how to run unit tests right-click on com samsung health sensorsdksample multisensormeasurement test and execute run 'tests in 'com samsung health sensorsdksample multisensormeasurement' command if you completed all the tasks correctly, you can see that all the unit tests passed successfully run the app after building the apk, you can run the application on a connected device to see blood oxygen and heart rate values right after the app is started, it requests for user permission allow the app to receive data from heart rate and blood oxygen sensors afterwards, it shows the application's main screen and automatically display the heart rate to get the blood oxygen spo2 value, tap on the start button to stop the measurement, tap on the stop button swipe right to view details of heart rate data you're done! congratulations! you have successfully achieved the goal of this code lab now, you can create a health app that measures blood oxygen and heart rate by yourself! if you're having trouble, you may download this file measuring blood oxygen and heart rate complete code 189 6 kb to learn more, explore samsung health sensor sdk