Hardware and software
To play with bluetooth, the first thing I needed was a bluetooth radio. Some simple beacon stuff can be done with the beacon toy app, I wanted to use a microcontroller, suitable for embedding into projects, as this was my eventual goal. There are plenty of good options on this front:
I went for the Adafruit nRF52, only because that was the first one that caught my eye. the micro:bit has several other onboard sensors, and is cheaper, so I may switch to that platform eventually.
To program the nRF52, i’m using the arduino toolchain, but since i’m a vim user, i’m editing primarily with vim, and only using the arduino IDE parts for the compilation. (I later switched to using the vim-arduino plugin, but i’ll talk about that setup separately).
There are also some mobile apps that are helpful here, notably nRF Connect, which is produced by Nordic Semiconductor, a major manufacturer of bluetooth chips. It has a variety of modes, and companion apps that i found indispensible for debugging and testing these examples.
First steps: Eddystone Beacon
My first goal was to just get an Eddystone beacon broadcasting, to direct an interested user to a web page. The code is nearly verbatim from the adafruit nRF52 example. it is important to note that the Eddystone protocol only allocates 17 bytes for the encoded URL. No error message is emitted when using a url that is too long. The best practice for avoiding problems here is to use a url shortener like goo.gl. This has the added benefit of letting you change the beacon’s destination without having to update the beacon device.
Next: Playbulb candle emulation
The big takeaway from this example is that i’ve now implemented entirely custom BLE services on the device, with read-only and read-write characteristics. These can serve as the basis for any custom services I build later.
Nordic Uart Service: not-exactly-standard, but close enough.
Chronologically, this project was the second one i built, not the third. but logically it makes more sense here.
In my research about bluetooth LE services, i kept seeing references to the Nordic Uart Service (NUS). This is a service common to many of the chips from Nordic Semiconductor, that emulates a standard bluetooth UART connection over BLE.
While the nRF52 i’m using is made by Nordic, it does not have built-in support for this service, so I decided to build a simple Echo service on the NUS protocol. A later addition can interpret commands delivered over this link, to perform actions. See the terminal echo sketch for the details here. I was now able to use nRF Connect mobile app to connect to the device over NUS, and send/receive text.
Well, now that i’ve mastered beacons, created custom services and characteristics, and exchanged simple text commands over BLE, its time to build something bigger! Maybe Zork over ble, or multi-player bluetooth hungry-hungry-hippos. I’m not sure exactly what’s next, but stay tuned to find out!