How-tos: ANT+ Devices
When people think about connections between "activity trackers", or wearables and devices, they usually think about Bluetooth or Bluetooth Low Energy. However, there exists another communication protocol that is actually very commonly used in the fitness world. This is the ANT protocol.
What is it
In its essence, ANT is just a communication method between two devices similar to Bluetooth Low Energy (BLE), except that ANT typically does one-way communication whereas BLE does two-way connections. In addition to this, ANT uses way less energy and can connect many devices at once whereas BLE is usually limited to only a couple of devices.
ANT+ on the other hand are device profiles that follow the ANT protocol and defines the type of information transferred by that profile. For example, there is a device profile for Heart Rate monitors which is specified to only broadcast Heart Rate data. There are also ones for bike speed sensors, bike cadence sensors, and more.
ANT+ Connections Basics
ANT+ device connections can be done if you follow the ANT protocol as shown by ANT's official page. Luckily, we can take this to a higher level and avoid transmitting and receiving bits by using the already given ANT+ SDK on Android (N.B this SDK would require you to install two additional plugins on your device). Trust me if you are not a big fan of hardware specifics, you might want to just download the SDK.
Once the SDK is installed, you can start searching and connecting to devices. We can use the following steps as a guide to how we can do this:
- Search for device
- Connect to the device and request permissions from the device
- Subscribe to the data you want
Searching
Let us start with searching for a device. This can be done using MultiDeviceSearch
class from the SDK
Assume we are only going to search for HEARTRATE
supported devices here. We will then also need to implement a searchCallback
and rssiCallback
. These serve as callback methods for different events, such as when a device has been found, or when the search has started and stopped.
Perfect! We now can scan for any devices that support HEARTRATE
updates. From here, we can directly try to connect to the device from onDeviceFound
and try to read the device's heart rate values.
Connecting
Let us edit the onDeviceFound
callback to try connect to the first device found (for convenience of course. In reality this should be handled differently, perhaps a ListAdapter
showing all the new found devices and having a frontend to select which device to connect to)
The connection is done by the requestAccess
function here. This function requires you to implement two other callbacks: pluginAccessResultReceiver
and deviceStateChangeReceiver
:
Thats it! You should now be connected (at least given that the pluginAccessResultReceiver
receives a SUCCESS
in the callback).
Subscribing
Now that we have connected, the last thing we need is to subscribe to the device's updates.
Let's try to subscribe to the HEARTRATE
events from the device. From the onResultReceived
callback function, we have a Plugin Communicator Class (PCC) object that is connected to the device. We can subscribe to the events from this class:
Thats it! You can now take the value from the onNewHeartRateData
and display it or stream it to your backend for further processing.
Quick-Go
Terra has abstracted ALL of this implementation into a single SDK: TerraRTAndroid. The SDK not only subscribes to Heart Rate event devices as shown in this example, but it also provides:
- Connecting to ANY ANT+ device imaginable
- A frontend widget to select which device to connect to
- A comprehensive selection of datatypes to subscribe to
- The data is streamed straight to your backend through our WebSocket API
- Seamlessly disconnect and unsubscribe from the devices and datatypes.
If you really aren't in the mood to implement all the steps highlighted in this blog, check out our SDK (installed in one line and only requires you to run two functions to start receiving data: startAntPlusScan
and startRealtime
.)