Diving into the Client Types in Health Services
in our using health services on galaxy watch article, we introduced you to health services and the series of steps you need to follow in order to start using it within your application. now, we present the different client types provided by health services and their core functionality. exerciseclient in recent years, many applications that use and process health data are focused on workouts due to increasing awareness of the importance of maintaining a healthy lifestyle. some of those applications are more complex than others and have a lot of features, from tracking exercise performance to setting daily, weekly or monthly goals. health services provides a specialized client to facilitate tracking ongoing workouts from a wide range of exercise types, such as running, playing soccer and skiing, among others. checking the capabilities on the device as mentioned in our previous post, you have to ensure at runtime that your configuration is supported. remember, there is nothing more confusing to users than dealing with an application that does not work as expected. the code below describes how to check the capabilities of the device for an exercise type for running: healthservicesclient healthclient = healthservices.getclient(this); exerciseclient exerciseclient = healthclient.getexerciseclient(); listenablefuture<exercisecapabilities> capabilitiesfuture = passiveclient.getcapabilities(); futures.addcallback(capabilitiesfuture, new futurecallback<exercisecapabilities>() { @override public void onsuccess(@nullable exercisecapabilities result) { runningcapabilities = result .supporteddatatypesexcercise() .contains(exercisetype.running) } @override public void onfailure(throwable t) { // display an error } }, contextcompat.getmainexecutor(this)); tracking workouts using exerciseclient after ensuring that the device running your application supports the type of exercise(s) you want to track, you must specify the relevant information, such as exercise type, exercise goal, and additional parameters supported by health services. health services provides an exerciseconfig class to help you to define a configuration for an exercise tracked by your application. with the help of this class, you can define and have access to information related to the exercise such as: exercise type exercise goals data type of the exercise flags to indicate the exercise state once you set up your configuration, you can start your exercise and then listen for its progress using an update listener. the basic setup flow should be something like: set up exerciseconfig call exerciseclient.startexercise set up an update listener listen for progress and update your app finally, health services provides a useful class to set goals and keep track of them. the exercisegoal class allows you to set goals based on certain parameters, so the users of your application can create milestones or one-time goals. an example of a milestone could be a hiking app that sets a goal that is achieved multiple times when the user has covered a distance of 200m. the event is triggered every 200m. in contrast, a one-time goal could be defined as a goal that sets a milestone with a threshold equivalent to the height of mount everest. the ability to work with exercise goals in health services will be described in more detail in a separate post. note: when using exerciseclient, make sure your app requests and maintains the necessary permissions. if your app uses location data, you should consider permissions on the device, like checking that gps is enabled. also, you should ensure that a foregroundservice with the appropriate foregroundservicetype is maintained throughout the workout, as shown in the code below: <!-- add foreground service location permission for exercise tracking --> <service android:name=".exerciseservice" android:foregroundservicetype="location" android:exported="false" /> measureclient using this client is highly recommended if your application requires constant updates. in a real-world scenario, your application should use this client when its ui is in the foreground. as a good practice, your application should minimize the amount of time reading data with this client. registering callbacks to read health data can increase sensor sampling rates, increasing power consumption. use this client carefully, having user experience in mind and taking into account that users want to extend the power of the device as much as possible. checking the capabilities on the device healthservicesclient healthclient = healthservices.getclient(this); listenablefuture<measurecapabilities> capabilitiesfuture = healthclient.getcapabilities(); futures.addcallback(capabilitiesfuture, new futurecallback<capabilities>() { @override public void onsuccess(@nullable capabilities result) { boolean supportsheartrate = result .supporteddatatypesmeasure() .contains(datatype.heart_rate_bpm) } @override public void onfailure(throwable t) { // handle error here. } }, contextcompat.getmainexecutor(this)); passivemonitoringclient this client is suitable for applications that monitor health data in the background. this client makes it easier to keep track of activities during longer periods of time, taking advantage of the fact that it does not require your application to be in the foreground. some examples of activities tracked by this client are step count or floors climbed during the day, week or month. checking the capabilities on the device healthservicesclient healthclient = healthservices.getclient(this); passivemonitoringclient passiveclient = healthclient.getpassivemonitoringclient(); listenablefuture<passivemonitoringcapabilities> capabilitiesfuture = passiveclient.getcapabilities(); futures.addcallback(capabilitiesfuture, new futurecallback<passivemonitoringcapabilities>() { @override public void onsuccess(@nullable passivemonitoringcapabilities result) { supportsheartrate = result .getsupporteddatatypespassivemonitoring() .contains(datatype.heart_rate_bpm) supportsstepsevent = result .supporteddatatypesevents() .contains(datatype.steps) } @override public void onfailure(throwable t) { // display an error } }, contextcompat.getmainexecutor(this)); choosing the right health services client having a clear understanding of every client type and their applications help you to make informed decisions when creating your application. this information helps you avoid unexpected results while interacting with the sensors on the device and it improves the overall user experience of your application. receiving health data updates in the background to receive data updates in the background, your application must have a broadcastreceiver declared in its androidmanifest.xml, as we mentioned in our previous post. the updates from health services regarding passive data is delivered to this receiver. so far, we have presented the key features for every client. you can make a more informed and confident decision of how to make the most of health services within your application. we encourage you to check health services to take advantage of all of these features.