From 694262d9a90d649d18de25b8bfaf563a1fbd3d37 Mon Sep 17 00:00:00 2001 From: Foti Dim Date: Wed, 18 Sep 2024 06:49:11 +0200 Subject: [PATCH] Allow pairing on all platforms Mark Web/Linux as not supporting in-app pairing --- lib/src/models/ble_capabilities.dart | 12 ++++++++++-- lib/src/universal_ble.dart | 5 +---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/src/models/ble_capabilities.dart b/lib/src/models/ble_capabilities.dart index c6be310..99f58cd 100644 --- a/lib/src/models/ble_capabilities.dart +++ b/lib/src/models/ble_capabilities.dart @@ -2,12 +2,20 @@ import 'package:flutter/foundation.dart'; class BleCapabilities { /// Returns true if pairing is possible either by API or by encrypted characteristic. - /// All platforms support this except Windows/Web. + /// + /// All platforms support this except Web/Windows and Web/Linux. + /// + /// On `Web/Windows` and `Web/Linux` it is known to only work for devices that require passkey pairing. + /// If your device requires passkey pairing you can consider this true for all platforms. + /// + /// `Web/Linux` could under certain circumstances present a pairing dialog also for devices that do + /// not use passkey pairing but it very unreliable so we consider it unsupported. static final bool supportsInAppPairing = _triggersPairingWithEncryptedChar || hasSystemPairingApi; static final _triggersPairingWithEncryptedChar = - defaultTargetPlatform != TargetPlatform.windows; + defaultTargetPlatform != TargetPlatform.windows || + defaultTargetPlatform != TargetPlatform.linux; static bool hasSystemPairingApi = !kIsWeb && (defaultTargetPlatform == TargetPlatform.android || diff --git a/lib/src/universal_ble.dart b/lib/src/universal_ble.dart index f744cce..3363cc1 100644 --- a/lib/src/universal_ble.dart +++ b/lib/src/universal_ble.dart @@ -232,14 +232,11 @@ class UniversalBle { /// You can optionally pass a pairingCommand if you know an encrypted read or write characteristic. /// If you do, it returns true if it can successfully execute the command after pairing. /// - /// Throws UnsupportedError on `Web/Windows` + /// On `Web/Windows` and `Web/Linux` it is known to work only for devices that require passkey pairing. static Future pair( String deviceId, { BleCommand? pairingCommand, }) async { - if (!BleCapabilities.supportsInAppPairing) { - throw UnsupportedError("Not supported"); - } if (BleCapabilities.hasSystemPairingApi) { return _platform.pair(deviceId); }