Skip to content

Commit

Permalink
Merge pull request #855 from alex3696/master
Browse files Browse the repository at this point in the history
fix #854 - Bluetooth stack breaks when trying connect on phone boot
  • Loading branch information
janusw committed May 19, 2024
2 parents 8cc17fc + 0341a8c commit 1e19fa8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Source/Plugin.BLE/Android/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Java.Util;
using Plugin.BLE.Abstractions;
using Plugin.BLE.Abstractions.Contracts;
using Plugin.BLE.Android.Extensions;
using Plugin.BLE.BroadcastReceivers;
using Plugin.BLE.Extensions;
using Object = Java.Lang.Object;
Expand Down Expand Up @@ -306,6 +307,8 @@ public override async Task<IDevice> ConnectToKnownDeviceNativeAsync(Guid deviceG
var nativeDevice = _bluetoothAdapter.GetRemoteDevice(macBytes);
if (nativeDevice == null)
throw new Abstractions.Exceptions.DeviceConnectionException(deviceGuid,"", $"[Adapter] Device {deviceGuid} not found.");
if (!nativeDevice.SupportsBLE())
throw new Abstractions.Exceptions.DeviceConnectionException(deviceGuid,"", $"[Adapter] Device {deviceGuid} does not support BLE.");
var device = new Device(this, nativeDevice, null);

await ConnectToDeviceAsync(device, connectParameters, cancellationToken);
Expand All @@ -320,9 +323,9 @@ public override IReadOnlyList<IDevice> GetSystemConnectedOrPairedDevices(Guid[]
}

//add dualMode type too as they are BLE too ;)
var connectedDevices = _bluetoothManager.GetConnectedDevices(ProfileType.Gatt).Where(d => d.Type == BluetoothDeviceType.Le || d.Type == BluetoothDeviceType.Dual);
var connectedDevices = _bluetoothManager.GetConnectedDevices(ProfileType.Gatt).Where(d => d.SupportsBLE());

var bondedDevices = _bluetoothAdapter.BondedDevices.Where(d => d.Type == BluetoothDeviceType.Le || d.Type == BluetoothDeviceType.Dual);
var bondedDevices = _bluetoothAdapter.BondedDevices.Where(d => d.SupportsBLE());

return connectedDevices.Union(bondedDevices, new DeviceComparer()).Select(d => new Device(this, d, null)).Cast<IDevice>().ToList();
}
Expand All @@ -335,7 +338,7 @@ public override IReadOnlyList<IDevice> GetKnownDevicesByIds(Guid[] ids)

protected override IReadOnlyList<IDevice> GetBondedDevices()
{
var bondedDevices = _bluetoothAdapter.BondedDevices.Where(d => d.Type == BluetoothDeviceType.Le || d.Type == BluetoothDeviceType.Dual);
var bondedDevices = _bluetoothAdapter.BondedDevices.Where(d => d.SupportsBLE());

return bondedDevices.Select(d => new Device(this, d, null, 0)).Cast<IDevice>().ToList();
}
Expand Down
12 changes: 12 additions & 0 deletions Source/Plugin.BLE/Android/Extensions/BluetoothDeviceExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Android.Bluetooth;

namespace Plugin.BLE.Android.Extensions
{
public static class BluetoothDeviceExtension
{
public static bool SupportsBLE(this BluetoothDevice d)
{
return d.Type == BluetoothDeviceType.Le || d.Type == BluetoothDeviceType.Dual;
}
}
}

0 comments on commit 1e19fa8

Please sign in to comment.