workshop-nodeconfeu2018

Step 3 : Services and Web Bluetooth

So, we’ve seen a simple way to receive data from a Bluetooth device, but how do you transmit (or ‘push’) data to one? With Bluetooth LE, you need to have created a connection to a device to do that.

Bluetooth LE has some confusing terminology here:

Services and Characteristics

When you connect to a Bluetooth device, it advertises a bunch of ‘Services’ it provides. These are described by a UUID (either 16, 32 or 128 bit).

For instance a wireless lightbulb may provide a ‘light’ service and a ‘battery’ service.

Each Service has Characteristics (also described as a UUID), which are basically like variables that can be:

So for our wireless lightbulb example we might have:

You can have more than one of the same type of service or Characteristic, so for example our wireless lightbulb may have two lights, or two batteries.

These can be a nightmare to figure out when trying to connect to a device with code, so it really helps to use an app like nRF Connect (mentioned above) to really dig into them…

Nordic UART Service

The Bluetooth SIG defines a bunch of standard 16 bit UUIDs that can be used, however one thing it doesn’t define is a simple way to send character data.

This is probably intentional, to stop people using it for everything instead of defining services.

However that hasn’t stopped Nordic Semi (who make the chips we’re using) from defining their own standard, called ‘Nordic UART’, using the 128 bit UUIDs.

This consist of one Service, and two characteristics:

A note on UUIDs: While 128 bit UUIDs are 16 bytes, once a connection is established the UUID is remembered - as long as you create one ‘base’ UUID of the form 0000xxxx-0000-0000-0000-000000000000 and then change only the xxxx bit it’s pretty efficient - which is what Nordic have done above.

Espruino devices use a Nordic UART Service for their REPL - so for instance this is what you see looking at an Espruino in the nRF Connect app:

And with the service ‘expanded’:

Creating an HTTPS page

First, as a test that everything is working, we’re going to use a simple library for Espruino called puck.js, which makes it easy to send text over the Nordic UART Service. Then, we’ll move onto bare Web Bluetooth.

Since the REPL is available over the Nordic UART, all we’ll do is send JS commands followed by a newline. For instance 'LED1.set();\n' should turn the LED on.

For security reasons, Web Bluetooth will only work from a webpage served over HTTPS (not from HTTP, or a local file) and not from within an iframe, so we can’t just use something like jsfiddle

You may have your own arrangement (HTTPS on localhost) in which case use whatever works for you, but here are the steps to use GitHub:

<html>
 <head>
 </head>
 <body>
  <script src="https://www.puck-js.com/puck.js"></script>
  <button onclick="Puck.write('LED1.set();\n');">On!</button>
  <button onclick="Puck.write('LED1.reset();\n');">Off!</button>
 </body>
</html>

You’ll now have your own webpage at: https://your_username.github.io/PuckTest/test.html

Now…

More info about this is on the Espruino site.

Connecting from an Espruino device

You can connect from one Espruino device to another using a UART connection as well. Check out http://www.espruino.com/Puck.js+BLE+UART for some examples of doing that.

Step 4 - Web Bluetooth - Writing