Skip to content

Commit

Permalink
Merge branch 'dotnet-bluetooth-le:master' into demo_connection_lost_r…
Browse files Browse the repository at this point in the history
…econnect
  • Loading branch information
AskBojesen authored May 13, 2024
2 parents a9efc5a + 18d5e3f commit ee64c6c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
25 changes: 14 additions & 11 deletions Source/Plugin.BLE/Windows/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Plugin.BLE.Windows
{
public class Adapter : AdapterBase
{
private readonly BluetoothAdapter _bluetoothAdapter;
private BluetoothLEAdvertisementWatcher _bleWatcher;

/// <summary>
Expand All @@ -24,8 +25,9 @@ public class Adapter : AdapterBase
/// </summary>
private readonly IDictionary<string, IDevice> disconnectingRegistry = new ConcurrentDictionary<string, IDevice>();

public Adapter()
public Adapter(BluetoothAdapter adapter)
{
_bluetoothAdapter = adapter;
}

public override async Task BondAsync(IDevice device)
Expand Down Expand Up @@ -264,13 +266,14 @@ private void AdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, Blue
{
var deviceId = btAdv.BluetoothAddress.ParseDeviceId();

if (DiscoveredDevicesRegistry.TryGetValue(deviceId, out var device) && device != null)
if (DiscoveredDevicesRegistry.TryGetValue(deviceId, out var device))
{
// This deviceId has been discovered
Trace.Message("AdvReceived - Old: {0}", btAdv.ToDetailedString(device.Name));
(device as Device)?.Update(btAdv.RawSignalStrengthInDBm, ParseAdvertisementData(btAdv.Advertisement));
this.HandleDiscoveredDevice(device);
HandleDiscoveredDevice(device);
}
if (device == null)
else
{
var bluetoothLeDevice = BluetoothLEDevice.FromBluetoothAddressAsync(btAdv.BluetoothAddress).AsTask().Result;
if (bluetoothLeDevice != null) //make sure advertisement bluetooth address actually returns a device
Expand All @@ -283,12 +286,7 @@ private void AdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, Blue
ParseAdvertisementData(btAdv.Advertisement),
btAdv.IsConnectable);
Trace.Message("AdvReceived - New: {0}", btAdv.ToDetailedString(device.Name));
_ = DiscoveredDevicesRegistry.TryRemove(deviceId, out _);
this.HandleDiscoveredDevice(device);
}
else
{
DiscoveredDevicesRegistry[deviceId] = null;
HandleDiscoveredDevice(device);
}
}
}
Expand All @@ -298,5 +296,10 @@ public override IReadOnlyList<IDevice> GetKnownDevicesByIds(Guid[] ids)
// TODO: implement this
return new List<IDevice>();
}

public override bool SupportsExtendedAdvertising()
{
return _bluetoothAdapter.IsExtendedAdvertisingSupported;
}
}
}
}
2 changes: 1 addition & 1 deletion Source/Plugin.BLE/Windows/BleImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class BleImplementation : BleImplementationBase

protected override IAdapter CreateNativeAdapter()
{
return new Adapter();
return new Adapter(btAdapter);
}

protected override BluetoothState GetInitialStateNative()
Expand Down

0 comments on commit ee64c6c

Please sign in to comment.