diff --git a/Source/Plugin.BLE/Android/Adapter.cs b/Source/Plugin.BLE/Android/Adapter.cs index 69f3f93e..cf6f31c3 100644 --- a/Source/Plugin.BLE/Android/Adapter.cs +++ b/Source/Plugin.BLE/Android/Adapter.cs @@ -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; @@ -306,6 +307,8 @@ protected override void DisconnectDeviceNative(IDevice device) 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); @@ -320,9 +323,9 @@ public override IReadOnlyList 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().ToList(); } @@ -335,7 +338,7 @@ public override IReadOnlyList GetKnownDevicesByIds(Guid[] ids) protected override IReadOnlyList 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().ToList(); } diff --git a/Source/Plugin.BLE/Android/Extensions/BluetoothDeviceExtension.cs b/Source/Plugin.BLE/Android/Extensions/BluetoothDeviceExtension.cs new file mode 100644 index 00000000..2e869a8c --- /dev/null +++ b/Source/Plugin.BLE/Android/Extensions/BluetoothDeviceExtension.cs @@ -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; + } + } +}