How to use Bluetooth Low Energy devices for Android Bike Fitness Application

Aníbal Bastías Soto
8 min readNov 9, 2020

This article explains how to use Bluetooth Low Energy devices like as 4iiii PRECISION Crank (Power Meter) and Polar OH1 (Heart Rate Monitor) for measure data and get Cadence, Power Cycling, Calibrate Crank and Heart Rate Pulse.

Bluetooth Low Energy (BLE)

Bluetooth Low Energy hit the market in 2011 as Bluetooth 4.0. When talking about Bluetooth Low Energy vs. Bluetooth, the key difference is in Bluetooth 4.0’s low power consumption.

Although that may sound like something negative, it’s actually extremely positive when talking about M2M communication. With Bluetooth LE’s power consumption, applications can run on a small battery for four to five years. Although this isn’t ideal for talking on the phone, it is vital for applications that only need to exchange small amounts of data periodically.

Just like Bluetooth, BLE operates in the 2.4 GHz ISM band. Unlike classic Bluetooth, however, BLE remains in sleep mode constantly except for when a connection is initiated. The actual connection times are only a few mS, unlike Bluetooth which would take ~100mS. The reason the connections are so short, is that the data rates are so high at 1 Mb/s.

BLE’s M2M/IoT Applications

  • Blood pressure monitors
  • Fibit-like devices
  • Industrial monitoring sensors
  • Geography-based, targeted promotions (iBeacon)
  • Public transportation apps

Bluetooth vs. Bluetooth Low Energy — The IoT Difference

In summary, Bluetooth and Bluetooth Low Energy are used for very different purposes. Bluetooth can handle a lot of data, but consumes battery life quickly and costs a lot more. BLE is used for applications that do not need to exchange large amounts of data, and can therefore run on battery power for years at a cheaper cost. It all depends on what you’re trying to accomplish.

Key terms and concepts

Here is a summary of key BLE terms and concepts:

  • Generic Attribute Profile (GATT) — The GATT profile is a general specification for sending and receiving short pieces of data known as “attributes” over a BLE link. All current Low Energy application profiles are based on GATT
  • The Bluetooth SIG defines many profiles for Low Energy devices. A profile is a specification for how a device works in a particular application. Note that a device can implement more than one profile. For example, a device could contain a heart rate monitor and a battery level detector.
  • Attribute Protocol (ATT) — GATT is built on top of the Attribute Protocol (ATT). This is also referred to as GATT/ATT. ATT is optimized to run on BLE devices. To this end, it uses as few bytes as possible. Each attribute is uniquely identified by a Universally Unique Identifier (UUID), which is a standardized 128-bit format for a string ID used to uniquely identify information. The attributes transported by ATT are formatted as characteristics and services.
  • Characteristic — A characteristic contains a single value and 0-n descriptors that describe the characteristic’s value. A characteristic can be thought of as a type, analogous to a class.
  • Descriptor — Descriptors are defined attributes that describe a characteristic value. For example, a descriptor might specify a human-readable description, an acceptable range for a characteristic’s value, or a unit of measure that is specific to a characteristic’s value.
  • Service — A service is a collection of characteristics. For example, you could have a service called “Heart Rate Monitor” that includes characteristics such as “heart rate measurement.” You can find a list of existing GATT-based profiles and services on bluetooth.org.

Heart Rate Profile

The Heart Rate Profile is used to enable a data collection device to obtain data from a Heart Rate Sensor that exposes the Heart Rate Service

The profile defines two roles: Heart Rate Sensor and Collector. The Heart Rate Sensor is the device that measures heart rate and other information and the Collector is the device that receives the measurement and other data from a Heart Rate Sensor.

Regarding Collector, this must subscribe to the correct GATT service and Characteristic for reach the connection and receive the notifications.

For this case, for each Profile and Service, this have a UUID associated with the characteristics.

Then, for parse the Heart Rate Measurement, we can guide by the following link:

https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.heart_rate_measurement.xml

This XML describes any characteristics and split by flags. So, to measure the Heart Rate Measurement Value can receive by uint8 or uint16 format.

The current implementation in Kotlin language parse the value by the flag and check the format value:

Polar OH1 Device — Heart Rate Monitor

Power Cycling Profile

This profile also has one mandatory service, the Cycling Speed and Cadence Service and one optional service, the Device Information Service. The CSC Service (Cycling Speed and Cadence Profile) — in for sensors attached to a bicycle or exercise bike to measure cadence and wheel speed.

A cycling power meter is a device on a bicycle that measures the power output of the rider. Most cycling power meters use strain gauges to measure the torque applied, and when combined with angular velocity, calculate power.

Collector Data Diagram

Interfaces

Older cycling power meters use a set of wires to transmit power information to a computer mounted on the bicycle; this system has a serious disadvantage of having fine electrical cables being run all over the bicycle, making it harder to clean as well as using a fair number of fasteners to hold them up. However, since 2009 there is a general trend to move towards wireless systems. Power meters generally transmit data over ANT+ or Bluetooth Low Energy protocols and can be paired to standard bike computers that display information about the power output generated by the rider.

