From 72090571a37ce17691cca4aff173c298899a0d84 Mon Sep 17 00:00:00 2001 From: Rohit Sangwan Date: Fri, 13 Jun 2025 04:00:13 +0530 Subject: [PATCH] Add WebBluetoothGloballyDisabled exception and fix availability state (#165) * Add WebBluetoothGloballyDisabled exception and fix availability state * Update changelog and version --------- Co-authored-by: Foti Dim --- CHANGELOG.md | 4 ++ example/pubspec.lock | 6 +-- lib/src/universal_ble_exceptions.dart | 8 ++++ .../universal_ble_web/universal_ble_web.dart | 41 ++++++++++--------- pubspec.yaml | 4 +- 5 files changed, 38 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed67d6e..a3f9d6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.20 +* Fix `getBluetoothAvailabilityState` reporting wrong Bluetooth status on browsers where Web Bluetooth can be globally disabled +* `startScan` API on Web platforms now throws a `WebBluetoothGloballyDisabled` exception if scanning cannot proceed due to Web Bluetooth being globally disabled + ## 0.19.0 * Get and prefer advertised name of scanned devices on Apple * Improve Android runtime permissions docs diff --git a/example/pubspec.lock b/example/pubspec.lock index 9a5b439..0488018 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -156,10 +156,10 @@ packages: dependency: transitive description: name: flutter_web_bluetooth - sha256: "1363831def5eed1e1064d1eca04e8ccb35446e8f758579c3c519e156b77926da" + sha256: ad26a1b3fef95b86ea5f63793b9a0cdc1a33490f35d754e4e711046cae3ebbf8 url: "https://pub.dev" source: hosted - version: "1.0.0" + version: "1.1.0" flutter_web_plugins: dependency: transitive description: flutter @@ -402,7 +402,7 @@ packages: path: ".." relative: true source: path - version: "0.19.0" + version: "0.19.1" vector_math: dependency: transitive description: diff --git a/lib/src/universal_ble_exceptions.dart b/lib/src/universal_ble_exceptions.dart index 15275b9..dc3fd3c 100644 --- a/lib/src/universal_ble_exceptions.dart +++ b/lib/src/universal_ble_exceptions.dart @@ -22,6 +22,14 @@ class PairingException implements Exception { String toString() => message; } +class WebBluetoothGloballyDisabled implements Exception { + String message; + WebBluetoothGloballyDisabled(this.message); + + @override + String toString() => message; +} + String _errorParser(dynamic error) { if (error == null) { return "Failed"; diff --git a/lib/src/universal_ble_web/universal_ble_web.dart b/lib/src/universal_ble_web/universal_ble_web.dart index 65afb17..4749cf3 100644 --- a/lib/src/universal_ble_web/universal_ble_web.dart +++ b/lib/src/universal_ble_web/universal_ble_web.dart @@ -3,9 +3,8 @@ import 'dart:collection'; import 'package:flutter/foundation.dart'; import 'package:flutter_web_bluetooth/flutter_web_bluetooth.dart'; -import 'package:universal_ble/src/models/model_exports.dart'; -import 'package:universal_ble/src/universal_ble_platform_interface.dart'; import 'package:universal_ble/src/universal_logger.dart'; +import 'package:universal_ble/universal_ble.dart'; class UniversalBleWeb extends UniversalBlePlatform { static UniversalBleWeb? _instance; @@ -64,15 +63,9 @@ class UniversalBleWeb extends UniversalBlePlatform { @override Future getBluetoothAvailabilityState() async { bool isSupported = FlutterWebBluetooth.instance.isBluetoothApiSupported; - if (!isSupported) { - return AvailabilityState.unsupported; - } - bool isAvailable = await FlutterWebBluetooth.instance.isAvailable.first; - if (isSupported && !isAvailable) { - return AvailabilityState.poweredOff; - } else if (isAvailable) { - return AvailabilityState.poweredOn; - } + if (!isSupported) return AvailabilityState.unsupported; + bool isAvailable = await FlutterWebBluetooth.instance.getAvailability(); + if (isAvailable) return AvailabilityState.poweredOn; return AvailabilityState.unknown; } @@ -81,18 +74,26 @@ class UniversalBleWeb extends UniversalBlePlatform { ScanFilter? scanFilter, PlatformConfig? platformConfig, }) async { - FlutterWebBluetooth.instance.isAvailable; - BluetoothDevice device = await FlutterWebBluetooth.instance.requestDevice( - _getRequestOptionBuilder(scanFilter, platformConfig?.web), - ); + try { + FlutterWebBluetooth.instance.isAvailable; + BluetoothDevice device = await FlutterWebBluetooth.instance.requestDevice( + _getRequestOptionBuilder(scanFilter, platformConfig?.web), + ); - // Update local device list - _bluetoothDeviceList[device.id] = device; + // Update local device list + _bluetoothDeviceList[device.id] = device; - // Update Scan Result - updateScanResult(device.toBleScanResult()); + // Update Scan Result + updateScanResult(device.toBleScanResult()); - _watchDeviceAdvertisements(device); + _watchDeviceAdvertisements(device); + } catch (e) { + String error = e.toString().replaceAll("DeviceNotFoundError:", "").trim(); + if (error.toLowerCase().contains("api globally disabled")) { + throw WebBluetoothGloballyDisabled(error); + } + rethrow; + } } @override diff --git a/pubspec.yaml b/pubspec.yaml index b6aa01e..fb0ace1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: universal_ble description: A cross-platform (Android/iOS/macOS/Windows/Linux/Web) Bluetooth Low Energy (BLE) plugin for Flutter -version: 0.19.0 +version: 0.20.0 homepage: https://navideck.com repository: https://github.com/Navideck/universal_ble issue_tracker: https://github.com/Navideck/universal_ble/issues @@ -31,7 +31,7 @@ dependencies: flutter: sdk: flutter plugin_platform_interface: ^2.1.6 - flutter_web_bluetooth: ^1.0.0 + flutter_web_bluetooth: ^1.1.0 bluez: ^0.8.3 dev_dependencies: