measure blood oxygen level on galaxy watch objective create a health app for galaxy watch, operating on wear os powered by samsung, utilizing the new samsung privileged health sdk to trigger and obtain blood oxygen level (spo2) measurement results. partnership request in this code lab, you will use a specially prepared mock library. it has limited functionality and uses dummy data instead of real-time data. to get real values, you will need the full version of the samsung privileged health sdk library, which is available to registered samsung partners. apply as a partner by checking out the partner app program to get exclusive access to the samsung privileged health sdk. overview samsung privileged health 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 privileged health 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 level sample code (110.13 kb) 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 will display as on the image below. afterwards, developer options will be visible under settings. tap developer options and enable the following options: adb debugging debug over wi-fi turn off automatic wi-fi 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. when successfully connected, tap a wi-fi network name, swipe down, and note the ip address. you will need this to connect your watch over adb from your pc. connect your galaxy watch to android studio in android studio, go to terminal and type: adb connect <ip address as mentioned in previous step> when prompted, tap always allow from this computer to allow debugging. upon successful connection, you will 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. start your project after downloading the sample code containing the project files, in android studio click open to open existing project. locate the downloaded android project (basic) from the directory and click ok. check capabilities for the device to track data with the samsung privileged health sdk, it must support a given tracker type – blood oxygen level. to check this, get the list of available tracker types and verify that the tracker is on the list. in the connectionmanager.java file, navigate to the isspo2available() function, use a provided healthtrackingservice object to create a healthtrackercapability instance, send it to the checkavailabletrackers function, and assign its result to the availabletrackers list. gettrackingcapability() returns a healthtrackercapability instance in the healthtrackingservice object 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 supporting health tracker type list. /****************************************************************************************** * [practice 1] check capabilities to confirm spo2 availability * * ---------------------------------------------------------------------------------------- * * (hint) replace todo 1 with java code * get healthtrackercapability object from healthtrackingservice * send the object to checkavailabletrackers() ******************************************************************************************/ public boolean isspo2available(healthtrackingservice healthtrackingservice) { if (healthtrackingservice == null) return false; list<healthtrackertype> availabletrackers = null; //"todo 1" if (availabletrackers == null) return false; else return availabletrackers.contains(healthtrackertype.spo2); } check connection error resolution suppose a connection to the health tracking service attempt ends with an error. in that case, you can try to resolve it using the samsung privileged health sdk api. in the connectionmanager.java file, navigate to the processtrackerexception() function, and check if the provided healthtrackerexception object has a resolution. assign the result to hasresolution variable. hasresolution() function in the healthtrackerexception object checks if the api side can fix the error healthtrackerexceptionhealthtrackerexception contains error codes and checks the error's resolution. if there is a resolution, solving the error is available by calling resolve(activity). boolean hasresolution() checks whether the given error has a resolution. /******************************************************************************************* * [practice 2] resolve healthtrackerexception error * * ----------------------------------------------------------------------------------------- * * (hint) replace todo 2 with java code * call hasresolution() on healthtrackerexception object ******************************************************************************************/ public void processtrackerexception(healthtrackerexception e) { boolean hasresolution = false; //"todo 2" if (hasresolution) e.resolve(callingactivity); if (e.geterrorcode() == healthtrackerexception.old_platform_version || e.geterrorcode() == healthtrackerexception.package_not_installed) observerupdater.getobserverupdater().notifyconnectionobservers(r.string.novalidhealthplatform); else observerupdater.getobserverupdater().notifyconnectionobservers(r.string.connectionerror); log.e(tag, "could not connect to health tracking service: " + e.getmessage()); } initialize spo2 tracker before the measurement starts, initialize the spo2 tracker by obtaining the proper health tracker object. in the spo2listener.java file, navigate to the init() function. using the provided healthtrackingservice object, create an instance of the spo2 tracker and assign it to the spo2tracker object. gethealthtracker() with healthtrackertype.spo2 as an argument will create a healthtracker instance 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] initialize spo2 tracker * * ---------------------------------------------------------------------------------------- * * (hint) replace todo 3 with java code * initialize spo2tracker with proper samsung privileged health sdk functionality * call gethealthtracker() on healthtrackingservice object * use healthtrackertype.spo2 as an argument ******************************************************************************************/ void init(healthtrackingservice healthtrackingservice) { //"todo 3" } 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 measure button. each time there is new data, the listener callback receives it. after the measurement completes, the listener has to be disconnected. since, due to battery drain, on-demand measurement should not last more than approximately 30 seconds. the measurement will cancel if the final blood oxygen level value is not delivered in time. it’s also worth noting that the sensor needs a few seconds to warm up and provide correct values, adding to the overall measurement time. the blood oxygen level values come in the ondatareceived callback of trackereventlistener. see below example of the code, in the spo2listener.java file, reading the value: private final healthtracker.trackereventlistener spo2listener = new healthtracker.trackereventlistener() { @override public void ondatareceived(@nonnull list<datapoint> list) { for (datapoint data : list) { updatespo2(data); } } }; private void updatespo2(datapoint data) { int status = data.getvalue(valuekey.spo2set.status); int spo2value = 0; if (status == measurement_completed) spo2value = data.getvalue(valuekey.spo2set.spo2); observerupdater.getobserverupdater().notifytrackerobservers(status, spo2value); } run unit tests for your convenience, you will find an additional unit tests package. this will let you verify your code changes even without using a physical watch. see instructions below on how to run the unit tests: right click on com.samsung.sdc22.health.basic (test) and execute run 'tests in 'com.samsung.sdc22.health.basic'' command. if you completed all the tasks correctly, you will see all the unit tests passed successfully. run the app after building the apk, you can run the application on a connected device to measure your blood oxygen level. right after the app is started, it will request user permission. allow the app to receive data from the body sensors. afterwards, it will show the application's main screen. to get the blood oxygen level value, tap on the measure 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 level by yourself! if you're having trouble, you may download this file: measuring blood oxygen level complete code (109.86 kb) to learn more about samsung health, visit: developer.samsung.com/health