Skip to content

Commit

Permalink
Merge branch 'dotnet-bluetooth-le:master' into pair_connect_windows
Browse files Browse the repository at this point in the history
  • Loading branch information
AskBojesen authored Apr 22, 2024
2 parents 154988a + 9509a99 commit d7ba42d
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 8 deletions.
27 changes: 27 additions & 0 deletions Source/BLE.Client/BLE.Client.WinConsole/PluginDemos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ public async Task Connect_Disconnect()
Write("Test_Connect_Disconnect done");
}

public async Task ShowBondState()
{
string bleaddress = BleAddressSelector.GetBleAddress();
var id = bleaddress.ToBleDeviceGuid();
IDevice dev = await Adapter.ConnectToKnownDeviceAsync(id);
Write("BondState: " + dev.BondState);
dev.Dispose();
}

public async Task Connect_Read_Services_Disconnect_5X()
{
string bleaddress = BleAddressSelector.GetBleAddress();
Expand Down Expand Up @@ -211,6 +220,24 @@ public async Task Connect_Change_Parameters_Disconnect()
Write("Test_Connect_Disconnect done");
}

public async Task BondAsync()
{
string bleaddress = BleAddressSelector.GetBleAddress();
var id = bleaddress.ToBleDeviceGuid();
IDevice dev = await Adapter.ConnectToKnownDeviceAsync(id);
await Adapter.BondAsync(dev);
}

public Task GetBondedDevices()
{
int idx = 0;
foreach(var dev in Adapter.BondedDevices)
{
Write($"{idx++} Bonded device: {dev.Name} : {dev.Id}");
}
return Task.FromResult(true);
}

public async Task Pair_Connect_Disconnect()
{
string bleaddress = BleAddressSelector.GetBleAddress();
Expand Down
3 changes: 3 additions & 0 deletions Source/BLE.Client/BLE.Client.WinConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
{ConsoleKey.D7, new Demo("5X: Connect -> Read services -> Disconnect", ppemos.Connect_Read_Services_Disconnect_5X) },
{ConsoleKey.D8, new Demo("5X: Connect -> Read services -> Dispose", ppemos.Connect_Read_Services_Dispose_5X) },
{ConsoleKey.D9, new Demo("Connect -> Loop: ConnectionLost -> Connect", ppemos.Connect_ConnectionLost_Connect) },
{ConsoleKey.Q, new Demo("Adapter.BondAsync", ppemos.BondAsync) },
{ConsoleKey.W, new Demo("Adapter.BondedDevices", ppemos.GetBondedDevices) },
{ConsoleKey.E, new Demo("Device.BondState", ppemos.ShowBondState) },
{ConsoleKey.A, new Demo("Pure Windows: Connect -> Disconnect", wdemos.Connect_Disconnect) },
{ConsoleKey.S, new Demo("Pure Windows: Unpair all BLE devices", wdemos.UnPairAllBleDevices) },
};
Expand Down
3 changes: 3 additions & 0 deletions Source/Plugin.BLE/Shared/Contracts/IAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public interface IAdapter
event EventHandler<DeviceErrorEventArgs> DeviceConnectionError;
/// <summary>
/// Occurs when the bonding state of a device changed
/// Android: Supported
/// iOS: Not supported
/// Windows: Not supported
/// </summary>
event EventHandler<DeviceBondStateChangedEventArgs> DeviceBondStateChanged;
/// <summary>
Expand Down
54 changes: 47 additions & 7 deletions Source/Plugin.BLE/Windows/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,27 @@ public Adapter()
{
}

public override Task BondAsync(IDevice device)
public override async Task BondAsync(IDevice device)
{
throw new NotImplementedException();
var bleDevice = device.NativeDevice as BluetoothLEDevice;
if (bleDevice is null)
{
Trace.Message($"BondAsync failed since NativeDevice is null with: {device.Name}: {device.Id} ");
return;
}
DeviceInformation deviceInformation = await DeviceInformation.CreateFromIdAsync(bleDevice.DeviceId);
if (deviceInformation.Pairing.IsPaired)
{
Trace.Message($"BondAsync is already paired with: {device.Name}: {device.Id}");
return;
}
if (!deviceInformation.Pairing.CanPair)
{
Trace.Message($"BondAsync cannot pair with: {device.Name}: {device.Id}");
return;
}
DevicePairingResult result = await deviceInformation.Pairing.PairAsync();
Trace.Message($"BondAsync pairing result was {result.Status} with: {device.Name}: {device.Id}");
}

protected override Task StartScanningForDevicesNativeAsync(ScanFilterOptions scanFilterOptions, bool allowDuplicatesKey, CancellationToken scanCancellationToken)
Expand Down Expand Up @@ -165,6 +183,33 @@ public override async Task<IDevice> ConnectToKnownDeviceNativeAsync(Guid deviceG
return knownDevice;
}

protected override IReadOnlyList<IDevice> GetBondedDevices()
{
string pairedSelector = BluetoothLEDevice.GetDeviceSelectorFromPairingState(true);
DeviceInformationCollection pairedDevices = DeviceInformation.FindAllAsync(pairedSelector).GetAwaiter().GetResult();
List<IDevice> devlist = new List<IDevice>();
foreach (var dev in pairedDevices)
{
Guid id = dev.Id.ToBleDeviceGuidFromId();
ulong bleaddress = id.ToBleAddress();
var bluetoothLeDevice = BluetoothLEDevice.FromBluetoothAddressAsync(bleaddress).AsTask().Result;
if (bluetoothLeDevice != null)
{
var device = new Device(
this,
bluetoothLeDevice,
0, id);
devlist.Add(device);
Trace.Message("GetBondedDevices: {0}: {1}", dev.Id, dev.Name);
}
else
{
Trace.Message("GetBondedDevices: {0}: {1}, BluetoothLEDevice == null", dev.Id, dev.Name);
}
}
return devlist;
}

public override IReadOnlyList<IDevice> GetSystemConnectedOrPairedDevices(Guid[] services = null)
{
string pairedSelector = BluetoothLEDevice.GetDeviceSelectorFromPairingState(true);
Expand Down Expand Up @@ -198,11 +243,6 @@ public override IReadOnlyList<IDevice> GetSystemConnectedOrPairedDevices(Guid[]
return devlist;
}

protected override IReadOnlyList<IDevice> GetBondedDevices()
{
return null; // not supported
}

/// <summary>
/// Parses a given advertisement for various stored properties
/// Currently only parses the manufacturer specific data
Expand Down
11 changes: 10 additions & 1 deletion Source/Plugin.BLE/Windows/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,16 @@ public override void Dispose()

protected override DeviceBondState GetBondState()
{
return DeviceBondState.NotSupported;
try
{
DeviceInformation deviceInformation = DeviceInformation.CreateFromIdAsync(NativeDevice.DeviceId).AsTask().Result;
return deviceInformation.Pairing.IsPaired ? DeviceBondState.Bonded : DeviceBondState.NotBonded;
}
catch (Exception ex)
{
Trace.Message($"GetBondState exception for {NativeDevice.DeviceId} : {ex.Message}");
return DeviceBondState.NotSupported;
}
}

public override bool UpdateConnectionParameters(ConnectParameters connectParameters = default)
Expand Down

0 comments on commit d7ba42d

Please sign in to comment.