ECG Monitor
ecg monitor in this blog post we are going to show how to track on-demand data, using ecg as an example make sure you are acquainted with the general overview on using the samsung health sensor sdk, by reading samsung health sensor sdk introduction we created a sample application called ecg monitor which allows you to track ecg using functionality covered in the article you can download it and try it yourself ecg monitor v1 2 0 132 6 kb sep 14, 2024 how to prepare for a measurement on successful connection to the health tracking service, but before starting a measurement, we have to check if the device is able to track ecg data we can do this by calling checkcapabilities list<healthtrackertype> availabletrackers = healthtrackingservice gettrackingcapability getsupporthealthtrackertypes ; make sure the returned list contains healthtrackertype ecg_on_demand if !availabletrackers contains healthtrackertype ecg_on_demand { toast maketext getapplicationcontext , getstring r string noecgsupport , toast length_long show ; log e app_tag, "device does not support ecg tracking" ; finish ; } after that, we can set up the ecg tracker object ecgtracker = healthtrackingservice gethealthtracker healthtrackertype ecg_on_demand ; the next step is to prepare an event listener – an object to process incoming data we create one at the start of the application and implement its functionality in this blog, we focus on the ondatareceived event it contains data gathered by the samsung health sensor sdk as a list of data points a full description of all elements of the list can be found in the api reference document for valuekey ecgset, so here we only focus on the necessary parts, which are lead_off and, of course, the ecg values checking if watch electrodes are in contact in order to track ecg values correctly, the watch electrodes must be in contact – meaning the watch must be worn tightly on the wrist and the user has to hold their index finger on the “home” key to track electrode contact, the lead_off flag is used if its value is 0 – it means that electrodes are in contact otherwise – if it's 5 – it means the electrodes are not in contact the ecg readings are valid only if the flag is set, otherwise they should be ignored the flag can be read in valuekey ecgset final int isleadoff = list get 0 getvalue valuekey ecgset lead_off ; in the sample application we set a variable, which holds the current status of electrodes contact final int no_contact = 5; if isleadoff == no_contact { leadoff set true ; return; } else leadoff set false ; how to read ecg values when the ondatareceived event is called, it provides a list of data points the list has either 5 or 10 elements, but each of them contains an ecg value, so regardless of the list size we can iterate through all of them since ecg has a 500 hz sample rate, we need to calculate the average ecg value in the received data the following example calculates the average ecg value obtained in a sample private final healthtracker trackereventlistener ecglistener = new healthtracker trackereventlistener { @override public void ondatareceived @nonnull list<datapoint> list { if list size == 0 return; float sum = 0; for datapoint data list { final float curecg = data getvalue valuekey ecgset ecg_mv ; sum += curecg; } avgecg set sum / list size ; how to track incoming ecg data with the prerequisites in the previous steps done, we are now ready to measure ecg first, we set up a listener ecghandler post -> ecgtracker seteventlistener ecglistener ; we can now start receiving ecg data since the sampling frequency is 500 hz, we cannot update the ui with each sample instead, we create a timer which updates ecg values every second for 30 seconds – the time required to make a successful measurement in the timer, we analyze ecg data and act accordingly all on-demand sensors need about 2 seconds to warm up in order to provide accurate data – therefore we ignore the first 2 seconds of the measurement if timeleft > measurement_duration - 2000 return; after the warm up time, we check if both electrodes are in contact if they are not, we show a warning to the user if leadoff get { runonuithread -> binding txtoutput settext r string outputwarning ; } if the data received is valid, we read the current average ecg value after that, we update the ui with the result for convenience, we also provide information on how much time is left until the end of the measurement else { final string measurevalue = getstring r string measurementupdate, timeleft / 1000, string format locale english, "% 2f", avgecg get ; runonuithread -> binding txtoutput settext measurevalue ; } after 30 seconds, the timer reaches its end, and we stop the measurement by unsetting the listener ecgtracker unseteventlistener ; finally, we update the ui for the last time – with either the last measured ecg value or an error if electrodes were not in contact these steps show how we can use on-demand sensors to perform an ecg measurement we encourage you to try doing it yourself and explore the other available features provided by the samsung health sensor sdk