Skip to content

Instantly share code, notes, and snippets.

@brianpursley
Created February 2, 2017 13:50
Show Gist options
  • Save brianpursley/b78beec4fe43054da6e92d56f6d0f3a7 to your computer and use it in GitHub Desktop.
Save brianpursley/b78beec4fe43054da6e92d56f6d0f3a7 to your computer and use it in GitHub Desktop.
iBeacon Windows 10 Console Application using C#
using System;
using System.Linq;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Storage.Streams;
namespace BeaconExample
{
class Program
{
private class BeaconData
{
public Guid Uuid { get; set; }
public ushort Major { get; set; }
public ushort Minor { get; set; }
public sbyte TxPower { get; set; }
public static BeaconData FromBytes(byte[] bytes)
{
if (bytes[0] != 0x02) { throw new ArgumentException("First byte in array was exptected to be 0x02", "bytes"); }
if (bytes[1] != 0x15) { throw new ArgumentException("Second byte in array was expected to be 0x15", "bytes"); }
if (bytes.Length != 23) { throw new ArgumentException("Byte array length was expected to be 23", "bytes"); }
return new BeaconData
{
Uuid = new Guid(
BitConverter.ToInt32(bytes.Skip(2).Take(4).Reverse().ToArray(), 0),
BitConverter.ToInt16(bytes.Skip(6).Take(2).Reverse().ToArray(), 0),
BitConverter.ToInt16(bytes.Skip(8).Take(2).Reverse().ToArray(), 0),
bytes.Skip(10).Take(8).ToArray()),
Major = BitConverter.ToUInt16(bytes.Skip(18).Take(2).Reverse().ToArray(), 0),
Minor = BitConverter.ToUInt16(bytes.Skip(20).Take(2).Reverse().ToArray(), 0),
TxPower = (sbyte)bytes[22]
};
}
public static BeaconData FromBuffer(IBuffer buffer)
{
var bytes = new byte[buffer.Length];
using (var reader = DataReader.FromBuffer(buffer))
{
reader.ReadBytes(bytes);
}
return BeaconData.FromBytes(bytes);
}
}
static void Main(string[] args)
{
var watcher = new BluetoothLEAdvertisementWatcher();
watcher.Received += Watcher_Received;
watcher.Start();
Console.WriteLine("Bluetooth LE Advertisement Watcher Started (Press ESC to exit)");
while (Console.ReadKey().Key != ConsoleKey.Escape)
{
}
watcher.Stop();
Console.WriteLine("Bluetooth LE Advertisement Watcher Stopped");
}
private static void Watcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
const ushort AppleCompanyId = 0x004C;
foreach (var adv in args.Advertisement.ManufacturerData.Where(x => x.CompanyId == AppleCompanyId))
{
var beaconData = BeaconData.FromBuffer(adv.Data);
Console.WriteLine(
"[{0}] {1}:{2}:{3} TxPower={4}, Rssi={5}",
args.Timestamp,
beaconData.Uuid,
beaconData.Major,
beaconData.Minor,
beaconData.TxPower,
args.RawSignalStrengthInDBm);
}
}
}
}
@cmukoyi
Copy link

cmukoyi commented Oct 20, 2019

Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name 'BluetoothLEAdvertisementWatcher' could not be found (are you missing a using directive or an assembly reference?) BeaconExample C:\Users\cmu\source\repos\BeaconExample\BeaconExample\Program.cs 57 Active

@cmukoyi
Copy link

cmukoyi commented Oct 20, 2019

Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name 'BluetoothLEAdvertisementWatcher' could not be found (are you missing a using directive or an assembly reference?) BeaconExample C:\Users\cmu\source\repos\BeaconExample\BeaconExample\Program.cs 46 Active

@cmukoyi
Copy link

cmukoyi commented Oct 20, 2019

Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name 'BluetoothLEAdvertisementReceivedEventArgs' could not be found (are you missing a using directive or an assembly reference?) BeaconExample C:\Users\cmu\source\repos\BeaconExample\BeaconExample\Program.cs 57 Active

@cmukoyi
Copy link

cmukoyi commented Oct 20, 2019

Severity Code Description Project File Line Suppression State
Error CS0103 The name 'DataReader' does not exist in the current context BeaconExample C:\Users\cmu\source\repos\BeaconExample\BeaconExample\Program.cs 36 Active

@benmp2
Copy link

benmp2 commented Jan 24, 2021

Thanks for this example! After rewriting it a bit for my own purposes it worked great.

FYI: If anyone tries running the above code as a Console App (.Net Core) in Microsoft Visual Studio, make sure you add the required references as explained in this post: https://stackoverflow.com/questions/53268156/is-universal-windows-platform-required-to-use-the-windows-devices-bluetooth-name
In my own case the first reference was actually here: "C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.18362.0\Windows.winmd"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment