Bluetooth && Azure IoT hub == IoT
Engström, Jimmy
The market totally explodes with new gadgets that can connect to tablets, pc's and phones. In 2015 alone there were 3 billion Bluetooth devices manufactured. In this session I will show how you can take advantage of that and how to communicate with devices that are Bluetooth enabled. I will also show how to figure out undocumented devices and how to use Azure IoT hub to turn them into IoT devices.
 Good morning. I'm so happy to see so many of you here this early in the morning. Thanks for joining me. So, as developers, we tend to go through different stages. My first stage was when I was about seven or eight years old. I got my first computer and I remember I sat down and wrote 10, print Jimmy, 20, go to 10. That was my code. I got my computer to do stuff for me. This is actually my first app. And if anyone wants a source code, I will be happy to share it. This is actually the moment when I realized that I wanted to be a developer. This is what I wanted to do with my life. The next stage was when I got a calculator and a mobile phone. There's a very special feeling when you're writing something on your computer and then you deploy it to something smaller that you can bring with you in your pocket. I could actually sit down and write code on my calculated during class. Mind blowing for me. The third stage was when I was introduced to circuit boards, Raspberry Pi, Netduino, Arduino and stuff like that. I remember I went into the living room and I gel to my wife, Jessica, Jessica, you got to come and see this. The LED is blinking. Look, the LED, it's blinking. On, off, on, see, it's blinking. And she just went into the room, yeah, what about it? You don't understand. The LED here on the circuit board is blinking. I made that happen. I think it was a bigger moment for me than for her. But I was so happy that it was blinking. Then the next stage was when I realized that I can control other devices. Devices that I actually don't have any code control over. I can get measurements from sensors. I can turn off and on lights. And this is what we're going to talk about today. Bluetooth, how we can control devices through Bluetooth. But why should we care about this? Well, first of all, it's really, really fun. And the second reason is this number. Three billion Bluetooth devices were manufactured last year. Of course we want to be here as app developers. Make apps for this customer base. So what we're going to talk about today is how does Bluetooth low energy work? How can we figure out a Bluetooth protocol? And then we're going to go from Bluetooth low energy all the way to IoT. Let's see if that works. So my name is Jim Engstrom and I work as an ASP.NET developer. I spend my spare time doing Windows and some HoloLens. And together with my wife, I run a user group called coding off the work and a podcast for the same name. I'm also a Windows development MVP. But enough about me. Let's dig deeper into this fun stuff. So Bluetooth low energy has many names. It's called Bluetooth smart. It's called, see, there we go. It's called Bluetooth LE, Bluetooth low energy, BLE. And sometimes, wrongly, I should say, Bluetooth 4.0. It's part of the Bluetooth 4.0 standards. But it's not the whole thing. Bluetooth low energy was actually introduced back in 2006 by Nokia under the name Vibri or Vibri, however you want to pronounce it. And then 2010, it became part of the Bluetooth core 4.0 standard. The nice thing about Bluetooth LE is that you can actually ask the device, what can you do? What services are you implementing? That's perfect for developers. It's easy to reverse engineer to figure out the protocols. So a BLE device can tell you what services, what service it has. A BLE device always has one service or many. Must have one. The service is identified by GWT. So there's an organization called Bluetooth SIG, special interest group that has named a couple of these services. They have documented them and they have said that to be able to use this GWT, this service needs to look a particular way. There are also, of course, services that are not defined by Bluetooth SIG and we will go into those later on. One service has one characteristic or more. Must have one. You can think of them like methods or events. Because every characteristic has a way to access them. You can read, you can write, you can indicate and you can notify. And the last two of them are, you can see them as events. So those are, your device is going to send those values back. So this is the documentation from the Bluetooth SIG, the special interest group, for the battery service. So their documentation says that to be implement a battery service, you need to have a characteristic called battery level. It must have read access. It wouldn't be much of a use if you couldn't read the battery level. And then it may have notify. Doesn't have to, but it could. So let's look at this service in another way. So we have the BLE device. It has a battery service. It has a battery level characteristic and it must have read and it may have notify. The really cool part of this is that you can actually build generic apps. You can say that my app wants to connect to any device that implements the battery service. So you don't have to write specific app for a specific device. So now when we know how to communicate or how they work, how do we talk to the device? Well, the first step is to pair the device. You can do that from within Windows and you can do it programmatically. They're actually working on, now I'm talking Windows 10 specifically, they're actually working on pairless Bluetooth as well, but they're not there yet. So you just search for Bluetooth settings, you click your device and you click pair. Sometimes you get this window. Usually it's 0, 0, 0, 0, 1, 2, 3, 4, something like that or you can probably find it in the documentation for the device. Or you can do as I did. I just try 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2 until you got it. Luckily it's what 0, 0, 0, 0, 7, so you don't have to do that for long. So if you want to do this programmatically, you set up a device watcher. You can request properties that you want to have access to. So in my case, I want a device address and I want to know if it's connected. Then you create a watcher and you supply it with a filter. That's those three dots. We will look at the filter on the next slide. And you supply it with the request properties. What properties do I want from it and what kind of endpoint am I looking for? Then you just add the added and updated event handlers and just start to watch it. And the filter looks something like this. You can find this at MSDN or something like that. So you don't have to write it down or anything. Then we have the added event. With the event handler. In my case, I'm checking the name. I don't have to check the name since I'm checking for services. But in some cases, they reuse the services. They reuse the grid. So it could be a good thing to actually check the name if it's just a particular set of device you want to use. And then if it is the right one, you just pair it. You can also use the pairing.custom.pairersync if you want to do some special pairing code. So I found this scale on Net-on-Net. This is an Anderson scale, but it's actually SenSun who makes them. So I've written a small application that lists all the services, all their characteristics, all the ways that they can be accessed because the device knows these things and are happy to share them with you. I also created a collection or a database that converts these services to their Bluetooth SIG real names, if you want. So if you look at the SenSun scale, it contains generic access. This service will contain things like name and device address. Then we have the generic attribute. This is the service that actually makes it possible to ask the device what can you do. Then we have device information. It contains manufacturer, versions, name of the device, name and stuff like that. Then we have another one, not defined by the Bluetooth SIG. Interesting. So the one, I know that generic access, generic attribute and device information won't give me a weight value because these are defined by the Bluetooth SIG. They don't have a get weight. So the one named 0000FFBs, I'm sorry, I'm not going to let you suffer through that. So the last one is the one I want. So looking at our master's service, it has two characteristics. They both have a way to access them. In this case, I can write to it and I can get a notification from both of them. So I wrote this very simple application that listened to those notifications. And from one of the services, I actually got a reply. So probably that one. So now the fun part starts, the detective work. So I get 10 bytes back. It looks like this. So I started off with a baseline, zero grams, not putting anything on the scale. So I'm guessing that I'm actually looking for bytes that are zero. Those ones. Then I try to put an energy drink on my scale. Just happened to be next to me. It weighed 235 grams. Now, this value changed. I actually have 235 on byte 5. So I'm guessing that's the weight value. So now we're set the scale. Remove the energy drink. And I got minus 235. Now bytes 7 and 9 changed. But byte 5 is still 235. So I'm guessing 7 or 9 is a sign byte. Then I tried with 120 grams. Suddenly byte 4 changed. So after testing different weights, I managed to figure out that bytes I wanted to talk to or read from was 4, 5, and 7. And the formula ended up being like this. So if I take the byte 4 times 256 plus byte 5, that's going to give me the weight. And then I can use byte 7 to figure out what sign it is. So 0 for positive minus is 1 for negative. So my first demo is going to be a legend. Wait for it. Dairy. I'm sorry, I really like puns. So let's take a look at the code. So let's start with, I've actually written a couple of helper methods. So the first one is set up notify a sync. To get notifications, I have to tell my Bluetooth characteristic that, hey, I want notifications from you. So what I do is the first thing is that I get the service with this particular grid. Then I get the characteristic with a particular grid. And then I write client characteristic configuration, the script or a sync, the longest method name ever. And I say that I'm interested in notifications. And this method is going to return the characteristics to me when it's done. When I also have a read value, the same thing here. I get the service, I get the characteristic, I read the value, and I say I want it uncached. Because sometimes the device will cache it for me. So that's good if you want to have low battery consumption. But in this case, I want the latest value. When everything goes well, I get the data. I feel I get an array, byte rate. And in my case, I have made this method generic. So I can actually say that I want to string back. But the Bluetooth device is always going to return bytes to me. In this case, I'm just converted. I'm guessing it's UTF-8. That's not a given. And then I just return the bytes. And then we have write value. Same thing here. Get the service, get the characteristic. And here I'm actually asking, okay, what kind of way can I write to you? Can I write with the response? Sorry, without response? Or can I write with the response? And depending on what it can do, I just implement that. So I just write value async. Again, bytes, always bytes. So in our application, I define the service ID. This is our mystery service. Then we have the characteristic, characteristic squid. Then I create a device selector from squid. So this is going to give me all the devices that implement this service. I get a collection and I just loop through them. I get the Bluetooth LE device. And, well, here I check, does the name contain sense on? Again, I don't have to do this if I know that all the devices that implement this particular service going to use the same characteristics. But just to be safe. Then if I get a device, I run the method set up notify async. So I say to the service, I want notifications. I get the characteristics back. And then I just listen to the value change event. In the value change event, I get an array back. An array of bytes. So I send it into my convert to wait method and just set the property I have on my page to the new wait. And the wait convert to wait method is doing, well, it's going to check whether or not byte 7 is set or not. And it's going to take the sign times byte number 4, byte shift, shifted, and then byte 5. So let's see if this works. I have an energy drink, put it on the scale, and the value changes. Pretty simple, huh? I mean, this is not a lot of lines of code to do this. So let's go back to the presentation. So while we is a company who makes robots, toy robots, and they release a whole bunch of robots. This is the one to your left is the MIP. It's a Bluetooth robot. That one is the MIPO sour. I actually written an SDK to talk to these two. Actually, the one for MIP is out already as open source. I'll share the link later on. And the MIPO sour is coming. They also took a lot of old toys. These robots were controlled by remote control. But they upgraded them. So now they can be controlled by Bluetooth. So let's talk a little bit about the Azure IoT Hub. Sorry, Microsoft has released the Azure IoT Hub, a way to communicate with IoT devices. So what I could have done here is that I could have shown how to upload sensor data to the cloud and do stuff with it. That's no fun. I want to do something more fun. So we're going to try to control one of these robots through the Azure IoT Hub. So the first thing we need to do is actually set up the IoT Hub. I've done that in advance, so it's a little bit quicker to do this with slides. So the first step is to click new. So this is the Azure portal. The next step is to click Internet of Things and then Azure IoT Hub. So let's name this NDC. I have chosen to use the free price and scale tier, and I've chosen location in Northern Europe. Just create and then you wait a couple of minutes. Then you will see this screen, and your IoT Hub is ready for use. The free tier actually gives you 8,000 messages that you can send through the IoT Hub for free. That's awesome. The next step is to install the device explorer. It can be found on this URL. You can download a source for it, or you can download a full installable. It looks something like this. So to be able to use the device explorer, you need a connection string. So let's go back to the Azure portal. Click the key, then IoT Hub owner. This is created for you. So just click away, and then you have the connection string just there. Copy it and paste it into your device explorer, and you're all done. So now we can go to the management tab and click add. So we could have done this from code, but since we're only going to use one device, this will just, this will work perfectly. So I'll name this RoboSapien, and I just press create, and I get it nicely in my list. Then to create your application, I have chosen to create a universal Windows platform app. This will, of course, work with any app. So the first thing you need to do is to install the Microsoft Azure device client Nougat package. Once that's done, you can go to this URL. Microsoft actually helps us even more with this. So this is not going to be an Azure IoT Hub deep dive. I want to open your eyes to see how cool and how easy it is to communicate through the IoT Hub with Bluetooth devices. So the first thing, you go to this portal, you click connect your device. You will see a list of some of the devices you can connect to the IoT Hub. I have chosen use another device and Windows. And Microsoft is actually going to provide you with all the code you need. How nice is this? Just copy and paste it into your application, and you're done. You also need a connection string to your device. So that's the same connection string you can find in the Azure portal, but you need to add a device ID. But the device explorer actually gives you the correct connection string. So you can just copy it from there. So let's see. Bluetooth Internet of Things robot. That's a really boring name. It should be called Bluetooth Robiot, right? Of course. So there we go. Much better. Right. We're about to demo something. Now, actors know that they should never work with children or animals. As a speaker, you know that you should never work with Bluetooth that can be disturbed by 1,000 people in the same area than you. We also know that we should never rely on Internet. So that's exactly what I've done. I'm relying on Internet and Bluetooth. Okay. So let's just hope this works. There we go. So this robot actually has a bug in it. So I have to pair it every time I use it. This is made for parallel communication, but it will work, hopefully. I'm actually talking to Huawei trying to solve this. So here I just, I went into the Bluetooth settings and I paired the device. Let's go into messages. I'm going to show you the code in a while. So let's try, let's do an explosive demo. C4. Sorry, I need to start the app as well. There we go. Let's try that again. So now I'm actually controlling my robot through the device explorer into IoT Hub, then back to my application and then over to the robot. So let's try something else. See, I actually have a list of things it can do. So let's try a seven. So all the commands that is available through the infrared controller is also available through Bluetooth. So let's take a look at the code. So what I've done here is that I start, I create a device client. I create it from my connection string. This is the connection string to my device. Then I have a robot sapien class. This is the class that talks to the robot sapien. And we will look at that in just a second. And then I say robot sapien connect. This is going to try to find the device and connect to it. And then I also have a send event. So the send event is going to send robot sapien connected message to my IoT Hub. And if you look at the device explorer, let's see. If I monitor it and I start it. So if I start the app, it will connect to the robot sapien and I will get the message robot sapien connected if I monitor the Hub. And then I have received commands. So this is going to check, do I have any messages on my Hub? If I have a message, I will convert it, I will get bytes. I will convert it to a message. And then I will call the send command on my robot sapien and send in the message. So in this case, I'm going to get the bytes for C4, for example. Then I'm going to convert it to text and send it in to my robot sapien. So let's take a look at the robot sapien class. So here I'm actually not using my helper methods just to show how easy it is. So first thing, get the service quit. Get the characteristics quit. Then I connect. I try to find the device selector. So I try to get all the devices that implement this service. And one way is actually one of the examples that reduce their service quits. So I have another robot that does the, that works completely differently, has different characteristics but the same service ID. I actually think it has the same characteristics ID as well. So I get the enumerator and I get the device. Here I check does it contain robot sapien because the other robot is called MIP. I get the service and I get the characteristic. Then we have the send command. So here just take the characteristic and write it, write the data straight to the characteristic. I also have a method here that converts my C4 string hex to a byte array. That's all it takes. So I can send commands up to the IT hub. Then it will return to my application or actually every application that listens to that particular device. And then it would just send the bytes out to robot. So here are some resources. Microsoft actually has a lot of great samples on their GitHub account. We have the Azure, sorry, the device explorer. And then we have the URL to my MIP WinRT SDK. Do we have any questions? I'm soon done. Thank you. Do we have any questions? No questions. So then let me round up just to say that as I mentioned before, there are three billion devices out there that were manufactured last year alone. It is so much fun to do this. And if you have any questions about this presentation or Windows development in general, or if you ever in Sweden, please come by coding after work. So I'm kind of ahead of schedule, so I'm going to let you go. So please contact me if you want to. Thank you.
 Good morning. I'm so happy to see so many of you here this early in the morning. Thanks  for joining me. So, as developers, we tend to go through different stages. My first  stage was when I was about seven or eight years old. I got my first computer and I  remember I sat down and wrote 10, print Jimmy, 20, go to 10. That was my code. I got my computer  to do stuff for me. This is actually my first app. And if anyone wants a source code, I  will be happy to share it. This is actually the moment when I realized that I wanted  to be a developer. This is what I wanted to do with my life. The next stage was when I  got a calculator and a mobile phone. There's a very special feeling when you're writing  something on your computer and then you deploy it to something smaller that you can bring  with you in your pocket. I could actually sit down and write code on my calculated during  class. Mind blowing for me. The third stage was when I was introduced to circuit boards,  Raspberry Pi, Netduino, Arduino and stuff like that. I remember I went into the living  room and I gel to my wife, Jessica, Jessica, you got to come and see this. The LED is  blinking. Look, the LED, it's blinking. On, off, on, see, it's blinking. And she just  went into the room, yeah, what about it? You don't understand. The LED here on the circuit  board is blinking. I made that happen. I think it was a bigger moment for me than for her.  But I was so happy that it was blinking. Then the next stage was when I realized that I  can control other devices. Devices that I actually don't have any code control over. I can get  measurements from sensors. I can turn off and on lights. And this is what we're going  to talk about today. Bluetooth, how we can control devices through Bluetooth. But why  should we care about this? Well, first of all, it's really, really fun. And the second  reason is this number. Three billion Bluetooth devices were manufactured last year. Of course  we want to be here as app developers. Make apps for this customer base. So what we're  going to talk about today is how does Bluetooth low energy work? How can we figure out a Bluetooth  protocol? And then we're going to go from Bluetooth low energy all the way to IoT. Let's  see if that works. So my name is Jim Engstrom and I work as an ASP.NET developer. I spend  my spare time doing Windows and some HoloLens. And together with my wife, I run a user group  called coding off the work and a podcast for the same name. I'm also a Windows development  MVP. But enough about me. Let's dig deeper into this fun stuff. So Bluetooth low energy  has many names. It's called Bluetooth smart. It's called, see, there we go. It's called  Bluetooth LE, Bluetooth low energy, BLE. And sometimes, wrongly, I should say, Bluetooth  4.0. It's part of the Bluetooth 4.0 standards. But it's not the whole thing. Bluetooth low  energy was actually introduced back in 2006 by Nokia under the name Vibri or Vibri, however  you want to pronounce it. And then 2010, it became part of the Bluetooth core 4.0 standard.  The nice thing about Bluetooth LE is that you can actually ask the device, what can  you do? What services are you implementing? That's perfect for developers. It's easy to  reverse engineer to figure out the protocols. So a BLE device can tell you what services,  what service it has. A BLE device always has one service or many. Must have one. The service  is identified by GWT. So there's an organization called Bluetooth SIG, special interest group  that has named a couple of these services. They have documented them and they have said  that to be able to use this GWT, this service needs to look a particular way. There are  also, of course, services that are not defined by Bluetooth SIG and we will go into those  later on. One service has one characteristic or more. Must have one. You can think of them  like methods or events. Because every characteristic has a way to access them. You can read, you  can write, you can indicate and you can notify. And the last two of them are, you can see  them as events. So those are, your device is going to send those values back. So this  is the documentation from the Bluetooth SIG, the special interest group, for the battery  service. So their documentation says that to be implement a battery service, you need  to have a characteristic called battery level. It must have read access. It wouldn't be much  of a use if you couldn't read the battery level. And then it may have notify. Doesn't  have to, but it could. So let's look at this service in another way. So we have the BLE  device. It has a battery service. It has a battery level characteristic and it must have  read and it may have notify. The really cool part of this is that you can actually build  generic apps. You can say that my app wants to connect to any device that implements the  battery service. So you don't have to write specific app for a specific device. So now  when we know how to communicate or how they work, how do we talk to the device? Well,  the first step is to pair the device. You can do that from within Windows and you can  do it programmatically. They're actually working on, now I'm talking Windows 10 specifically,  they're actually working on pairless Bluetooth as well, but they're not there yet. So you  just search for Bluetooth settings, you click your device and you click pair. Sometimes you  get this window. Usually it's 0, 0, 0, 0, 1, 2, 3, 4, something like that or you can  probably find it in the documentation for the device. Or you can do as I did. I just  try 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2 until you got it. Luckily it's what  0, 0, 0, 0, 7, so you don't have to do that for long. So if you want to do this programmatically,  you set up a device watcher. You can request properties that you want to have access to.  So in my case, I want a device address and I want to know if it's connected. Then you  create a watcher and you supply it with a filter. That's those three dots. We will look  at the filter on the next slide. And you supply it with the request properties. What properties  do I want from it and what kind of endpoint am I looking for? Then you just add the added  and updated event handlers and just start to watch it. And the filter looks something  like this. You can find this at MSDN or something like that. So you don't have to write it down  or anything. Then we have the added event. With the event handler. In my case, I'm checking  the name. I don't have to check the name since I'm checking for services. But in some cases,  they reuse the services. They reuse the grid. So it could be a good thing to actually check  the name if it's just a particular set of device you want to use. And then if it is  the right one, you just pair it. You can also use the pairing.custom.pairersync if you want  to do some special pairing code. So I found this scale on Net-on-Net. This is an Anderson  scale, but it's actually SenSun who makes them. So I've written a small application  that lists all the services, all their characteristics, all the ways that they can be accessed because  the device knows these things and are happy to share them with you. I also created a collection  or a database that converts these services to their Bluetooth SIG real names, if you  want. So if you look at the SenSun scale, it contains generic access. This service will  contain things like name and device address. Then we have the generic attribute. This is  the service that actually makes it possible to ask the device what can you do. Then we  have device information. It contains manufacturer, versions, name of the device, name and stuff  like that. Then we have another one, not defined by the Bluetooth SIG. Interesting. So the  one, I know that generic access, generic attribute and device information won't give me a weight  value because these are defined by the Bluetooth SIG. They don't have a get weight. So the one  named 0000FFBs, I'm sorry, I'm not going to let you suffer through that. So the last  one is the one I want. So looking at our master's service, it has two characteristics. They  both have a way to access them. In this case, I can write to it and I can get a notification  from both of them. So I wrote this very simple application that listened to those notifications.  And from one of the services, I actually got a reply. So probably that one. So now the  fun part starts, the detective work. So I get 10 bytes back. It looks like this. So I started  off with a baseline, zero grams, not putting anything on the scale. So I'm guessing that  I'm actually looking for bytes that are zero. Those ones. Then I try to put an energy drink  on my scale. Just happened to be next to me. It weighed 235 grams. Now, this value changed.  I actually have 235 on byte 5. So I'm guessing that's the weight value. So now we're set  the scale. Remove the energy drink. And I got minus 235. Now bytes 7 and 9 changed. But  byte 5 is still 235. So I'm guessing 7 or 9 is a sign byte. Then I tried with 120 grams.  Suddenly byte 4 changed. So after testing different weights, I managed to figure out  that bytes I wanted to talk to or read from was 4, 5, and 7. And the formula ended up  being like this. So if I take the byte 4 times 256 plus byte 5, that's going to give me the  weight. And then I can use byte 7 to figure out what sign it is. So 0 for positive minus  is 1 for negative. So my first demo is going to be a legend. Wait for it. Dairy. I'm sorry,  I really like puns. So let's take a look at the code. So let's start with, I've actually  written a couple of helper methods. So the first one is set up notify a sync. To get  notifications, I have to tell my Bluetooth characteristic that, hey, I want notifications  from you. So what I do is the first thing is that I get the service with this particular  grid. Then I get the characteristic with a particular grid. And then I write client  characteristic configuration, the script or a sync, the longest method name ever. And  I say that I'm interested in notifications. And this method is going to return the characteristics  to me when it's done. When I also have a read value, the same thing here. I get the service,  I get the characteristic, I read the value, and I say I want it uncached. Because sometimes  the device will cache it for me. So that's good if you want to have low battery consumption.  But in this case, I want the latest value. When everything goes well, I get the data.  I feel I get an array, byte rate. And in my case, I have made this method generic. So  I can actually say that I want to string back. But the Bluetooth device is always going to  return bytes to me. In this case, I'm just converted. I'm guessing it's UTF-8. That's  not a given. And then I just return the bytes. And then we have write value. Same thing here.  Get the service, get the characteristic. And here I'm actually asking, okay, what kind  of way can I write to you? Can I write with the response? Sorry, without response? Or  can I write with the response? And depending on what it can do, I just implement that.  So I just write value async. Again, bytes, always bytes. So in our application, I define  the service ID. This is our mystery service. Then we have the characteristic, characteristic  squid. Then I create a device selector from squid. So this is going to give me all the  devices that implement this service. I get a collection and I just loop through them.  I get the Bluetooth LE device. And, well, here I check, does the name contain  sense on? Again, I don't have to do this if I know that all the devices that implement  this particular service going to use the same characteristics. But just to be safe. Then  if I get a device, I run the method set up notify async. So I say to the service, I want  notifications. I get the characteristics back. And then I just listen to the value change  event. In the value change event, I get an array back. An array of bytes. So I send it  into my convert to wait method and just set the property I have on my page to the new  wait. And the wait convert to wait method is doing, well, it's going to check whether  or not byte 7 is set or not. And it's going to take the sign times byte number 4, byte  shift, shifted, and then byte 5. So let's see if this works. I have an energy drink,  put it on the scale, and the value changes. Pretty simple, huh? I mean, this is not a  lot of lines of code to do this. So let's go back to the presentation. So while we is  a company who makes robots, toy robots, and they release a whole bunch of robots. This  is the one to your left is the MIP. It's a Bluetooth robot. That one is the MIPO sour.  I actually written an SDK to talk to these two. Actually, the one for MIP is out already  as open source. I'll share the link later on. And the MIPO sour is coming. They also took  a lot of old toys. These robots were controlled by remote control. But they upgraded them.  So now they can be controlled by Bluetooth. So let's talk a little bit about the Azure  IoT Hub. Sorry, Microsoft has released the Azure IoT Hub, a way to communicate with  IoT devices. So what I could have done here is that I could have shown how to upload sensor  data to the cloud and do stuff with it. That's no fun. I want to do something more fun. So  we're going to try to control one of these robots through the Azure IoT Hub. So the  first thing we need to do is actually set up the IoT Hub. I've done that in advance,  so it's a little bit quicker to do this with slides. So the first step is to click new.  So this is the Azure portal. The next step is to click Internet of Things and then Azure  IoT Hub. So let's name this NDC. I have chosen to use the free price and scale tier, and  I've chosen location in Northern Europe. Just create and then you wait a couple of minutes.  Then you will see this screen, and your IoT Hub is ready for use. The free tier actually  gives you 8,000 messages that you can send through the IoT Hub for free. That's awesome.  The next step is to install the device explorer. It can be found on this URL. You can download  a source for it, or you can download a full installable. It looks something like this.  So to be able to use the device explorer, you need a connection string. So let's go  back to the Azure portal. Click the key, then IoT Hub owner. This is created for you. So  just click away, and then you have the connection string just there. Copy it and paste it into  your device explorer, and you're all done. So now we can go to the management tab and  click add. So we could have done this from code, but since we're only going to use one  device, this will just, this will work perfectly. So I'll name this RoboSapien, and I just press  create, and I get it nicely in my list. Then to create your application, I have chosen to  create a universal Windows platform app. This will, of course, work with any app. So the  first thing you need to do is to install the Microsoft Azure device client Nougat package.  Once that's done, you can go to this URL. Microsoft actually helps us even more with  this. So this is not going to be an Azure IoT Hub deep dive. I want to open your eyes  to see how cool and how easy it is to communicate through the IoT Hub with Bluetooth devices.  So the first thing, you go to this portal, you click connect your device. You will see  a list of some of the devices you can connect to the IoT Hub. I have chosen use another  device and Windows. And Microsoft is actually going to provide you with all the code you  need. How nice is this? Just copy and paste it into your application, and you're done.  You also need a connection string to your device. So that's the same connection string  you can find in the Azure portal, but you need to add a device ID. But the device explorer  actually gives you the correct connection string. So you can just copy it from there.  So let's see. Bluetooth Internet of Things robot. That's a really boring name. It should  be called Bluetooth Robiot, right? Of course. So there we go. Much better. Right. We're  about to demo something. Now, actors know that they should never work with children  or animals. As a speaker, you know that you should never work with Bluetooth that can  be disturbed by 1,000 people in the same area than you. We also know that we should never  rely on Internet. So that's exactly what I've done. I'm relying on Internet and Bluetooth.  Okay. So let's just hope this works. There we go. So this robot actually has a bug in  it. So I have to pair it every time I use it. This is made for parallel communication,  but it will work, hopefully. I'm actually talking to Huawei trying to solve this. So here I  just, I went into the Bluetooth settings and I paired the device.  Let's go into messages. I'm going to show you the code in a while. So let's try, let's  do an explosive demo. C4. Sorry, I need to start the app as well. There we go. Let's  try that again. So now I'm actually controlling my robot through the device explorer into  IoT Hub, then back to my application and then over to the robot. So let's try something  else. See, I actually have a list of things it can do. So let's try a seven. So all the  commands that is available through the infrared controller is also available through Bluetooth.  So let's take a look at the code. So what I've done here is that I start, I create a device  client. I create it from my connection string. This is the connection string to my device.  Then I have a robot sapien class. This is the class that talks to the robot sapien. And  we will look at that in just a second. And then I say robot sapien connect. This is going  to try to find the device and connect to it. And then I also have a send event. So the  send event is going to send robot sapien connected message to my IoT Hub. And if you look at  the device explorer, let's see. If I monitor it and I start it. So if I start the app,  it will connect to the robot sapien and I will get the message robot sapien connected  if I monitor the Hub. And then I have received commands. So this is going to check, do I  have any messages on my Hub? If I have a message, I will convert it, I will get bytes. I will  convert it to a message. And then I will call the send command on my robot sapien and send  in the message. So in this case, I'm going to get the bytes for C4, for example. Then  I'm going to convert it to text and send it in to my robot sapien. So let's take a look  at the robot sapien class. So here I'm actually not using my helper methods just to show how  easy it is. So first thing, get the service quit. Get the characteristics quit. Then I  connect. I try to find the device selector. So I try to get all the devices that implement  this service. And one way is actually one of the examples that reduce their service  quits. So I have another robot that does the, that works completely differently, has different  characteristics but the same service ID. I actually think it has the same characteristics  ID as well. So I get the enumerator and I get the device. Here I check does it contain  robot sapien because the other robot is called MIP. I get the service and I get the characteristic.  Then we have the send command. So here just take the characteristic and write it, write  the data straight to the characteristic. I also have a method here that converts my C4  string hex to a byte array. That's all it takes. So I can send commands up to the IT  hub. Then it will return to my application or actually every application that listens  to that particular device. And then it would just send the bytes out to robot. So here are  some resources. Microsoft actually has a lot of great samples on their GitHub account.  We have the Azure, sorry, the device explorer. And then we have the URL to my MIP WinRT SDK.  Do we have any questions? I'm soon done. Thank you. Do we have any questions? No questions.  So then let me round up just to say that as I mentioned before, there are three billion  devices out there that were manufactured last year alone. It is so much fun to do this.  And if you have any questions about this presentation or Windows development in general, or if you  ever in Sweden, please come by coding after work. So I'm kind of ahead of schedule, so  I'm going to let you go. So please contact me if you want to. Thank you.
