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!

  •   @JenMsft @LocalJoost @vinothrajendran not sure if related but when i click update &amp; restart my machine restarts, bu… https://t.co/4muU3d7aGG
  •   RT @natfriedman: Today(!) we’re thrilled to announce unlimited free private repos for all GitHub users, and a new simplified Enterprise off…
  •   @rendyfaqot hi, Sorry to hear. There been some breaking change in the VSTS api... didn't had time yet on fixing tho… https://t.co/lZBMHu4CPf
  •   RT @ameijers: The Global MR Bootcamp was a blast! The biggest #mixedreality event in the world! And all made possible by you the community.…
  •   RT @LocalJoost: Attendants 'working at my Immersive Headset Development workshop at #GlobalMRBootcamp https://t.co/NaNtyO5Ocj
  •   Why do i always decide to change the whole approach of a session the evening before?
  •   @JenMsft @bing it's not working so well for high speed or the distance is a problem https://t.co/KE1kE0wQuF
  •   @danhellem @mmanela Nice!
  •   RT @danhellem: Check out the new changes for the Azure Boards work item tracking experiences in Visual Studio 2019 https://t.co/c4l0rQky1o…
  •   @LocalJoost you put a AAAA battery in your surface pro itself? or the pen?