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.

https://www.youtube.com/watch?v=Wo2WmJFgf8g

Photo credits:

FitBit – By Wuefab CC BY-SA 4.0

21 thoughts on “Lazy Bluetooth: Build With BLE, Don’t Reinvent It

  1. I just received a BLE tag (iBeacon) purchased from ebay for a couple of dollar,
    ‘BEACON101,IBeacon support BLE4.0’.

    It has two leds (blue, red) and a pushbutton. Pushing once red flashes (on), pushing one more time blue flashes (off).
    I planned using it as a key finder (putting it on on my keychain),
    I literally installed over 40 android app, and my conclusion, is it mostly a joke.

    If it was a buzzer inside, it would might be useful, but finding your key based on some -64dBm value,
    which updates around 10 sec is a non working prototypish solution.

    I searched through the hackaday.io website, but could not find a project around BLE.

    Is there anyone who found a usecase for those 10$ BLE tags? (iBeacon)

      1. My cheapo Xiaomi fitness band has a haptic motor in it. They use that plus a couple leds instead of a screen to keep costs down and have crazy long battery life (advertised as 30 days, but more like 60 in practice).

        The little module can come out of the bands so you can customize or replace bands. Mine has popped out a few times and the buzzer was a godsend. One time it was so lost, I had to use a BLE app to home in using the signal strength, then the buzzer. It came out while moving boxes in the storage room and fell down into a box. It was buzzing away against a couple hangers making a heck of a racket.

        I imagine a haptic motor would jingle keys pretty good.

    1. Yes, i bougth one “NUC” tag, it’s working okay. But not far, one room, and you need to use their app in Chinese,and register with your phone number to make it work

      1. That sounds just like the “NUT” tags I got. The tag needs to be sitting within 3m to the phone for about 5 mins before the tag is shown as “Connected” and allows me to beep the tag.
        Measuring the distance of the tag is very crude at best and normally does not work as the tag keeps disconnecting and about 5m.
        I found the nRF apps most useful as you can see the raw RSSI and use that to figure out how close you are to the tags.

        1. A couple of years ago I was working for a company that was investigating the use of the iBeacons as a way of notifying customers of deals when they arrived at a store. Basically the customer had to have our app and bluetooth on which would create an event on the phone when it received a signal from one of these iBeacons.

  2. BLE has some neat applications, but productizing it is pricey. On top of the usual FCC certification, you need to throw a bunch of money at the Bluetooth SIG for their blessing. This is not like USB, where you can get away without any testing as long as you don’t use the logo.

    The Bluetooth SIG holds patents on necessary parts of BLE. Even if you use a prefab module with firmware already approved by the SIG and FCC cert, you still need to pay up for an “end product qualification” which starts at $2500 for first time submissions from small companies.

    1. I guess you should start in a country that doesn’t care, then make enough money for those licenses before expanding to a western country. Either than or get backers before going commercial.
      But this is HaD and not a business site, so for individual projects it matters not.

      1. Seriously? Because of < $ 5000 you're planning to start a business abroad?

        That won't work out *at all*, because, hell, you'll need some strategy to enter the market there, which would, no matter where you do that, be probably more expensive (in total costs, including administration, lawyer, and your own labour costs), made even more expensive if you're not in the country or even used to that country's economy and culture.

        I really don't understand how you think you'd be selling significant amounts of products in a foreign country without significant investment in marketing and local infrastructure.

        Of course, you could just hope for some "importer" business in your target country to be totally amazed by your product and start an advertisement campaign, and/or deliver your devices to shops etc, but usually, they'd a) not be that amazed by BLE gadget No 120091, and b) they'd try to keep themselves free of these costs. If they don't, hell, they think your idea is very profitable, so gettin a credit at home and doing the same there should be easier than this whole vague "pushing into a market I don't know" thing.

        1. I have you know the richest people in some of the countries that might apply are the ones selling phone related stuff.
          And some of those countries where it might work have a rather large number of people, and low production cost as well as low marketing cost. No need to drop 30 million for a superbowl ad.
          And it seems for you there is only ‘size of apple’ and ‘lose’, but in reality you only need to make a relatively modest initial profit in my scenario and you don’t need to be the gadget every person and site talks about, you only need to be in a decent amount of shops and have people buy it and talk about it in a positive manner after they did.

          Or as I said as an alternative, get some backers, if need be through some kickstarter type funding.

          And incidentally, both google and apple (and even microsoft really) started in a modest from-the-garage type setups.

  3. Hi, I’m a professional embedded engineer and most of my work is in the BLE field. Most devices are using at least some custom services.

    I think I missed the point of the article. Who is trying to reinvent bluetooth? The security has been flawed until LE Secure connections in 4.2, but other than that it is a wonderful space to be in.

    1. I was left wondering that as well – I thought the article was going to launch into using things like a discarded Fitbit to provide the BLE hardware.

      Gee were a fussy bunch :)

      It does highlight how important the heading is – it really sets the direction of the article ( rightly ot wrongly)

    2. Do you, by chance, have any advice/resources on how to program a gatt server (setting service UIDs & creating characteristics) w/ Linux/C++? I have been trying to find a good resource on how to do this but documentation for libraries such as BlueZ seem to be incredibly sparse

      1. Yes, I was in this state a few weeks ago. Bluez has extremely poor documentation and I could not find a proper way to talk with BLE devices using C/C++. Then I started following code in bluez/attrib which has source code for gatttool. Look for interactive.c. I used that as a reference to write code for my C app. It has a few bugs but it works. Gatttool has been deprecated and recommended way is to use the new DBUS api. You will find it in /client folder. But I did arrive to a conclusion that the best way to use BLE(Bluez) with linux is to use noble node js library. It has a simple api, well maintained and it wraps the lower level DBUS api. Adafruit has a very good series on using Bluez with raspberry pi. I recommend watching it as covers quirks I had to spend many hours investigating.

  4. Why do people re-invent stuff like this? Likely because BT is so damn flakey. Considering I can connect two nordic 2.4 ghz chips or two CC1101 433 Mhz chips across the house with 100% reliability for year after year, yet similar pairs of BT boards drop out, diss-appear, lose each other, unpair and drop packets for no reason whatsoever.

  5. Hi guys, I wonder if anyone here can help me.
    I want to make a bluetooth page turner device, that will stick directly on my guitar, under the strings.
    It only needs four buttons (up, down, left and right) to scroll through verses or select next/ previous song on my android phone. It will function like a bluetooth page turner pedal on the opensongapp (https://www.opensongapp.com/make-a-bluetooth-foot-pedal)

    I am looking at the smallest , simplest possible build. Everything I find so far on google, is overkill, I cant find a standalone chip like the bluefruit EZ-key controller used in the article as it has been discontinued.
    Someone mentioned to me looking at Nordic chips, but I dont know which one will work. I am a newbie at this.
    Please could you guys help me or point me in the right direction.

    Thanks in advance :-)
    Regards
    Delarey

Leave a Reply to robbz23Cancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.