Adding a Heart Rate Monitor (HRM) wraps up the Mobile Weather Sensor project. To get started, a sensor selection was needed. And since I already have and use a Garmin HRM with a wireless chest sensor, using that same sensor with my setup was the logical choice.
But how to do it?
That was the challenge…
First thing was to figure out how the Garmin unit worked. What sort of RF signal was being sent to my handlebar mounted display? And what message protocol is used? What will it take to interface with my Android, wirelessly?
After some research, I found that the Garmin HRM used an Ant+ interface.
What’s that? Ant+ does look a bit intimidating at first glance. Take a look here.
Fortunately. I was able to boil things down to the bare essentials of what was needed in order for my android phone to receive heart rate data from the sensor. There were several elements needed to make things work…
- An ant+ radio was needed in the smartphone
- The phone app needed to automatically detect the HRM and pair with it (like bluetooth).
The Phone Ant+ Radio
Fortunately, my Samsung Galaxy Note 3 has a built-in ant++ radio. But in order to access this interface, an App must be installed from the Google Play Store. It is called ANT+ Plugin Service. It is really simple. There is no App icon to launch. This is how it works…
Once installed, the service is called anytime an App attempts to connect with an ant+ device. My phone is an ancient model, almost two years old now. Many of the newer phone come with this service pre-installed.
Fortunately, a mechanism has been provided to “extend” the Cordova Web API to communicate with Java code. Here is the source of the information I used to implement the interface between my App and the native code needed to communicate with my heart rate monitor.
First thing to do was to add a reference to this interface code to the project’s config.xml file:
<feature name="HeartRateMonitor"> <param name="android-package" value="org.apache.cordova.antplus.heartrate.HeartRateMonitorInterface" /> <param name="onload" value="true" /> </feature>
The first parameter identifies the file containing the feature class object. In this case, the Java code is contained in the file “HeartRateMonitorInterface.java”. The second parameter makes this feature available upon loading the application’s Web view.
Only two feature actions were implemented to support the heart rate monitor interface:
- ConnectHeartRateMonitor – This method establishes a connection between the App and the Heart Rate Monitor
- GetHeartRate – This method retrieves the newest heart rate from the Java code.
ConnectHeartRateMonitor is called once at the start of the application while GetHeartRate is called each time the app refreshes the sensor values.
The Java Code
The Java code implements the class “HeartRateMonitorInterface”. This class contains the following key methods:
- ConnectHeartRateMonitor makes a call to the Ant+ API method “.requestAccess”. Fortunately, the API does all the hard work of connecting to the Heart Rate Monitor. The Class callback “base_IPluginAccessResultReceiver.onResultReceived” methods is invoked to report the status of the call.
- base_IPluginAccessResultReceiver.onResultReceived is the callback invoked when the ant+ “.requestAccess” method is called. If access was successfully granted, we subscribe to the Heart Rate Monitor Events. Whenever a new event is received, the callback “HeartRateDataReceiver().onNewHeartRateData()” is invoked.
We now have a working application that supports all of the capabilities planned. The Eclipse project files for this application are on GitHub here. The data collected on this mobile application include:
- Barometric Pressure
- Heart Rate
- UTC timestamp
- Internal ESP statistics
Who knows, there may be more added…
I am looking forward to the coming change of season. With this App and the ESP sensors, I expect to collect some interesting information about significant changes in micro-climate in the span of short distances along the local mountain trails. It will be great to finally quantify what is felt externally as you must peel on and off layers of clothing with changing conditions.
I hope you have found this information is useful to you in your own projects…