根据蓝牙广告样本的说法,我需要设置CompanyID
( UInt16 )和数据(IBuffer,样本中的UInt16)来开始监视广告商。
在iPhone中,我可以将信标UUID设置为4B503F1B-C09C-4AEE-972F-750E9D346784
。在互联网上读到,我找到苹果公司的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();
我还尝试倒转UUID中的字节:
writer.WriteGuid(Guid.Parse("504B1B3F-9CC0-EE4A-2F97-0E75349D8467"));
到目前为止还没有成功。我是混合了两种完全不同的技术吗?
发布于 2015-07-19 21:26:06
要检测Windows 10上的信标,最重要的是使用新的BluetoothLeAdvertisementWatcher
类。
问题中的代码似乎集中在设置一个过滤器,只查找特定的蓝牙LE广告,与公司代码相匹配,或者是广告中包含的UUID。虽然这是一种方法,但这并不是绝对必要的--你可以简单地查找所有的蓝牙LE广告,然后对它们进行解码,看看它们是否是信标广告。
我在下面粘贴了一些代码,显示了我认为你想做的事情。主要警告:我本人还没有测试过这段代码,因为我没有Windows10开发环境。如果您自己尝试并进行更正,请让我知道,我会更新我的答案。
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
这被解码为:
https://stackoverflow.com/questions/31505227
复制相似问题