Get started with beacons in your UWP App

With Windows 10 Microsoft added support for Beacons. In this blogpost I will tell you how to get started. It’s very easy! And since writing an UWP app it will work on your pc, tablet but ofcourse also on your phone.

Requirements:

  • A beacon, A little device that sends the signal
  • A Bluetooth LE enabled device

I am using this beacon in combination with my surface 3 pro and my Lumia 930.

First the setup; this one is little tricky. You need to enable Bluetooth for your app. In the past I was used to do this in the manifest designer where you could easily add capabilities but for some reason this seems to be missing. So you need to open the package.manifest with the xml editor. You can do this by right click on the file. Choose “Open with” and then from the list pick “Xml (Text) Editor”. In the Capability section you need to add: <DeviceCapability Name=”bluetooth” />

enter image description here

From here it’s getting pretty easy. Create an instance from BluetoothLEAdvertisementWatcher and add an event handler to the Received event and when you are ready call start

watcher = new BluetoothLEAdvertisementWatcher();
watcher.Received += Watcher_Received;
watcher.Start();

Now when a beacon is getting close the event will get fired. Here you get the data that is received from the beacon as Manufacturer data, signal strength and the timestamp of when the beacon signal was received. Code to parse the incoming data can look like this, I copied this from the Sample code Microsoft published at GitHub

// We can obtain various information about the advertisement we just received by accessing 
// the properties of the EventArgs class

// The timestamp of the event
DateTimeOffset timestamp = eventArgs.Timestamp;

// The type of advertisement
BluetoothLEAdvertisementType advertisementType = eventArgs.AdvertisementType;

// The received signal strength indicator (RSSI)
Int16 rssi = eventArgs.RawSignalStrengthInDBm;

// The local name of the advertising device contained within the payload, if any
string localName = eventArgs.Advertisement.LocalName;

// Check if there are any manufacturer-specific sections.
// If there is, print the raw data of the first manufacturer section (if there are multiple).
string manufacturerDataString = "";
var manufacturerSections = eventArgs.Advertisement.ManufacturerData;
if (manufacturerSections.Count > 0)
{
// Only print the first one of the list
var manufacturerData = manufacturerSections[0];
var data = new byte[manufacturerData.Data.Length];
using (var reader = DataReader.FromBuffer(manufacturerData.Data))
{
    reader.ReadBytes(data);
}
// Print the company ID + the raw data in hex format
manufacturerDataString = string.Format("0x{0}: {1}",
    manufacturerData.CompanyId.ToString("X"),
    BitConverter.ToString(data));
}

// Serialize UI update to the main UI thread
await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
// Display these information on the list
ReceivedAdvertisementListBox.Items.Add(string.Format("[{0}]: type={1}, rssi={2}, name={3}, manufacturerData=[{4}]",
    timestamp.ToString("hh\\:mm\\:ss\\.fff"),
    advertisementType.ToString(),
    rssi.ToString(),
    localName,
    manufacturerDataString));
});

The watcher now will fire the Received event for every beacon, but you can add filters. Filters can be placed on the manufacturer data, so you will just receive beacons that are created by your for example, but it’s also possible to place filters on the signal strength.

What next? With this code your app have to be running, but it’s also possible to make a BackgroundTask that gets triggered when you getting in range or out of range of a beacon. In a next blogpost I will show how you build this!