Hi!
Using the WinRT/UWP BluetoothLEAdvertisementWatcher
I'm scanning unpaired Bluetooth LE devices and trying to filter them based on their advertised services.
The watcher correctly finds my (unpaired) BLE device, but the list of advertised services in BluetoothLEAdvertisementReceivedEventArgs.Advertisement.ServiceUuids
is missing the custom service of my device.
My computer's Bluetooth chipset is a Qualcomm Atheros QCA61x4
and has support for Bluetooth 5 and my drivers are up-to-date.
When testing the same device on my phone, I'm properly getting the service UUID from the advertisement data so I'm thinking that there might be an issue with the implementation of BluetoothLEAdvertisementWatcher
?
EDIT: after more digging, I realized that the custom service UUID is send only after getting a "scan response" from the client. May be the watcher is not sending the request?
Here is my test code:
using System;
using System.Linq;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Storage.Streams;
using static System.Console;
namespace UWP_BLE
{
class Program
{
static void Main(string[] _args)
{
var watcher = new BluetoothLEAdvertisementWatcher();
watcher.Received += Watcher_Received;
watcher.AllowExtendedAdvertisements = true;
watcher.ScanningMode = BluetoothLEScanningMode.Active;
watcher.Start();
Console.ReadLine();
}
private static void Watcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
// I'm filtering by the name of my BLE device
if (args.Advertisement.LocalName == "5272271")
{
foreach (var uuid in args.Advertisement.ServiceUuids)
{
WriteLine("Service: " + uuid);
}
foreach (var dataSection in args.Advertisement.DataSections)
{
var data = new byte[dataSection.Data.Length];
using (var reader = DataReader.FromBuffer(dataSection.Data))
{
reader.ReadBytes(data);
}
WriteLine($"Data section: {dataSection.DataType} -- {string.Join(' ', data.Select(b => b.ToString("X2")))}");
}
foreach (var manufacturerData in args.Advertisement.ManufacturerData)
{
var data = new byte[manufacturerData.Data.Length];
using (var reader = DataReader.FromBuffer(manufacturerData.Data))
{
reader.ReadBytes(data);
}
WriteLine($"Manufacturer data: {manufacturerData.CompanyId} -- {string.Join(' ', data.Select(b => b.ToString("X2")))}");
}
}
}
}
}
And what I'm getting (note: the only service listed is not the custom one advertised by my device):
Service: 0000180a-0000-1000-8000-00805f9b34fb
Data section: 25 -- 00 00
Data section: 1 -- 06
Data section: 3 -- 0A 18
Data section: 255 -- 01 14 E5 D5 F9 A9 00 00 00
Data section: 9 -- 35 32 37 32 32 37 31
Manufacturer data: 5121 -- E5 D5 F9 A9 00 00 00
And a screenshot of the same device being scanned by my phone (notice the "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" service UUID which is the custom service I'm looking for).
Thanks for your help!