低功耗蓝牙 C# 广告和扫描间隔
Bluetooth LE C# Advertising and Scan Interval
我最近开始开发 UWP 项目,并一直在尝试与 'Minew S1' BLE 设备建立连接,根据其文档,该设备具有三个主要功能服务 运行宁。
文档没有指出这样的方式,但我发现了在BLE扫描期间读取设备数据(frameType、productModel、batteryLevel、温度、湿度、MAC地址)的可能性,因为设备使用未记录的服务(UUID:0000ffe1-0000-1000-8000-00805f9b34fb)和相应的服务数据字符串响应(?)这些扫描。
我创建了一个简单的 BluetoothLEAdvertisementWatcher 对象并订阅了它的 Received 事件。这样,我在 运行 时间内只从该对象获取一个 serviceData 字符串。代码片段如下:
BluetoothLEAdvertisementWatcher bleWatcher;
bleWatcher = new BluetoothLEAdvertisementWatcher();
bleWatcher.ScanningMode = BluetoothLEScanningMode.Active;
bleWatcher.Received += BleWatcher_Received;
bleWatcher.Stopped += BleWatcher_Stopped;
bleWatcher.Start();
private void BleWatcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
DataReader dataReader;
foreach (var item in args.Advertisement.ServiceUuids)
{
if (item == new Guid("0000ffe1-0000-1000-8000-00805f9b34fb"))
{
foreach (var data in args.Advertisement.DataSections)
{
dataReader = DataReader.FromBuffer(data.Data);
byte[] bytes = new byte[data.Data.Length];
dataReader.ReadBytes(bytes);
if (BitConverter.ToString(bytes).Length > 5)
{
S1Sensor s1 = new 1Sensor(BitConverter.ToString(bytes));
Debug.WriteLine("New S1 Sensor advertisement received:");
Debug.WriteLine("Timestamp: " + DateTime.Now + ", Battery level: " + s1.BatteryLevel + "%, Temperature: " +
s1.Temperature + "°C, " + "Humidity: " + s1.Humidity + "%, MAC Address: " + s1.MAC_Address + "==");
}
}
}
}
}
问题 1: 上面的代码使用我编写的 S1Sensor 帮助程序 class 方法成功打印了所需的数据。唯一的问题是设备的广告间隔默认设置为 1000 毫秒,但我在应用程序期间只收到一次此字符串(很少两次)运行。有没有办法更频繁地接收和处理这些serviceData? (最好每 1000 毫秒)
问题二:
我的另一个问题是,当设备与我的计算机配对时,此代码仅 returns 值。我认为即使设备未与计算机配对,我也应该能够在不连接的情况下扫描设备并查看它们的响应(serviceData)。
即使 运行 运行此应用程序的计算机未与设备配对,是否有查询 serviceData 字符串的方法?
感谢您的宝贵时间。
Problem 1: Is there a way to receive and process these serviceData
more frequently? (preferrably every 1000 ms)
如果设备处于连接状态而不是广告状态,它将不会进行广告并且可能导致您无法再接收服务数据。您需要断开设备连接才能将其恢复为广告状态。你可以检查这个 thread.
Problem 2: Is there a way to query the serviceData string even when
the computer that is running this application not paired to the
device?
From Windows 10 Creators Update,这可以通过使用BluetoothLEDevice.GetGattServicesAsync()
、GattDeviceService.GetCharacteristicsAsync()
以及GattCharacteristic.GetDescriptorsAsync()
查询远程设备而无需配对来实现。
我使用 TI SensorTag BLE 设备测试 UWP 低功耗蓝牙样本。没有配对,我可以像这样读取设备数据:
我最近开始开发 UWP 项目,并一直在尝试与 'Minew S1' BLE 设备建立连接,根据其文档,该设备具有三个主要功能服务 运行宁。
文档没有指出这样的方式,但我发现了在BLE扫描期间读取设备数据(frameType、productModel、batteryLevel、温度、湿度、MAC地址)的可能性,因为设备使用未记录的服务(UUID:0000ffe1-0000-1000-8000-00805f9b34fb)和相应的服务数据字符串响应(?)这些扫描。
我创建了一个简单的 BluetoothLEAdvertisementWatcher 对象并订阅了它的 Received 事件。这样,我在 运行 时间内只从该对象获取一个 serviceData 字符串。代码片段如下:
BluetoothLEAdvertisementWatcher bleWatcher;
bleWatcher = new BluetoothLEAdvertisementWatcher();
bleWatcher.ScanningMode = BluetoothLEScanningMode.Active;
bleWatcher.Received += BleWatcher_Received;
bleWatcher.Stopped += BleWatcher_Stopped;
bleWatcher.Start();
private void BleWatcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
DataReader dataReader;
foreach (var item in args.Advertisement.ServiceUuids)
{
if (item == new Guid("0000ffe1-0000-1000-8000-00805f9b34fb"))
{
foreach (var data in args.Advertisement.DataSections)
{
dataReader = DataReader.FromBuffer(data.Data);
byte[] bytes = new byte[data.Data.Length];
dataReader.ReadBytes(bytes);
if (BitConverter.ToString(bytes).Length > 5)
{
S1Sensor s1 = new 1Sensor(BitConverter.ToString(bytes));
Debug.WriteLine("New S1 Sensor advertisement received:");
Debug.WriteLine("Timestamp: " + DateTime.Now + ", Battery level: " + s1.BatteryLevel + "%, Temperature: " +
s1.Temperature + "°C, " + "Humidity: " + s1.Humidity + "%, MAC Address: " + s1.MAC_Address + "==");
}
}
}
}
}
问题 1: 上面的代码使用我编写的 S1Sensor 帮助程序 class 方法成功打印了所需的数据。唯一的问题是设备的广告间隔默认设置为 1000 毫秒,但我在应用程序期间只收到一次此字符串(很少两次)运行。有没有办法更频繁地接收和处理这些serviceData? (最好每 1000 毫秒)
问题二: 我的另一个问题是,当设备与我的计算机配对时,此代码仅 returns 值。我认为即使设备未与计算机配对,我也应该能够在不连接的情况下扫描设备并查看它们的响应(serviceData)。 即使 运行 运行此应用程序的计算机未与设备配对,是否有查询 serviceData 字符串的方法?
感谢您的宝贵时间。
Problem 1: Is there a way to receive and process these serviceData more frequently? (preferrably every 1000 ms)
如果设备处于连接状态而不是广告状态,它将不会进行广告并且可能导致您无法再接收服务数据。您需要断开设备连接才能将其恢复为广告状态。你可以检查这个 thread.
Problem 2: Is there a way to query the serviceData string even when the computer that is running this application not paired to the device?
From Windows 10 Creators Update,这可以通过使用BluetoothLEDevice.GetGattServicesAsync()
、GattDeviceService.GetCharacteristicsAsync()
以及GattCharacteristic.GetDescriptorsAsync()
查询远程设备而无需配对来实现。
我使用 TI SensorTag BLE 设备测试 UWP 低功耗蓝牙样本。没有配对,我可以像这样读取设备数据: