我可以用 Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData 读取 iPhone 信标吗

Can I read an iPhone beacon with Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData

根据Bluetooth Advertisement sample,我需要设置CompanyIDUInt16)和数据(IBuffer,示例中的一个 UInt16) 开始监视广告商。

在iPhone中,我可以将信标UUID设置为4B503F1B-C09C-4AEE-972F-750E9D346784。在互联网上阅读,我 found Apple 的公司 ID 是 0x004C,所以我尝试了 0x004C0x4C00

所以,这是我到目前为止的代码,但当然,它不起作用。

var manufacturerData = new BluetoothLEManufacturerData();

// Then, set the company ID for the manufacturer data. Here we picked an unused value: 0xFFFE
manufacturerData.CompanyId = 0x4C00;

// Finally set the data payload within the manufacturer-specific section
// Here, use a 16-bit UUID: 0x1234 -> {0x34, 0x12} (little-endian)
var writer = new DataWriter();
writer.WriteGuid(Guid.Parse("4B503F1B-C09C-4AEE-972F-750E9D346784"));

// Make sure that the buffer length can fit within an advertisement payload. Otherwise you will get an exception.
manufacturerData.Data = writer.DetachBuffer();

我也试过反转 UUID 中的字节:

writer.WriteGuid(Guid.Parse("504B1B3F-9CC0-EE4A-2F97-0E75349D8467"));

到目前为止还没有成功。我是否混合了两种完全不同的技术?

要在 Windows 10 上检测信标,您需要做的最重要的事情是使用新的 BluetoothLeAdvertisementWatcher class。

问题中的代码似乎着重于设置过滤器以仅查找与公司代码以及广告中包含的 UUID 匹配的特定蓝牙 LE 广告。虽然这是一种方法,但并非绝对必要——您可以简单地查找所有蓝牙 LE 广告,然后对其进行解码以查看它们是否是信标广告。

我在下面粘贴了一些代码,显示了我认为您想要执行的操作。 主要警告:我没有亲自测试此代码,因为我没有 Windows 10 开发环境。 如果您自己尝试并进行更正,请告诉我我会更新我的答案。

private BluetoothLEAdvertisementWatcher bluetoothLEAdvertisementWatcher;

public LookForBeacons() {
    bluetoothLEAdvertisementWatcher = new BluetoothLEAdvertisementWatcher();
    bluetoothLEAdvertisementWatcher.Received += OnAdvertisementReceived;
    bluetoothLEAdvertisementWatcher.Start();
}


private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs) {

    var manufacturerSections = eventArgs.Advertisement.ManufacturerData;
    if (manufacturerSections.Count > 0) {
        var manufacturerData = manufacturerSections[0];
        var data = new byte[manufacturerData.Data.Length];
        using (var reader = DataReader.FromBuffer(manufacturerData.Data)) {
            reader.ReadBytes(data);

            // If we arrive here we have detected a Bluetooth LE advertisement
            // Add code here to decode the the bytes in data and read the beacon identifiers

        }
    }
}

下一个明显的问题是如何解码广告的字节?在网上搜索并找出各种信标类型(甚至专有信标)的字节序列非常容易。为了使这个答案简短并且不涉及知识 属性 丛林,我将简单地描述如何解码开源 AltBeacon 广告的字节:

18 01 be ac 2f 23 44 54 cf 6d 4a 0f ad f2 f4 91 1b a9 ff a6 00 01 00 02 c5 00

解码为:

  • 前两个字节是公司代码(0x0118 = Radius Networks)
  • 接下来的两个字节是信标类型代码(0xacbe = AltBeacon)
  • 接下来的16个字节是第一个标识符2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6
  • 接下来的2个字节是第二个标识符0x0001
  • 接下来的2个字节是第三个标识符0x0002
  • 后面的字节是功率校准值0xC5 -> -59 dBm