kie*_*wic 2 windows-runtime ibeacon windows-10
根据蓝牙广告示例,我需要设置CompanyID(UInt16)和数据(IBuffer,示例中的 UInt16 )以开始观看广告商。
在 iPhone 中,我可以将信标 UUID 设置为4B503F1B-C09C-4AEE-972F-750E9D346784. 在互联网上阅读,我发现Apple 的公司 ID 是0x004C,所以我尝试了0x004C和 0x4C00。
所以,这是我到目前为止的代码,但当然,它不起作用。
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();
Run Code Online (Sandbox Code Playgroud)
我还尝试反转 UUID 中的字节:
writer.WriteGuid(Guid.Parse("504B1B3F-9CC0-EE4A-2F97-0E75349D8467"));
Run Code Online (Sandbox Code Playgroud)
目前还没有成功。我是否混合了两种完全不同的技术?
在 Windows 10 上检测信标需要做的最重要的事情是使用新BluetoothLeAdvertisementWatcher类。
问题中的代码似乎侧重于设置过滤器,以仅查找与公司代码匹配的特定蓝牙 LE 广告以及广告中可能包含的 UUID。虽然这是一种方法,但并非绝对必要——您可以简单地查找所有蓝牙 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
}
}
}
Run Code Online (Sandbox Code Playgroud)
下一个显而易见的问题是如何解码广告的字节?搜索网络并找出各种信标类型(甚至是专有信标类型)的字节序列非常容易。为了使这个答案简短且不涉及知识产权,我将简单描述如何解码开源 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
Run Code Online (Sandbox Code Playgroud)
这被解码为:
| 归档时间: |
|
| 查看次数: |
1982 次 |
| 最近记录: |