Track Deadlift Exercise on Galaxy Watch
track deadlift exercise on galaxy watch objective create a native app for galaxy watch, operating on wear os powered by samsung, using health services to track deadlift exercise this app measures repetition count, calories burned, and time spent during the exercise overview health services provides a simple and unified way for accessing a wide range of health and wellness related data with health services api, you will no longer need to develop your own algorithms processing sensors data in order to compute metrics like heart rate, steps counts, distance, calories burned, and other more these are now accessible through health services embedded on wearables operating on wear os powered by samsung see health platform 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! health track deadlift sample code 132 83 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, open your android studio and click open to open an existing project locate the downloaded android project deadlift from the directory and click ok check dependency and app manifest in the dependencies section of gradle scripts > build gradle module app file, see the appropriate dependency for health services dependencies { implementation 'androidx health health-services-client 1 0 0-beta03' // } notesince the library might update from time to time, it is recommended to choose the version suggested by android studio in androidmanifest xml file, note the following <queries> element <queries> <package android name="com google android wearable healthservices" /> </queries> section with requests for necessary permissions <uses-permission android name="android permission body_sensors" /> <uses-permission android name="android permission activity_recognition" /> check capabilities to check what can be measured during an exercise, you need to check its capabilities go to app > java > com samsung sdc21 deadlift open the deadliftutil java file and navigate to the checkcapabilities method an inner class c definition implements the methods of the futurecallback interface within this definition, define the onsuccess method to retrieve the exercise type capabilities public void onsuccess exercisecapabilities result { objects requirenonnull result ; log i tag, "got exercise capabilities" ; /*********************************************************************************** * [practice 1] define the onsuccess method * * - hint uncomment lines below and replace todo 1 * call getexercisetypecapabilities method of result object, * passing already initialized t as an argument **********************************************************************************/ final exercisetype t = exercisetype deadlift; // final exercisetypecapabilities capabilities = "todo 1" // final exerciseconfig builder builder = exerciseconfig builder t ; // builder setdatatypes capabilities getsupporteddatatypes ; // exerciseconfigbuilder = builder; } next, implement the findcapabilitesfuture method to get a callback with exercisecapabilities getcapabilitiesasync returns the exercisecapabilities of the exerciseclient for the device static listenablefuture<exercisecapabilities> findcapabilitiesfuture exerciseclient client { /******************************************************************************************* * [practice 1] create a listenablefuture object that will get a callback with * with exercise capabilities choose the correct method from exerciseclient * * - hint uncomment line and replace null with todo 2 * for checking capabilities use getcapabilitiesasync method ******************************************************************************************/ return null; //"todo 2"; } start the exercise inside the startexercise method, there is a call to the futures addcallback method this method adds a callback function that executes when the asynchronous operation of starting the exercise completes set an update callback for the exercise client within the onsuccess method of the callback function public void onsuccess void result { log i tag, "successfully started" ; /*************************************************************************** * [practice 2] set an update callback * * - hint uncomment lines below and fill todos * 1 make appropriate call of setupdatecallback method * and pass exerciseupdatelistener object as an argument * 2 change ismeasurementrunning flag value to true **************************************************************************/ // exerciseclient setupdatecallback "todo 3 1 " ; log i tag, "successfully set update listener" ; // "todo 3 2 " } in the deadlift java file, call the startexercise method in onbuttonclickhelper public void onbuttonclickhelper { /******************************************************************************************* * [practice 2] start the exercise using a method from deadliftutil java * * - hint uncomment line below and fill todo 4 * call startexercise method on util object * ****************************************************************************************/ // "todo 4" } get the results go to the deadliftutil java file, and in the getnewrepsvalue method, call the getlatestmetrics method from the exerciseupdate class to get the data collected during the exercise store the data in the resultlist, where the last element holds the most up-to-date value of all your repetitions public long getnewrepsvalue exerciseupdate update, deltadatatype<long, intervaldatapoint<long>> datatype { /******************************************************************************************* * [practice 3] get the data collected during exercise * * - hint uncomment lines below and fill todo 5 * call getlatestmetrics method of exerciseupdate object * then, get the data of appropriate type * for this, you can use dedicated method getdata , passing datatype as an argument * ****************************************************************************************/ // final list<intervaldatapoint<long>> resultlist = "todo 5" // if !resultlist isempty { // final int lastindex = resultlist size - 1; // return resultlist get lastindex getvalue ; // } return no_new_value; } 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 instruction below on how to run unit tests right click on com samsung sdc21 deadlift test > deadliftunittest and execute run 'deadliftunittest' 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 actual deadlift parameters right after the app is started, it will request for the user permission allow the app to receive data of the activity afterwards, the application main screen will be shown before doing deadlifts, press the start button to track your exercise when done, tap on the stop button you're done! congratulations! you have successfully achieved the goal of this code lab now, you can create a deadlift exercise tracker app by yourself! if you're having trouble, you may download this file health track deadlift complete code 132 42 kb learn more by going to health platform