Measure blood oxygen on Galaxy Watch
measure blood oxygen 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 blood oxygen spo2 measurement results 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 sample code 188 1 kb connect your galaxy watch to wi-fi go to settings > connection > wi-fi and make sure that wi-fi is enabled from the list of available wi-fi networks, choose and connect to the same one as your pc turn on developer mode and adjust its settings on your watch, go to settings > about watch > software and tap on software version 5 times upon successful activation of developer mode, a toast message displays as on the image below afterwards, developer options is going to be visible under settings tap developer options and enable the following options adb debugging in developer options find wireless debugging turn on wireless debugging check always allow on this network and tap allow go back to developer options and click turn off automatic wi-fi notethere may be differences in settings depending on your one ui version connect your galaxy watch to android studio go to settings > developer options > wireless debugging and choose pair new device take note of the wi-fi pairing code, ip address & port in android studio, go to terminal and type adb pair <ip address> <port> <wi-fi pairing code> when prompted, tap always allow from this computer to allow debugging after successfully pairing, type adb connect <ip address of your watch> <port> upon successful connection, you can see the following message in android studio's terminal connected to <ip address of your watch> now, you can run the app directly on your watch turn on developer mode for health platform swipe down from the top of the screen to open the quick panel, then tap the settings icon scroll down and tap apps select health platform quickly tap health platform for about 10 times developer mode is enabled when [dev mode] appears below health platform noteyou can disable developer mode by quickly tapping the health platform until [dev mode] disappears start your project in android studio, click open to open existing project locate the downloaded android project from the directory and click select folder check capabilities for the device to track data with the samsung health sensor sdk, it must support a given tracker type – blood oxygen to check this, get the list of available tracker types and verify that the tracker is on the list in the trackingmanager kt file, navigate to the isspo2available function use the provided healthtrackingservice object to obtain supported health tracker types using trackingcapability supporthealthtrackertypes from healthtrackingservice api assign the obtained list into the provided local variable availabletrackers trackingcapability returns a healthtrackercapability instance in the healthtrackingservice object supporthealthtrackertypes returns a list of supported tracker types healthtrackingservicehealthtrackingservice initiates a connection to samsung's health tracking service and provides a healthtracker instance to track a healthtrackertype public healthtrackercapability gettrackingcapability provide a healthtrackercapability instance to get a supported health tracker type list /****************************************************************************************** * [practice 1] check the list of supported health tracker types * ---------------------------------------------------------------------------------------- * * hint replace todo 1 with the code as follows * - set the provided local variable 'availabletrackers' using trackingcapability supporthealthtrackertypes * from the healthtrackingservice api * - use the provided healthtrackingservice object ******************************************************************************************/ fun isspo2available boolean? { if healthtrackingservice == null return false var availabletrackers list<healthtrackertype>? = null //todo 1 if availabletrackers == null return null return availabletrackers contains healthtrackertype spo2_on_demand } check connection error resolution using samsung health sensor sdk api, resolve any error when connecting to health tracking service in the trackingmanager kt file, navigate to the processtrackerexception function, and check if the provided healthtrackerexception object has a resolution assign the result to the provided variable hasresolution hasresolution function in the healthtrackerexception object checks if the api can fix the error healthtrackerexceptionhealthtrackerexception provides an error code and an error resolution for a failure of healthtrackingservice connectservice boolean hasresolution checks whether the given error has a resolution /******************************************************************************************* * [practice 2] check if healthtrackerexception has a resolution * ----------------------------------------------------------------------------------------- * * hint replace todo 2 with the code as follows * - set the provided local variable 'hasresolution' using the hasresolution method * called on the healthtrackerexception object ******************************************************************************************/ fun processtrackerexception exception healthtrackerexception { var hasresolution boolean = false //todo 2 if hasresolution { scope launch { _messagestate emit messagestate resolvableerror exception } } else { exception printstacktrace val errormessage = exception message scope launch { _messagestate emit messagestate error errormessage } } } initialize spo2 tracker before the measurement starts, initialize the spo2 tracker by obtaining the proper health tracker object in the trackingmanager kt file, navigate to the initspo2tracker 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 a parameter healthtrackingservicehealthtrackingservice initiates a connection to samsung's health tracking service and provides a healthtracker instance to track a healthtrackertype healthtracker gethealthtracker healthtrackertype healthtrackertype provides a healthtracker instance for the given healthtrackertype /******************************************************************************************* * [practice 3] create a spo2 healthtracker object * - retrieve the healthtracker object * - use the provided variable 'spo2tracker' ------------------------------------------------------------------------------------------- * * hint replace todo 3 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 initspo2tracker healthtracker? { var spo2tracker healthtracker? = null //todo 3 return spo2tracker } perform measurement for the client app to start obtaining the data through the sdk, it has to set a listener method on the healthtracker the application setups the listener when the user taps on the start button each time there is new data, the listener callback receives it after the measurement is completed, the listener has to be disconnected due to battery drain, on-demand measurement should not last more than 30 seconds the measurement is cancelled if the final value is not delivered in time note that the sensor needs a few seconds to warm up and provide correct values, which adds to the overall measurement time the blood oxygen values come in the ondatareceived callback of trackereventlistener in trackingmanager kt file, you can see the code for reading the value private val spo2trackerlistener = object trackereventlistener { override fun ondatareceived data mutablelist<datapoint> { data foreach { datapoint -> val status = datapoint getvalue valuekey spo2set status val spo2 = datapoint getvalue valuekey spo2set spo2 _spo2statusstate value = obtainspo2status status _spo2state value = datastate success spo2 } } } 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 spo2measurement test and execute run 'tests in 'com samsung health sensorsdksample spo2measurement'' 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 measure blood oxygen right after the app is started, it requests for user permission allow the app to receive data from the blood oxygen sensor afterwards, it shows the application's main screen to get the blood oxygen, tap on the start button to stop the measurement, tap on the stop button 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 by yourself! if you're having trouble, you may download this file measuring blood oxygen complete code 188 1 kb to learn more, explore samsung health sensor sdk