Monday, November 17, 2014

BLE on Windows Phone 8.1 (and Xamarin.Forms)

 

Introduction / Compatibility

BLE or Bluetooth Low Energy is a bit of a funny story when it comes to Windows Phone.  The technology was created by Nokia back in 2006, but both iOS and Android added support for it in their mobile devices well before Windows Phone.  Even worse, it sounded like BLE would work on Windows Phone 8 almost 2 years ago but the simple truth is: you currently need a Windows Phone 8.1 Nokia device running at the least the Cyan update.  The “color” updates are alphabetical so Denim will work, (eggplant?), some color that’s starts with f, etc. 

You can check on update availability for Nokia Lumia devices here: http://www.microsoft.com/en/mobile/support/software-update/wp8-software-update/ 

I’ve personally been using a Rogers Nokia Lumia 920 and T-Mobile Lumia 521.  Both work well after the Cyan update.

Connecting to BLE Devices from Windows Phone

So BLE was designed to avoid needing to “Pair” devices like we do with traditional Bluetooth.  But strangely, on Windows Phone 8.1 you still need to pair with BLE devices, unlike iOS and Android.  This means opening up your Bluetooth Settings and selecting your target device.

 

wp_ss_20141117_0001

 

The upside to this is, your Windows Phone doesn’t need to search for new devices every time you want to use BLE in your application.  The downside is, Windows Phone doesn’t search for new devices and therefore doesn’t verify automatically that your paired device is present.

Windows Phone 8.1 Capabilities (Permissions)

You’ll also need to specify BLE capability in your Package.Appmanifest.  Currently Visual Studio doesn’t offer BLE in capabilities selector so you’ll need to edit the file manually.

ViewCode

 

First you’ll want to include “internetClientServer” capability:

<Capability Name="internetClientServer" />

 

And then specify a DeviceCapability:

 

<m2:DeviceCapability Name="bluetooth.genericAttributeProfile">

You can then specify a BLE device specifically but to make things simple I recommend using “any”

 

<m2:Device Id="any">

 

Finally, you need to specify which services you’re going to be using with the BLE device, which you can do by category or Guid:

<m2:Function Type="name:genericAccess" />

<m2:Function Type="serviceId:713d0000-503e-4c75-ba94-3148f18d941e"/>

 

If you’re not sure what services you’ll be using you can just list all of the categories:

<m2:Function Type="name:genericAccess" />

<m2:Function Type="name:alertNotification" />

<m2:Function Type="name:battery" />

<m2:Function Type="name:bloodPressure" />

...

<m2:Function Type="name:txPower" />

A simple complete example to communicate with the RedBear Labs BLE Mini:

<Capabilities>

  <Capability Name="internetClientServer" />

  <m2:DeviceCapability Name="bluetooth.genericAttributeProfile">

     <m2:Device Id="any">

        <m2:Function Type="name:genericAccess" />

        <m2:Function Type="serviceId:713d0000-503e-4c75-ba94-3148f18d941e"/>

     </m2:Device>

  </m2:DeviceCapability>

</Capabilities>

 

Windows Phone 8.1 Projects

You will need to make a project targeting Windows Phone 8.1 to use BLE in your application, but there are actually two APIs you can use and they are not equal.

The more complete API set is in the new Windows Phone Store app model which uses the RT APIs.  Windows Phone Silverlight 8.1 is said to contain about 90% of the Store API but apparently some of the BLE APIs fall into the missing 10%.

Specifically, it’s missing the ability to generally search for Gatt Characteristics.  As long as you know the Guids for your characteristics this shouldn’t be an issue.  But you will come across more example code using the Windows Phone Store APIs.

Now if you’re a Xamarin.Forms developer like I am, you’re going to be using the Silverlight APIs.  The good news is, you can upgrade your Xamarin Forms Windows Phone projects to Silverlight 8.1!

 

Code Example

I’ll add some coding specifics on here soon, but for now, you can take a look at a Windows Phone 8.1 Silverlight example app here:

https://github.com/adrianstevens/WP8_RedBear_BLEMini