 Hi, I'm Mark Thistle, one of the developers on the Microsoft Band SDK. Today, I'm going to show you how easy it is to integrate the Band SDK into your Windows Universal app using Visual Studio 2015. The Microsoft Band SDK provides the APIs that you can use to integrate your application with the Microsoft Band. With the Band SDK, you can monitor the band sensors in real time opening up new interactions. The sensors also let you add new insights to extend the functionality of your app. For developers that require a custom app experience on the Microsoft Band, there's a Band Tile API to allow you to add custom tiles to the band. This will allow you to provide on-band feedback to the user and send messages to the user's band. The Band SDK also allows you to provide band customization to your users by setting the band theme colors and the me tile. Since no app code runs on the band, you require a companion app to interact with the band. Your companion app can be an Android, iOS, Windows Phone, or Windows Store app. The Band SDK allows you to open up new user experiences by using sensors as input sources. You could use the accelerometer as a controller for your app, for example. With the Band Tile API, you can extend your app UI onto the band with a custom tile that allows you to provide app insights to your user. For example, you could alert the user to the end of an event in your companion app, such as a timer or long-running task being completed. That's what the SDK provides, but how do we use the SDK? Let's take a look. All right. Let's launch BandJumper. When we do the first thing you're going to notice is that we have a blue section and we can see that the first player has connected, and then we have a red player. You can see that it's connected and we have the name of the band that was connected. At the top, I have the status message, which actually shows any errors and shows us how many bands are connected. Now, one of the first things we're going to want to do is put the player tile onto the band. To do that, we're going to press the Player 1 setup. Here's the tile that comes up when we're going to install a tile onto the user's band that they have to acknowledge it. We choose Yes, and then it's going to install onto their band. You'll see that the band says updating while the tile is being downloaded to it. We have the tiles installed onto two user's bands, so now we're going to start the game. We choose Start, and the bands will vibrate to indicate that the game is ready, and then the users can start doing jumping jacks. Next, we actually want to tell the user that the game is over, so we're going to send a haptic when we touch the Stop button. After the haptic is done, we're going to show the user who won and who lost. We're going to send a message dialog, which we can see on the band now. That's what the SDK provides, but how do we use the SDK? To demonstrate the Band SDK, I'm going to build a two-player game called Band Jumper. The game will award points to the players for performing jumping jacks. The game will use many of the band's APIs in action. First, let's start a new Windows Universal app in Visual Studio 2015. Once we have the app, let's add our dependency on the Microsoft Band SDK using NuGet. I'm going to right-click on References, choose Manage NuGet Packages, then I'm going to go to Search, and I'm going to type in Microsoft Band SDK, and here we go, and I'll choose Install. Once NuGet is installed, we're going to need to add the Bluetooth capability to our application. We can double-click on the Package AppX Manifest, go to Capabilities, and check to see if Bluetooth is enabled. If it's not, we can turn it on. Now we can save. Once we added our references from NuGet and verified that the Bluetooth capability is enabled, we're going to want to add a Band Connection class so that we can actually connect to our band. So I've already added that. So the first thing we're going to want to do is add using Microsoft Band, and then I'm going to keep track of the Band Info for Player 1 and Player 2. The Band Info has the name of the band, so we can reference that later to show to the user. Another thing I'm going to use and I'm going to make it public is a game tile ID. So the game tile ID is an ID we're going to use for our tile when it's installed into the band. The tile ID needs to be unique across the band, so you're going to want to use a unique one for each application that you create. So one of the first things we're going to do is find the bands. So when we connect, we're going to call Find Bands. Sorry, when our app starts, we're going to call Find Bands, and then we'll look at the paired bands that are connected to our phone. If we find the first one, we're going to store the Band Client Info, and well, Band Client Info here, and we're going to store the client. Now, to get the client, we're actually going to have to call Band Client Manager instance, Connect Async. So this will connect to the band based on the Band Info. So I'm going to send some messages to the UI that basically inform the user that we've connected to the band, the band name, and how many bands have been connected. Then if we find a second band, we're going to do the repeat the same process. Okay. So how are we going to call that? So what I'm going to do is go into the app XAML, and inside here, when my app is created, I'm going to create an instance of the band connections. So to display the connection status of the bands, and for the start of the game, we're going to create a main page. So we can see here that we have the blue box for player one and the red box for player two, and we've connected up the band one title and the band two title back to our band connection class. Those are supported down below using a property changed event. So here's the band one title, and so when we get the new band one title, we'll update the UI with that. Now we're going to want to take a look at our main page. So in our main page, we're going to use the app current.bands as our data source. That's what's going to update our UI. So then when we navigate to the main page, if it's a new navigation mode, we're going to actually call connect bands. This is what we'll start the process to look for the bands that are connected to our phone and then update the UI with them. Okay. So to push a custom tile to the band, I've got a method here player one set up underscore click and player two set up underscore click. They're both very similar. But what I've done is created a method here called create and push band tile async. That takes the band client we want to connect to, the color of the player that we want to push to the band and the player names that we're going to push to the band. So there's a lot going on in the create and push tile async. So let's go take a look at that now. So what you'll see here is we want to push the name of the player down to the tile. So one of the things we're going to have to do is create a band tiles pages text block. So we'll create that with the size that we want it. Then we're going to create the field panel. Now with the field panel, the color that we passed in, we're going to pass that into the background color of the field panel so that the user knows that they're the blue player or the red player. Now earlier I mentioned that in band connections, I have a game tile ID. So I'm going to set that here and then when I create my band tile, I'm going to give it a name. I'm going to call it jumper. I'm going to load the tile icons for it. The next thing I'm going to do is I'm going to add a page layout. So with the page layout added to the tile, next I can add the tile. Now in case I previously had played the game and I had a different color, the first thing I'm going to do is remove the tile async for this game session. Then I'm going to go and add the tile async. Now I've pushed down the page layout and I've pushed the tile to the band and we can see it, but there's no data there yet. So to do that, we need to actually add a page data. So we need to give it a GUID for each page. If you want to reference a page later, you would need to save your GUID, but in this case we're just going to create a new one on the fly because we're only going to have one page that we're using. Now this is where I'm going to set the player name for the text block that I created earlier at the very beginning of the method right here. So next thing I'm going to do is call set pages async. This is actually going to send the page data down and we will now be able to see the player tile on their band. So to do a lot of the heavy lifting for getting the sensor data when the game actually starts, I've created a band data source class. So let's take a look at that now. So when the game begins, we're going to send a haptic to the player. So I've put that information into the band data source. Send start game async is going to send a vibration or a haptic to the band using notification manager vibrate async using the two-tone high. So for the start of the game, I'm going to use two-tone high, which is going to buzz twice and it's a little stronger than some of the other vibrations, but it's not our strongest vibration because it's the start of the game and the players aren't active yet. So I'm going to call that on the band client. The next thing we need to do when we start the game is we need to actually start scoring the players. So the score to players, what I'm going to do is I'm going to connect to the heart rate and to the jumping jack detector I've called it here, but really it's the accelerometer. So let me explain how I'm going to use those. For the jumping jack detector, I'm going to look at movement and see that the player is actually moving the arms and then I'm going to using a pretty simple algorithm. I'm just going to look at the moving average and see when the user has basically moved their arms enough to account it as a jumping jack. So whenever they do a jumping jack, I'm going to score larger amounts of points rather than just when they're moving a little bit. The other thing I'm going to do with the heart rate is I'm going to look at the heart rate and if the heart rate is raising as it raises through several heart rate zones, I'm going to apply a bonus multiplier to it. Okay. So we're going to connect to the heart rate sensor first. So let's take a look at that. Here's a start heart rate async. You'll see that we check to see that it's supported. Then we check for if the heart rate consent has been given. So when you ask for the heart rate on a band, the user will be notified and they have to grant access. So we're going to check here to get current user consent and see that it's granted. If it is, we'll set the consent to true. If it's not granted then we're going to call request user consent async and we're going to wait that because it's going to pop up a dialogue that the user is going to choose the give consent or not give consent. After we come back from that, we will have set it with true or false indicate if the user has given us consent and then we can start, if they granted us consent, we can start the handler for the heart rate change reading. So let's take a look at what that handler looks like. It's just up here. So basically, we're going to get a band sensor reading event ARGs. That's going to have a I band heart rate reading. So that reading is going to basically tell us what the heart rate value is. To start actually listening to the heart rate, we're going to call band client sensor manager heartrate.startreading async. Now, this will start the heart rate running and we're going to get an update about every second that's going to tell us the heart rate of the user. Okay, so next we have our jumping jack detector. So that's our accelerometer. For the accelerometer, again, we're going to check to see that it's supported. If it is, we're going to create a jumping jack detector. This is just a class that I created that is processing the accelerometer to see if there are jumping jacks that have been created. So again, I'm going to use a method to handle the callback and I'll start it with band client sensor manager that accelerometer.startreading async. The handler is up here. So now what I'm doing with the accelerometer readings is I'm kind of doing a poor man's version of a algorithm that detects the jumping jacks. I'm grabbing the magnitude by doing the square root of the sum of the squares of the accelerometer for x, y and z readings. This will basically give me a smooth out version of if the user is actually moving and if they've made a large change. So I'm just taking a look at the delta. I'm seeing that they're moving a little bit. I score small points for that. And when I pass in the readings into the jumping jack detector, if I detect a jumping jack, then I'm giving them higher points for that. Then I'm going to set the score value which the user will see in the application. So what I've done is when I create a user on the game page, I'm going to create one of these player user control elements. I'm going to pass in the color of the player. I'm going to pass in the player's band client which is their connection to their band. And I'm going to pass in the name. Now when I do that, I will create our band data source which we just looked at and I'll set its name, I'll set its client and then I'll set that as the data source for our player user control. And that's how I'm going to get the score value. So if we go quickly back to the band data source, we're going to see that I have a score value here and I'm using the property change to update the score value. Okay, so our player user control has a start game async and a stop game async. Now you'll notice here that I have the band data source and we have the start sensor async and the send start game async. So those are the ones we just looked at and this is going to send our haptic and this one's going to start the scoring for the player. Now we have a stop game async which is going to stop the sensors and is going to send a send game async call. Okay, so here's the send game async method and you can see it looks very much like our start game async. Again, we're calling notification manager vibrate async with a vibration type this time of three tone high. So I chose the three tone high because it's more powerful and the user's going to be doing jumping jacks so we really want to alert them. And again, we're going to send that to the band client. Now stop sensors async is going to actually stop scoring the player. So we have a stop heart rate async and a stop jumping jack detector async. So the stop heart rate async is very similar. What we're going to do is call sensor manager heart rate dot stop reading async. The other thing I'm going to do is remove the event handler that I added earlier to handle the callbacks. So as a basically a good sensor citizen when you're using the band, you're going to want to stop reading from your sensors when your application is finished with them. And also similar with the jumping jack detector, we're going to do the same thing. We're going to do sensor manager, a accelerometer dot stop reading async. And that will stop any of the sensor readings from coming into us. So you'll notice that on the game page, we have a stop button and then a stop button. We're going to, we have a stop click callback handler. So with that there, we are going to see if we had one player. And so if we do, we're going to basically stop that player's game and then we're going to grab the score from that player. Now we're going to use the game tile again. So back from the band connections class, we're going to grab the game tile ID. We're going to need that in a second to send the message to the user to tell them that they won the game or they lost. So you can see here, this is the message we're going to send to the user. So if we want to send a, if we have a tile on the band and we want to send a message to that tile, we can call on the band client, we can call notification manager, send message async. We can pass in our tile ID. So we send it to the correct tile. We can give it a title and then we can give it a body. So what we're sending here is for the player that won, we said you won and then we're going to tell them their score. We have to add a date. And the last thing that we can do is we can add a show dialogue. A message is a notification that goes to the tile and is shown in a page inside the tile and it will badge the tile with a number. So each time you send a message, the count will go up on the badge. If you want to show a dialogue such that when the user turns on their screen, they see a message from you, then you'll need to turn on the show dialogue. So show dialogue is a temporary thing and it doesn't get stored. So if we sent just a dialogue, we wouldn't be sending the message such that it was stored inside of the tile. That's it. That's all we need from the Microsoft Band SDK to create our game. Now that you've seen that, I have some resources to help you get started with the Microsoft Band SDK. Thank you and reach out if you have any questions or feedback at healthmsatmicrosoft.com. We can't wait to see what you're going to build.