Lazy Bluetooth: Build with BLE, Don’t Reinvent It


It is a good bet that you have at least one Bluetooth device hanging around. Headsets, mice, keyboards, and speakers have become increasingly common. Bluetooth forms a short range wireless network and can also perform file transfers and create virtual serial ports.

If you have ever had to stop listening to music to recharge a Bluetooth headphone, you know Bluetooth won’t run long on batteries. In 2006, Nokia introduced Wibree, which would later become Bluetooth Low Energy (or BLE). These days it’s used in everything and it’s well worth your time to gather a basic understanding of this technology.

Fitbit_Charge_HRModern Bluetooth devices can implement “classic” Bluetooth, BLE, or both. As you might expect, BLE is very power efficient and  should run for months or years on a simple button cell battery. It is also supposed to be small and inexpensive. If you have a fitness band (like the FitBit at left) or a smart watch, it probably relies on BLE. Range is usually somewhere less than 30 meters.

Clients, Servers, and Conserving Power

GATTTo conserve power, BLE devices don’t transmit very much. Bluetooth devices use profiles that describe what data and services they offer. All BLE devices use profiles derived from the Generic Attribute Profile (GATT). GATT uses UUID (those long hex numbers) to identify services and characteristics. Official services have a 16-bit ID while custom ones have a 128-bit ID. Servers are devices that have data like fitness bands, embedded thermostats, or beacons (BLE devices that send an ID and possibly a small amount of other data). Most devices will offer multiple services (see figure right).

As part of the power conservation scheme, servers have data and spend most of their time asleep. For example, a BLE temperature sensor would act as a server. The client is the cell phone, tablet, or desktop computer that wants to read the data. This can seem backwards, but it is just a matter of picking your frame of reference.

Servers periodically advertise the services they have using a very short message. As part of the advertisement, the server can include a small amount of data or it can tell others that it will not accept connections (or, often, it will do both). When a server sends all of its data via an advertisement and won’t accept connections, it is known as a beacon.

Standard Services

There are many standard services defined in the Bluetooth standard. For example, many devices will provide a battery service so a client can ask the device about its power state. However, it would be a boring device that only provides its battery level.

Other services are geared at BLE’s target markets. For example, there are services that provide blood pressure, running speed, or weight (as in body weight). There are also specialized services like “find me” that causes the server to beep or otherwise indicate where it is.

If you are familiar with traditional Bluetooth, the names won’t throw you. However, if you haven’t worked with the older system, it might help to remember that:

  • A profile contains one or more services.
  • A server has one or more characteristics.
  • While characteristics are typically single values, there are cases where a value can be an array of related data (e.g., X, Y, and Z coordinates from a position sensor)
  • Descriptors describe characteristics

There’s even a proximity service that can estimate the distance between the client and the server. That works by measuring the received signal strength at the server. The client rates itself on expected signal strength at a certain distance and the server reports the actual signal strength. The ratio of these two quantities is related to the distance.

Abstraction

ble1
Hackaday edition Tiny BLE board will be used to demonstrate Bluetooth Low Energy in the next article of this series.

There’s a lot of detail about the BLE internals you can find in the Bluetooth specification. However, in practice, you probably won’t write your own BLE stack. Instead, you’ll use a chipset or a development board that provides an API (like the Tiny BLE that appears to the right).

Abstraction is especially useful for BLE because every microsecond of transmit time is burning battery. You hope the manufacturer has fine-tuned the stack to get the most from their hardware. Sure, maybe you could do better with a lot of effort, but it would likely be hard work. Next time I’ll look at one of those development boards and the API it presents to make BLE development simple.

Meanwhile, you can learn more about BLE (also known as Bluetooth Smart) in the video with [Jimmy Ray Purser] below. [Jimmy Ray] has an inimitable style of presentation.