在我的 windows 窗体应用程序中,我开始枚举设备列表,如下所示:
DeviceWatcher deviceWatcher =
DeviceInformation.CreateWatcher(
BluetoothLEDevice.GetDeviceSelectorFromPairingState(false),
requestedProperties,
DeviceInformationKind.AssociationEndpoint);
// Register event handlers before starting the watcher.
// Added, Updated and Removed are required to get all nearby devices
deviceWatcher.Added += DeviceWatcher_Added;
// EnumerationCompleted and Stopped are optional to implement.
deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted;
deviceWatcher.Stopped += DeviceWatcher_Stopped;
// Start the watcher.
deviceWatcher.Start();
找到设备后,我将按如下方式检索服务和特征:
BluetoothLEDevice btdev;
List<DeviceInformation> lst = new List<DeviceInformation>();
GattCharacteristic ch;
private async void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args)
{
if (args.Name == "Bluno")
{
btdev = await BluetoothLEDevice.FromIdAsync(args.Id);
GattDeviceServicesResult result = await btdev.GetGattServicesAsync();
Guid customGuid = new Guid("0000dfb0-0000-1000-8000-00805f9b34fb");
foreach (GattDeviceService service in result.Services)
{
if (customGuid == service.Uuid)
{
GattCharacteristicsResult cresult = await service.GetCharacteristicsAsync();
ch = cresult.Characteristics.Where(x => x.Uuid == new Guid("0000dfb1-0000-1000-8000-00805f9b34fb")).FirstOrDefault();
ch.ValueChanged += Ch_ValueChanged;
}
}
}
}
问题在于,即使 BLE 设备每秒持续发送多个值,它也不会触发 ValueChanged
事件。
感谢帮助