Please welcome Nicole. Hello, everyone. My name is Nicole. I was running a little bit late today because I was so busy at work that I spent way too much of my free time playing around with all the new web technologies and browser API APIs and today, I'm here to take you through my journey with the web Bluetooth API.
So, browsers have been built about this idea that they would request data from a server, and they would render that data. So, browsers have always been great at rendering data. But it was always quite difficult to interact with devices that were right next to the browser. Now, with the whole movement of progressive web apps, with the project Fugu, we have seen a couple of new APIs, we have seen the web USB, web serial API, web HID API, and so on and so forth, and they all try to fill this gap between the browser and the peripheral devices.
Now, let's have a look at some basics. So, Bluetooth is a wireless technology standard to exchange data that uses the same frequency as wireless LAN, but it sends really small amounts of data over rather short distance. If you now think about this old and buggy and frustrating experience you had with your first smartphones or your first cell phones, I have very good news for you, because that was the old Bluetooth, Bluetooth 4.0, we also have BLE, which stands for Bluetooth Low Energy, and that's a completely new standard that was built specifically for IoT devices, so it's very energy efficient, and it also runs over quite a long distance. And with Web Bluetooth, we always talk about BLE, because that's a standard that was implemented.
Bluetooth normally works like this. You would have a central device which is the more capable device in terms of CPU and battery usage, and then you would have peripheral devices. Now, we also refer to them as the client and the server, and that's very important that you can connect one central device to multiple peripheral devices, but one peripheral device can only be connected to one central device at the same time. That's also the reason why, for example, you can't connect your headphones and your smart watch to the same smart phone, but you can't connect your headphones to two smart phones at the same time. That's just not possible. Now, as part of BLE, the Bluetooth special interest group introduced the GATT, the generic attribute profile, and that's basically the way how the peripheral device exposes its data to the client. You would have the GATT server that contains a list of GATT services, each service is then just a group of characteristics, and now, one characteristic identifies a value and it also defines what operations can be performed on that value.
For example, if you have a battery level, there it makes sense that you can read the battery level and you also want to be notified when the battery level changes on the device, but it doesn't make sense to make that writeable because you can't just override the battery level and then expect it to be magically 100 per cent if that would be the case, or our energy problems, climate change problems would be solved. But that's not. But if you have the flight direction of a drone, for example, there it does make sense or it's crucial that you can control the direction that you can't control your drone. It needs to be writeable. Now, a GATT value is a low-level data structure, so it's just an array of bytes. If you now look at the right side, here I have the implementation of a GTT server from the play bulb sphere which is a BLE light bulb, and we have the services, we have the characteristics, and that's very important to mention that services and characteristics are not really identified by their names, but by UUIDs. So there's the list of standard UUIDs maintained by the Bluetooth special interest group that cover common use cases like information about the device, or battery levels, so we do know that on each device, the characteristic, or let's say the service 180F, would be the battery service, but then we also have non-standard services like in that example the light service and the light characteristic, and there we can reach for either 128-bits, so long, unique UUIDs, or we can choose 16-bit UUIDs that are not yet specified, in that case we have FF0F for the light service, and FFFC for the characteristic. The tricky part is now, how are those values structured? The first two are pretty basic, that's basically just a UTF-8 encoded string. The battery level is the decimal representation of our byte, so 5a would be 90 per cent, and now for the custom characteristics, there we need to reach out for the documentation, and in most cases, there is no documentation, but in our case, we have documentation, so we do know that the play box here has four bytes, four LEDs inside the light bulb, and with the value of each byte, you can control the intensity of one of those LEDs, and the cool thing is now with red, green, and blue, we can basically create any colour that we have in the RGB colour space. So, now that we have the basics, let's have a look at the web API. So, the web API is pretty straightforward.
Comments