Skip to content

Commit

Permalink
Fixed StartUpdatesNativeAsync for indicate
Browse files Browse the repository at this point in the history
  • Loading branch information
AskBojesen committed May 19, 2024
1 parent 1e19fa8 commit 74b0446
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
25 changes: 22 additions & 3 deletions Source/BLE.Client/BLE.Client.WinConsole/PluginDemos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public async Task Connect_Read_Services_Disconnect_Loop()
string bleaddress = BleAddressSelector.GetBleAddress();
var id = bleaddress.ToBleDeviceGuid();
var connectParameters = new ConnectParameters(connectionParameterSet: ConnectionParameterSet.Balanced);
new Task(ConsoleKeyReader).Start();
new Task(ConsoleKeyReader).Start();
using (IDevice dev = await Adapter.ConnectToKnownDeviceAsync(id, connectParameters))
{
int count = 1;
Expand Down Expand Up @@ -151,7 +151,7 @@ public async Task Connect_Read_Services_Dispose_Loop()
var connectParameters = new ConnectParameters(connectionParameterSet: ConnectionParameterSet.Balanced);
new Task(ConsoleKeyReader).Start();
int count = 1;
while (true)
while (true)
{
await Task.Delay(100);
Write($"---------------- {count++} ------- (Esc to stop) ------");
Expand All @@ -163,16 +163,35 @@ public async Task Connect_Read_Services_Dispose_Loop()
{
var characteristics = await service.GetCharacteristicsAsync();
charlist.AddRange(characteristics);
foreach (Characteristic characteristic in characteristics)
{
if (characteristic.Properties.HasFlag(CharacteristicPropertyType.Indicate)
|| characteristic.Properties.HasFlag(CharacteristicPropertyType.Notify))
{
Write($"Characteristic.Properties: {characteristic.Properties}");
try
{
await characteristic.StartUpdatesAsync();
} catch { }
}
}
}

foreach (var service in services)
{
service.Dispose();
}
foreach (Characteristic characteristic in charlist)
{
try
{
await characteristic.StopUpdatesAsync();
} catch { }
}
charlist.Clear();
Write("Waiting 3 secs");
await Task.Delay(3000);
await Adapter.DisconnectDeviceAsync(dev);
await Adapter.DisconnectDeviceAsync(dev);
Write("Disposing");
dev.Dispose();
}
Expand Down
14 changes: 12 additions & 2 deletions Source/Plugin.BLE/Windows/Characteristic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,18 @@ protected override async Task StartUpdatesNativeAsync(CancellationToken cancella
NativeCharacteristic.ValueChanged -= OnCharacteristicValueChanged;
NativeCharacteristic.ValueChanged += OnCharacteristicValueChanged;

var result = await NativeCharacteristic.WriteClientCharacteristicConfigurationDescriptorWithResultAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
result.ThrowIfError();
if (Properties.HasFlag(CharacteristicPropertyType.Notify))
{
var result = await NativeCharacteristic.WriteClientCharacteristicConfigurationDescriptorWithResultAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
result.ThrowIfError();
} else if (Properties.HasFlag(CharacteristicPropertyType.Indicate))
{
var result = await NativeCharacteristic.WriteClientCharacteristicConfigurationDescriptorWithResultAsync(GattClientCharacteristicConfigurationDescriptorValue.Indicate);
result.ThrowIfError();
} else
{
throw new Exception($"StartUpdatesNativeAsync for {Uuid} failed since not Notify or Indicate");
}
}

protected override async Task StopUpdatesNativeAsync(CancellationToken cancellationToken = default)
Expand Down

0 comments on commit 74b0446

Please sign in to comment.