Bluetooth Low Energy (a.k.a Bluetooth LE, BTLE, Bluetooth Smart)

Getting started with Bluetooth Smart (Bluetooth low energy) on mbed

04 Oct 2016

On Android, the BLE stack didn’t really work in 4.3 and has been getting progressively better since, but runs into all kinds of bugs depending on the platform (this StackOverflow post documents some of those problems very well). There are a lot of reasons, and some history, for why this is the case – but to me that doesn’t matter. As an app developer, all I know is that it’s only semi-stable depending on the manufacturer, OS, time of day, wind conditions, etc…

CoreBluetooth is much more stable in terms of BLE (except on a specific version of iOS which was horribly broken – I think it was iOS 8.1ish territory). However, it’s a little bit low-level and not very well abstracted away (I would say similar about Android BLE to some extent too). Also, iOS in general adds some limitations on how much BLE stack access there is (hard to modify connection intervals and such). How’s Your API?

Anyways, all of these things got me looking around for libraries that I can put on top of those low-level libraries to make my life easier. For me, I’m all about the API. I’ve noticed that when you start looking at and developing APIs, you’ll find that there are some patterns you like, some you don’t; and that some people make APIs as simple as possible to start using, whereas some people like swiss-army knives – at the ‘expense’ of more code to get started on.

A good example of this is LGBluetooth vs BluetoothKit for iOS. LGBluetooth wants to make things simple, streamlined, and idiot-proof – but BluetoothKit wants a lot of functionality at the expense of somewhat ‘more challenging’ to setup and understand.

Me, personally, I love simplicity with the option to make extensible (extensibility – which, 90% of the time, I don’t use). For example, one of my proudest APIs is this one to control a camera:

Basically 10 lines of code, which controls a cross-platform, custom UVC camera. I have the hooks so that you can independently handle memory management, images, drivers, add custom algorithms, etc… However, to get up and running for 90% of the time, it’s 10ish lines.

That’s just my philosophy. As a developer, even if you make really good stuff, but hard to use, people probably won’t use it.

In my opinion, no one does the API game better than Square. Back to BLE

So, extending that to the Android BLE API – it’s a nightmare. I have to set up a bunch of code, broadcast receivers, callbacks, etc… Ugh. Horrible.

What’s 90% of all BLE communication? Scanning for devices, getting advertisements, writing to a peripheral, reading from a peripheral… That’s it, so why does anyone really need more than those 4-5 API calls + 1-2 for initialization and maybe tear-down?

It’s a rhetorical question, but that’s what I think about how APIs work. If you want, add in hooks to do deeper levels of control over your library, but encourage people to use your ‘helper’ functions which encapsulate all the underlying cruft. Lambda, Lambda, Lambda

People… Use lambda functions… “new Callback() {…}” is a lot of weak sauce.

The value of lambdas in BLE communication surrounds the fact that I usually need a callback directly after a successful read/write – so why should I dig through other parts of my code to see what the callback was doing, when it could be right in front of me in the form of “() -> “.

RetroLambda makes this happen. Callback Hell

This one is prolific in Javascript code, but present in iOS and Android too.

BLE is inherently sequential, meaning that you can generally only do one call at a time (otherwise, you might squash a previous call if it wasn’t finished). If you want to chain calls, you need to nest them in callbacks. Problem is, when you want to read a BLE device’s standard information, that’s 5-6 nested calls. You can read the full post here: http://www.sureshjoshi.com/mobile/bluetooth-bluetooths-blueteeth/

10 Oct 2016

hi,

at the moment i'm learning the mbed somehow i miss something. i have the board: https://developer.mbed.org/platforms/Seeed-Tiny-BLE/

i loaded the project in mbed workbench and i can compile it. it looks like that it works (using serial for debugging ) but on the android phone with the nordic app i can't find it. for me it looks like the bluetooth is not active.

would be nice if there are some hints, thank you.

01 Mar 2017

The description code in the text here shows a different code than in the downloaded example. This makes it a bit confusing as this is supposed to be an introduction.

Jim

01 Mar 2017

The Heart Rate description code in the text here shows a different code than in the downloaded example. This makes it a bit confusing as this is supposed to be an introduction.

Jim

15 Jan 2018

Hi, I would like to use BLE for sending some sensor data now and again. It is possible to include some data, say temperature and battery condition in the advertise transmission. It is also possible to use a short connection for the actual data, after advertise. What is used most commonly? Which BLE modules are suitable for this, or most popular? I am looking at HM-10, HM-11, RN4020 or RN4870 Bluetooth Modules. Are they fine, or are there other modules for this?

23 May 2018

Hello everybody, I work with a X-NUCLEO-IDB05A1 and I need a password to connect with my smartphone. It is normal ? I imported the heartrate program !

How can I remove this passwork ? or Do you have this passwork ?

Thank you very much for your help !

24 Jun 2018

Very nice example and explanation, thank you! :-)

18 Jul 2018

Johann Breit--Boudroit wrote:

Hello everybody, I work with a X-NUCLEO-IDB05A1 and I need a password to connect with my smartphone. It is normal ? I imported the heartrate program !

How can I remove this passwork ? or Do you have this passwork ?

Thank you very much for your help !

What app are you using to connect? I have had an HRM monitor app running for about a year with the IDB04A1 and the same application and with the Android nRF tools I can connect just fine. Perhaps it is something on your application side that is convinced it needs a PIN of some kind to bond to the device? I can read the GATT attributes just fine without bonding.

28 Nov 2018

Hi, Which part of the code is specific to the communication between the app and BLE Device ? Thanks

17 Jul 2019

Hello,

I'm using board MAX32630FTHR by Maxim Integrated. I would like to read data from on-board accelerometer and send them via BLE. I'm able to run the Heart-Rate example but I need to use a different "service" for my project.

Is there a UUID that can be good for me? How can i create a custom UUID? Is it complicated?

Thanks