Add WebBluetoothGloballyDisabled exception and fix availability state (#165)
* Add WebBluetoothGloballyDisabled exception and fix availability state * Update changelog and version --------- Co-authored-by: Foti Dim <foti@navideck.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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<AvailabilityState> 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
|
||||
|
||||
+2
-2
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user