Bluetooth_Azure_IoT_hub_IoT_Or_how_to_rule_0_59.png Bluetooth_Azure_IoT_hub_IoT_Or_how_to_rule_60_107.png Bluetooth_Azure_IoT_hub_IoT_Or_how_to_rule_108_2099.png Bluetooth_Azure_IoT_hub_IoT_Or_how_to_rule_2100_11915.png Bluetooth_Azure_IoT_hub_IoT_Or_how_to_rule_11916_12539.png Bluetooth_Azure_IoT_hub_IoT_Or_how_to_rule_12540_12743.png Bluetooth_Azure_IoT_hub_IoT_Or_how_to_rule_12744_15203.png Bluetooth_Azure_IoT_hub_IoT_Or_how_to_rule_15204_16475.png Bluetooth_Azure_IoT_hub_IoT_Or_how_to_rule_16476_34091.png Bluetooth_Azure_IoT_hub_IoT_Or_how_to_rule_34092_37475.png Bluetooth_Azure_IoT_hub_IoT_Or_how_to_rule_37476_37919.png Bluetooth_Azure_IoT_hub_IoT_Or_how_to_rule_37920_38879.png Bluetooth_Azure_IoT_hub_IoT_Or_how_to_rule_38880_40739.png Bluetooth_Azure_IoT_hub_IoT_Or_how_to_rule_40740_41363.png Bluetooth_Azure_IoT_hub_IoT_Or_how_to_rule_41364_54891.png
Good morning. I'm so happy to see so many of you here this early in the morning. Thanks for joining me. So, as developers, we tend to go through different stages. My first stage was when I was about seven or eight years old. I got my first computer and I remember I sat down and wrote 10, print Jimmy, 20, go to 10. That was my code. I got my computer to do stuff for me. This is actually my first app. And if anyone wants a source code, I will be happy to share it. This is actually the moment when I realized that I wanted to be a developer. This is what I wanted to do with my life. The next stage was when I got a calculator and a mobile phone. There's a very special feeling when you're writing something on your computer and then you deploy it to something smaller that you can bring with you in your pocket. I could actually sit down and write code on my calculated during class. Mind blowing for me. The third stage was when I was introduced to circuit boards, Raspberry Pi, Netduino, Arduino and stuff like that. I remember I went into the living room and I gel to my wife, Jessica, Jessica, you got to come and see this. The LED is blinking. Look, the LED, it's blinking. On, off, on, see, it's blinking. And she just went into the room, yeah, what about it? You don't understand. The LED here on the circuit board is blinking. I made that happen. I think it was a bigger moment for me than for her. But I was so happy that it was blinking. Then the next stage was when I realized that I can control other devices. Devices that I actually don't have any code control over. I can get measurements from sensors. I can turn off and on lights. And this is what we're going to talk about today. Bluetooth, how we can control devices through Bluetooth. But why should we care about this? Well, first of all, it's really, really fun. And the second reason is this number. Three billion Bluetooth devices were manufactured last year. Of course we want to be here as app developers. Make apps for this customer base. So what we're going to talk about today is how does Bluetooth low energy work? How can we figure out a Bluetooth protocol? And then we're going to go from Bluetooth low energy all the way to IoT. Let's see if that works. So my name is Jim Engstrom and I work as an ASP.NET developer. I spend my spare time doing Windows and some HoloLens. And together with my wife, I run a user group called coding off the work and a podcast for the same name. I'm also a Windows development MVP. But enough about me. Let's dig deeper into this fun stuff. So Bluetooth low energy has many names. It's called Bluetooth smart. It's called, see, there we go. It's called Bluetooth LE, Bluetooth low energy, BLE. And sometimes, wrongly, I should say, Bluetooth 4.0. It's part of the Bluetooth 4.0 standards. But it's not the whole thing. Bluetooth low energy was actually introduced back in 2006 by Nokia under the name Vibri or Vibri, however you want to pronounce it. And then 2010, it became part of the Bluetooth core 4.0 standard. The nice thing about Bluetooth LE is that you can actually ask the device, what can you do? What services are you implementing? That's perfect for developers. It's easy to reverse engineer to figure out the protocols. So a BLE device can tell you what services, what service it has. A BLE device always has one service or many. Must have one. The service is identified by GWT. So there's an organization called Bluetooth SIG, special interest group that has named a couple of these services. They have documented them and they have said that to be able to use this GWT, this service needs to look a particular way. There are also, of course, services that are not defined by Bluetooth SIG and we will go into those later on. One service has one characteristic or more. Must have one. You can think of them like methods or events. Because every characteristic has a way to access them. You can read, you can write, you can indicate and you can notify. And the last two of them are, you can see them as events. So those are, your device is going to send those values back. So this is the documentation from the Bluetooth SIG, the special interest group, for the battery service. So their documentation says that to be implement a battery service, you need to have a characteristic called battery level. It must have read access. It wouldn't be much of a use if you couldn't read the battery level. And then it may have notify. Doesn't have to, but it could. So let's look at this service in another way. So we have the BLE device. It has a battery service. It has a battery level characteristic and it must have read and it may have notify. The really cool part of this is that you can actually build generic apps. You can say that my app wants to connect to any device that implements the battery service. So you don't have to write specific app for a specific device. So now when we know how to communicate or how they work, how do we talk to the device? Well, the first step is to pair the device. You can do that from within Windows and you can do it programmatically. They're actually working on, now I'm talking Windows 10 specifically, they're actually working on pairless Bluetooth as well, but they're not there yet. So you just search for Bluetooth settings, you click your device and you click pair. Sometimes you get this window. Usually it's 0, 0, 0, 0, 1, 2, 3, 4, something like that or you can probably find it in the documentation for the device. Or you can do as I did. I just try 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2 until you got it. Luckily it's what 0, 0, 0, 0, 7, so you don't have to do that for long. So if you want to do this programmatically, you set up a device watcher. You can request properties that you want to have access to. So in my case, I want a device address and I want to know if it's connected. Then you create a watcher and you supply it with a filter. That's those three dots. We will look at the filter on the next slide. And you supply it with the request properties. What properties do I want from it and what kind of endpoint am I looking for? Then you just add the added and updated event handlers and just start to watch it. And the filter looks something like this. You can find this at MSDN or something like that. So you don't have to write it down or anything. Then we have the added event. With the event handler. In my case, I'm checking the name. I don't have to check the name since I'm checking for services. But in some cases, they reuse the services. They reuse the grid. So it could be a good thing to actually check the name if it's just a particular set of device you want to use. And then if it is the right one, you just pair it. You can also use the pairing.custom.pairersync if you want to do some special pairing code. So I found this scale on Net-on-Net. This is an Anderson scale, but it's actually SenSun who makes them. So I've written a small application that lists all the services, all their characteristics, all the ways that they can be accessed because the device knows these things and are happy to share them with you. I also created a collection or a database that converts these services to their Bluetooth SIG real names, if you want. So if you look at the SenSun scale, it contains generic access. This service will contain things like name and device address. Then we have the generic attribute. This is the service that actually makes it possible to ask the device what can you do. Then we have device information. It contains manufacturer, versions, name of the device, name and stuff like that. Then we have another one, not defined by the Bluetooth SIG. Interesting. So the one, I know that generic access, generic attribute and device information won't give me a weight value because these are defined by the Bluetooth SIG. They don't have a get weight. So the one named 0000FFBs, I'm sorry, I'm not going to let you suffer through that. So the last one is the one I want. So looking at our master's service, it has two characteristics. They both have a way to access them. In this case, I can write to it and I can get a notification from both of them. So I wrote this very simple application that listened to those notifications. And from one of the services, I actually got a reply. So probably that one. So now the fun part starts, the detective work. So I get 10 bytes back. It looks like this. So I started off with a baseline, zero grams, not putting anything on the scale. So I'm guessing that I'm actually looking for bytes that are zero. Those ones. Then I try to put an energy drink on my scale. Just happened to be next to me. It weighed 235 grams. Now, this value changed. I actually have 235 on byte 5. So I'm guessing that's the weight value. So now we're set the scale. Remove the energy drink. And I got minus 235. Now bytes 7 and 9 changed. But byte 5 is still 235. So I'm guessing 7 or 9 is a sign byte. Then I tried with 120 grams. Suddenly byte 4 changed. So after testing different weights, I managed to figure out that bytes I wanted to talk to or read from was 4, 5, and 7. And the formula ended up being like this. So if I take the byte 4 times 256 plus byte 5, that's going to give me the weight. And then I can use byte 7 to figure out what sign it is. So 0 for positive minus is 1 for negative. So my first demo is going to be a legend. Wait for it. Dairy. I'm sorry, I really like puns. So let's take a look at the code. So let's start with, I've actually written a couple of helper methods. So the first one is set up notify a sync. To get notifications, I have to tell my Bluetooth characteristic that, hey, I want notifications from you. So what I do is the first thing is that I get the service with this particular grid. Then I get the characteristic with a particular grid. And then I write client characteristic configuration, the script or a sync, the longest method name ever. And I say that I'm interested in notifications. And this method is going to return the characteristics to me when it's done. When I also have a read value, the same thing here. I get the service, I get the characteristic, I read the value, and I say I want it uncached. Because sometimes the device will cache it for me. So that's good if you want to have low battery consumption. But in this case, I want the latest value. When everything goes well, I get the data. I feel I get an array, byte rate. And in my case, I have made this method generic. So I can actually say that I want to string back. But the Bluetooth device is always going to return bytes to me. In this case, I'm just converted. I'm guessing it's UTF-8. That's not a given. And then I just return the bytes. And then we have write value. Same thing here. Get the service, get the characteristic. And here I'm actually asking, okay, what kind of way can I write to you? Can I write with the response? Sorry, without response? Or can I write with the response? And depending on what it can do, I just implement that. So I just write value async. Again, bytes, always bytes. So in our application, I define the service ID. This is our mystery service. Then we have the characteristic, characteristic squid. Then I create a device selector from squid. So this is going to give me all the devices that implement this service. I get a collection and I just loop through them. I get the Bluetooth LE device. And, well, here I check, does the name contain sense on? Again, I don't have to do this if I know that all the devices that implement this particular service going to use the same characteristics. But just to be safe. Then if I get a device, I run the method set up notify async. So I say to the service, I want notifications. I get the characteristics back. And then I just listen to the value change event. In the value change event, I get an array back. An array of bytes. So I send it into my convert to wait method and just set the property I have on my page to the new wait. And the wait convert to wait method is doing, well, it's going to check whether or not byte 7 is set or not. And it's going to take the sign times byte number 4, byte shift, shifted, and then byte 5. So let's see if this works. I have an energy drink, put it on the scale, and the value changes. Pretty simple, huh? I mean, this is not a lot of lines of code to do this. So let's go back to the presentation. So while we is a company who makes robots, toy robots, and they release a whole bunch of robots. This is the one to your left is the MIP. It's a Bluetooth robot. That one is the MIPO sour. I actually written an SDK to talk to these two. Actually, the one for MIP is out already as open source. I'll share the link later on. And the MIPO sour is coming. They also took a lot of old toys. These robots were controlled by remote control. But they upgraded them. So now they can be controlled by Bluetooth. So let's talk a little bit about the Azure IoT Hub. Sorry, Microsoft has released the Azure IoT Hub, a way to communicate with IoT devices. So what I could have done here is that I could have shown how to upload sensor data to the cloud and do stuff with it. That's no fun. I want to do something more fun. So we're going to try to control one of these robots through the Azure IoT Hub. So the first thing we need to do is actually set up the IoT Hub. I've done that in advance, so it's a little bit quicker to do this with slides. So the first step is to click new. So this is the Azure portal. The next step is to click Internet of Things and then Azure IoT Hub. So let's name this NDC. I have chosen to use the free price and scale tier, and I've chosen location in Northern Europe. Just create and then you wait a couple of minutes. Then you will see this screen, and your IoT Hub is ready for use. The free tier actually gives you 8,000 messages that you can send through the IoT Hub for free. That's awesome. The next step is to install the device explorer. It can be found on this URL. You can download a source for it, or you can download a full installable. It looks something like this. So to be able to use the device explorer, you need a connection string. So let's go back to the Azure portal. Click the key, then IoT Hub owner. This is created for you. So just click away, and then you have the connection string just there. Copy it and paste it into your device explorer, and you're all done. So now we can go to the management tab and click add. So we could have done this from code, but since we're only going to use one device, this will just, this will work perfectly. So I'll name this RoboSapien, and I just press create, and I get it nicely in my list. Then to create your application, I have chosen to create a universal Windows platform app. This will, of course, work with any app. So the first thing you need to do is to install the Microsoft Azure device client Nougat package. Once that's done, you can go to this URL. Microsoft actually helps us even more with this. So this is not going to be an Azure IoT Hub deep dive. I want to open your eyes to see how cool and how easy it is to communicate through the IoT Hub with Bluetooth devices. So the first thing, you go to this portal, you click connect your device. You will see a list of some of the devices you can connect to the IoT Hub. I have chosen use another device and Windows. And Microsoft is actually going to provide you with all the code you need. How nice is this? Just copy and paste it into your application, and you're done. You also need a connection string to your device. So that's the same connection string you can find in the Azure portal, but you need to add a device ID. But the device explorer actually gives you the correct connection string. So you can just copy it from there. So let's see. Bluetooth Internet of Things robot. That's a really boring name. It should be called Bluetooth Robiot, right? Of course. So there we go. Much better. Right. We're about to demo something. Now, actors know that they should never work with children or animals. As a speaker, you know that you should never work with Bluetooth that can be disturbed by 1,000 people in the same area than you. We also know that we should never rely on Internet. So that's exactly what I've done. I'm relying on Internet and Bluetooth. Okay. So let's just hope this works. There we go. So this robot actually has a bug in it. So I have to pair it every time I use it. This is made for parallel communication, but it will work, hopefully. I'm actually talking to Huawei trying to solve this. So here I just, I went into the Bluetooth settings and I paired the device. Let's go into messages. I'm going to show you the code in a while. So let's try, let's do an explosive demo. C4. Sorry, I need to start the app as well. There we go. Let's try that again. So now I'm actually controlling my robot through the device explorer into IoT Hub, then back to my application and then over to the robot. So let's try something else. See, I actually have a list of things it can do. So let's try a seven. So all the commands that is available through the infrared controller is also available through Bluetooth. So let's take a look at the code. So what I've done here is that I start, I create a device client. I create it from my connection string. This is the connection string to my device. Then I have a robot sapien class. This is the class that talks to the robot sapien. And we will look at that in just a second. And then I say robot sapien connect. This is going to try to find the device and connect to it. And then I also have a send event. So the send event is going to send robot sapien connected message to my IoT Hub. And if you look at the device explorer, let's see. If I monitor it and I start it. So if I start the app, it will connect to the robot sapien and I will get the message robot sapien connected if I monitor the Hub. And then I have received commands. So this is going to check, do I have any messages on my Hub? If I have a message, I will convert it, I will get bytes. I will convert it to a message. And then I will call the send command on my robot sapien and send in the message. So in this case, I'm going to get the bytes for C4, for example. Then I'm going to convert it to text and send it in to my robot sapien. So let's take a look at the robot sapien class. So here I'm actually not using my helper methods just to show how easy it is. So first thing, get the service quit. Get the characteristics quit. Then I connect. I try to find the device selector. So I try to get all the devices that implement this service. And one way is actually one of the examples that reduce their service quits. So I have another robot that does the, that works completely differently, has different characteristics but the same service ID. I actually think it has the same characteristics ID as well. So I get the enumerator and I get the device. Here I check does it contain robot sapien because the other robot is called MIP. I get the service and I get the characteristic. Then we have the send command. So here just take the characteristic and write it, write the data straight to the characteristic. I also have a method here that converts my C4 string hex to a byte array. That's all it takes. So I can send commands up to the IT hub. Then it will return to my application or actually every application that listens to that particular device. And then it would just send the bytes out to robot. So here are some resources. Microsoft actually has a lot of great samples on their GitHub account. We have the Azure, sorry, the device explorer. And then we have the URL to my MIP WinRT SDK. Do we have any questions? I'm soon done. Thank you. Do we have any questions? No questions. So then let me round up just to say that as I mentioned before, there are three billion devices out there that were manufactured last year alone. It is so much fun to do this. And if you have any questions about this presentation or Windows development in general, or if you ever in Sweden, please come by coding after work. So I'm kind of ahead of schedule, so I'm going to let you go. So please contact me if you want to. Thank you.
Bluetooth devices were manufactured last year. Of reverse engineer to figure out the protocols. So it. You can also use the access them. In this case, I can write to it and Hub, a way to communicate with IoT devices. So of Things and then Azure IoT Hub. So let's name devices you can connect to the IoT Hub. I have it will connect to the robot sapien and I will are three billion devices out there that were