我可以使用Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData读取iPhone信标吗 [英] Can I read an iPhone beacon with Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData

查看:348
本文介绍了我可以使用Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData读取iPhone信标吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据蓝牙广告示例,我需要设置CompanyID( UInt16 )和数据(示例中的UInt16 IBuffer )来开始监视广告客户.

在iPhone中,我可以将信标UUID设置为4B503F1B-C09C-4AEE-972F-750E9D346784.在互联网上阅读后,我找到了苹果的公司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上检测信标,您需要做的最重要的事情就是使用新的Bluetooth Advertisement sample, I need to set the CompanyID (UInt16) and the Data (IBuffer, a UInt16 in the sample) to start watching for advertisers.

In the iPhone, I can set the beacon UUID to 4B503F1B-C09C-4AEE-972F-750E9D346784. And reading on the internet, I found Apple's company id is 0x004C, so I tried 0x004C and 0x4C00.

So, this is the code I have so far, but of course, it is not working.

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();

I also tried inverting the bytes in the UUID:

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

Not success so far. Am I mixing two completely different technologies?

解决方案

The most important thing you need to do to detect Beacons on Windows 10 is to use the new BluetoothLeAdvertisementWatcher class.

The code in the question seems focussed on setting up a filter to look for only specific Bluetooth LE advertisements matching a company code and perhaps a UUID contained in the advertisement. While this is one approach, it isn't strictly necessary -- you can simply look for all Bluetooth LE advertisements, then decode them to see if they are beacon advertisements.

I've pasted some code below that shows what I think you want to do. Major caveat: I have not tested this code myself, as I don't have a Windows 10 development environment. If you try it yourself and make corrections, please let me know and I will update my answer.

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

        }
    }
}

The next obvious question is how do you decode the bytes of the advertisement? It's pretty easy to search the web and find out the byte sequence of various beacon types, even proprietary ones. For the sake of keeping this answer brief and out of the intellectual property thicket, I'll simply describe how to decode the bytes of an open-source AltBeacon advertisement:

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

This is decoded as:

  • The first two bytes are the company code (0x0118 = Radius Networks)
  • The next two bytes are the beacon type code (0xacbe = AltBeacon)
  • The next 16 bytes are the first identifier 2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6
  • The next 2 bytes are the second identifier 0x0001
  • The next 2 bytes are the third identifier 0x0002
  • The following byte is the power calibration value 0xC5 -> -59 dBm

这篇关于我可以使用Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData读取iPhone信标吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