Power meter types

Most cycling power meters use strain gauges to measure torque applied, and when combined with angular velocity, calculate power. Power meters using strain gauges are mounted in the bottom bracket, rear freehub, or crankset. Certain newer devices do not use strain gauges and instead measure power through handlebar-mounted units that utilize the principles of Newton’s Third Law by measuring a cyclist’s opposing forces (gravity, wind resistance, inertia, rolling resistance) and combining these with velocity to determine the rider’s power output.

Crank or Spider

Crank and Spider based power meters measure the torque applied through both pedals via strain gauge/s positioned within the crank or crank spider. A calculation of power is derived from the deflection of the strain gauge/s and pedaling cadence. While most crank-based power meters measure the power output of one leg only or need a second sensor to measure the power output of both legs, the spider-based power meters always measure the total power output of both legs.

These units require specific cranks or cranksets but can be relatively simple to interchange between bikes, depending on compatibility.

Pedals

Pedal-based power meters can be located either in the pedal axle or in the pedal body. This type of cycling power meter measures the cyclist’s force exactly where it is applied, through one or both pedals. Power meters with sensors on both pedals can provide a real dual-sided power measurement, that is power data gathered individually on both legs. This feature is useful to observe and correct penalizing differences in performance between legs.

Power meter pedals are easy to install and swap across bikes.

Bottom bracket

Bottom bracket power meters rely on the torsional deflection in the BB shaft. This is done by the shaft having a disc at each end with perforations. These perforations are detected using non-contact photo-electric sensors that detect when torque is applied to the left pedal and then doubled. Data is sent digitally to a handlebar-mounted computer unit.

These units are difficult to interchange and require a different bottom bracket unit for each bike.

Freehub

A freehub power meter uses the same strain gauges that are present in the crank power meters, but it is located in the rear wheel hub and measures the power at the rear wheel. The power measured by a freehub power meter will be slightly less that the power measured by a crank-based power meter due to power losses in the chain, pedals, and bottom bracket. Because freehub power meters are built into the rear wheel, it is simple to interchange them among bikes so long as the wheels are compatible.

Chain

At the heart of chain units is essentially a guitar pick-up that mounts to the cycle’s chain stay. The pick-up detects chain vibration from which it calculates chain tension which, along with chain speed, gives power output. Finnish company Polar was the first to bring a chain-based power meter to market.

Opposing force

Opposing force power meters measure hill slope (gravity), bike acceleration (inertia) and, sometimes, wind speed. From this, power can be indirectly calculated.

Direct applied force

This method monitors the forces applied to the pedal by the cyclist’s foot. Sensors in the shoe or pedal measure the forces as the cranks rotate, and calculate the power based on the magnitude and direction of the applied force, and the angular velocity of the crank. Advantages of this technique include independent measurement of power for each leg, measurement of efficiency of pedaling style, and (depending on placement of sensors) avoiding the need to replace bike components.

Vendors and models for Crank Power Meters

  • Avio PowerSense
  • WATTEAM Powerbeat
  • PowerTap ChainRing
  • Stages (crank arm)
  • 4iiii Precision (crank arm)
  • 4iiii Fiiiit (crank arm)
  • Pioneer Power
  • Verve infocrank
  • Rotor
  • Magene

Cadence and Power

In cycling, cadence (or pedalling rate) is the number of revolutions of the crank per minute; this is the rate at which a cyclist is pedalling/turning the pedals. Cadence is directly proportional to wheel speed, but is a distinct measurement and changes with gearing — which determines the ratio of crank rpm to wheel rpm.

The current formula that we use for calculating the current Cadence in a crank is

Instant Cadence =( Instant Revolutions / Instant TimeStamp)*60

In cycling, Power cycling can be done manually, usually using a switch on the device to be cycled; automatically, through some type of device, system, or network management monitoring and control; or by remote control; through a communication channel.

GATT Characteristics

The following link contains the full description of Power Cycling GATT Characteristics:

https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.cycling_power_measurement.xml

The next table explains how to calculate the Power Cycling and Cadence with some sample data:

INPUT: 0x2C-00–00–00–7C-00–11–00–24–81 (Octets by UINT16)

In this case, we need the following data:

Power Cycling = 0 [Watt]

Cadence = (17/32.285) * 60 = 0.03 [RPM]

Cycling Control Point

This characteristic indicates the control point reference for beginning to measure the data. For cranks, this is the meaning about the Calibration. Regarding that, this GATT needs to write a Characteristic and need to subscribe to notifications to receive a possible Error or Success Code.

Error: 201

Success: 100

The following Kotlin code is calibrating a Crank writing a 0x0C value to the GATT Characteristic.:

Click 👏 to say “thanks!” and help others find this article.

--

--