首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我能用Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData阅读iPhone信标吗?

我能用Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData阅读iPhone信标吗?
EN

Stack Overflow用户
提问于 2015-07-19 19:45:43
回答 1查看 2.1K关注 0票数 2

根据蓝牙广告样本的说法,我需要设置CompanyID ( UInt16 )和数据(IBuffer,样本中的UInt16)来开始监视广告商。

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

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

代码语言:javascript
运行
复制
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中的字节:

代码语言:javascript
运行
复制
writer.WriteGuid(Guid.Parse("504B1B3F-9CC0-EE4A-2F97-0E75349D8467"));

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-19 21:26:06

要检测Windows 10上的信标,最重要的是使用新的BluetoothLeAdvertisementWatcher类。

问题中的代码似乎集中在设置一个过滤器,只查找特定的蓝牙LE广告,与公司代码相匹配,或者是广告中包含的UUID。虽然这是一种方法,但这并不是绝对必要的--你可以简单地查找所有的蓝牙LE广告,然后对它们进行解码,看看它们是否是信标广告。

我在下面粘贴了一些代码,显示了我认为你想做的事情。主要警告:我本人还没有测试过这段代码,因为我没有Windows10开发环境。如果您自己尝试并进行更正,请让我知道,我会更新我的答案。

代码语言:javascript
运行
复制
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广告的字节:

代码语言:javascript
运行
复制
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网络)
  • 接下来的两个字节是信标类型代码(0 0xacbe= AltBeacon)
  • 接下来的16个字节是第一个标识符2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6。
  • 接下来的2个字节是第二个标识符0x0001。
  • 接下来的2个字节是第三个标识符0x0002。
  • 以下字节是功率校准值0xC5 -> -59 dBm
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31505227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档