Transfer heart rate data from Galaxy Watch to a mobile device with Samsung Privileged Health SDK
transfer heart rate data from galaxy watch to a mobile device with samsung privileged health sdk objective create a health app for galaxy watch, operating on wear os powered by samsung, to measure heart rate and inter-beat interval ibi , send data to a paired android phone, and create an android application for receiving data sent from a paired galaxy watch 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 you will need the full version of the samsung privileged health sdk library to get real values, which is available to samsung partners apply as a partner by checking out the partner app program for exclusive samsung privileged health sdk access overview with this code lab, you can measure various health data using samsung privileged health sdk and send it to a paired android mobile device for further processing using a paired mobile device will make the data more organized by taking advantage of a bigger screen and saving health data to a local database or remote server see samsung privileged health sdk descriptions for detailed information set up your environment you will need the following galaxy watch4 or newer, powered by wear os android mobile device android studio latest version recommended java se development kit jdk 17 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! heart rate data transfer sample code 196 70 kb notethis code lab uses mock library, which has limited functionality with dummy data data from the mock library is not from the galaxy watch’s real sensor data you need a full version of the samsung privileged health sdk library to get actual values and it's only available to registered partners connect your galaxy watch to wi-fi go to settings > connection > wi-fi and make sure that the 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 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 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 will see the following message in the 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 this step is only applicable to registered partners of samsung privileged health sdk you can replace priv-health-tracking-mock-2023 aar in app > libs with the real library to receive real sensor data with the application this requires health platform to be set in developer mode on your watch go to settings > apps > health platform quickly tap health platform title for 10 times this enables developer mode and displays [dev mode] below the title to stop using developer mode, quickly tap health platform title for 10 times to disable it set up your android device click on the following links to setup your android device enable developer options run apps on a hardware device connect the galaxy watch with you samsung mobile phone start your project after downloading the sample code containing the project files, in android studio click open to open an existing project locate the downloaded android project hrdatatransfer-code-lab from the directory and click ok parse data from the watch after opening the sample project, you need to parse the ibi data collected while tracking the heart rate-related data the updatelistener collects datapoint instances from the watch, which contains a collection of valuekey objects those objects contain heart rate, ibi values, and ibi statuses there's always one value for heart rate while the number of ibi values vary from 0-4 both ibi value and ibi status lists have the same size go to the wear > java > data > com samsung health hrdatatransfer under ibidataparsing kt, provide the implementation for the function below /******************************************************************************* * [practice 1] get list of valid inter-beat interval values from a datapoint * - return arraylist<int> of valid ibi values validibilist * - if no ibi value is valid, return an empty arraylist * * var ibivalues is a list representing ibivalues up to 4 * var ibistatuses is a list of their statuses has the same size as ibivalues ------------------------------------------------------------------------------- * - hints * use local function isibivalid status, value to check validity of ibi * ****************************************************************************/ fun getvalidibilist datapoint datapoint arraylist<int> { val ibivalues = datapoint getvalue valuekey heartrateset ibi_list val ibistatuses = datapoint getvalue valuekey heartrateset ibi_status_list val validibilist = arraylist<int> //todo 1 return validibilist } check data sending capabilities for the watch once the heart rate tracker can collect data, set it up so it can send data to a paired android mobile device first, check if the devices within range have proper capabilities the capability data type is a string and is used when sending a message on both devices go to the wear > java > com samsung health hrdatatransfer > data in capabilityrepositoryimpl kt, and fill in the function below /************************************************************************************** * [practice 2] check capabilities for reachable remote nodes devices * - return a set of node objects out of all capabilities represented by 2nd function * argument, having the capability represented by 1st function argument * - return empty set if no node has the capability -------------------------------------------------------------------------------------- * - hints * you might want to use filtervalues function on the given allcapabilities map * ***********************************************************************************/ override suspend fun getnodesforcapability capability string, allcapabilities map<node, set<string>> set<node> { //todo 2 } the purpose of this exercise is to identify which nodes have the exact capability you are looking for represented by capability argument you need those nodes later to send the message to them encode message for the watch before sending the results of the heart rate and ibi to the paired mobile device, you need to encode the message into a string go to the wear > java > com samsung health hrdatatransfer > domain in sendmessageusecase kt, fill in the function below and use json format to encode the list of results arraylist<trackeddata> into a string /*********************************************************************** * [practice 3] - encode heart rate & inter-beat interval into string * - encode function argument trackeddata into json format * - return the encoded string ----------------------------------------------------------------------- * - hint * use json encodetostring function **********************************************************************/ fun encodemessage trackeddata arraylist<trackeddata> string { //todo 3 } 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 or mobile device see the instruction below on how to run unit tests right click on com samsung health hrdatatransfer test , and execute run 'tests in 'com samsung health hrdatatransfer" command do the same step with the mobile module if you have completed all the tasks correctly, you will see all the unit tests pass successfully run the app after building the apks, you can run the applications on your watch to measure heart rate and ibi values, and on your mobile device to collect the data from your watch once the app starts, allow the app to receive data from the body sensors afterwards, it shows the application's main screen to get the heart rate and ibi values, tap the start button tap the send button to send the data to your mobile device notethe watch keeps last ~40 values of heart rate and ibi you’re done! congratulations! you have successfully achieved the goal of this code lab now, you can create a health app on a watch to measure heart rate and ibi, and develop a mobile app that receives that health data! if you face any trouble, you may download this file heart rate data transfer complete project 195 74 kb to learn more about samsung health, visit developer samsung com/health