From f7c05b2490de2a9067bf6fccff43c4ad28393ea8 Mon Sep 17 00:00:00 2001 From: Rohit Sangwan Date: Tue, 13 Aug 2024 19:49:21 +0530 Subject: [PATCH] Remove error from onPairingStateChange (#75) * Remove error from onPairingStateChange * Update ChangeLog and Readme * Update Changelog --- CHANGELOG.md | 1 + README.md | 2 +- example/lib/data/mock_universal_ble.dart | 4 ++-- .../lib/peripheral_details/peripheral_detail_page.dart | 9 ++------- lib/src/universal_ble.dart | 4 ++-- lib/src/universal_ble_linux/universal_ble_linux.dart | 4 ++-- .../universal_ble_pigeon_channel.dart | 2 +- lib/src/universal_ble_platform_interface.dart | 10 ++++++---- 8 files changed, 17 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 873b8fe..3e2f3c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## 0.12.0 * BREAKING CHANGE: `unPair` is now `unpair` +* BREAKING CHANGE: `onPairingStateChange` does not return error anymore * Add `pair()`, `isPaired` and `onPairingStateChange` support for Apple and web * `connect()` and `pair()` now return a bool result * Add `PlatformConfig` property in `StartScan` diff --git a/README.md b/README.md index e468b7a..677d632 100644 --- a/README.md +++ b/README.md @@ -203,7 +203,7 @@ bool? isPaired = await UniversalBle.pair(deviceId); // Returns true if successfu UniversalBle.pair(deviceId, pairingCommand: BleCommand(service:"SERVICE", characteristic:"ENCRYPTED_CHARACTERISTIC",)); // Receive pairing state changes -UniversalBle.onPairingStateChange = (String deviceId, bool isPaired, String? error) { +UniversalBle.onPairingStateChange = (String deviceId, bool isPaired) { // Handle pairing state change } diff --git a/example/lib/data/mock_universal_ble.dart b/example/lib/data/mock_universal_ble.dart index d1c9ec3..f0a8c6f 100644 --- a/example/lib/data/mock_universal_ble.dart +++ b/example/lib/data/mock_universal_ble.dart @@ -103,13 +103,13 @@ class MockUniversalBle extends UniversalBlePlatform { @override Future pair(String deviceId) async { - updatePairingState(deviceId, true, null); + updatePairingState(deviceId, true); return true; } @override Future unpair(String deviceId) async { - updatePairingState(deviceId, false, null); + updatePairingState(deviceId, false); } @override diff --git a/example/lib/peripheral_details/peripheral_detail_page.dart b/example/lib/peripheral_details/peripheral_detail_page.dart index 480b3f7..acedda0 100644 --- a/example/lib/peripheral_details/peripheral_detail_page.dart +++ b/example/lib/peripheral_details/peripheral_detail_page.dart @@ -81,14 +81,9 @@ class _PeripheralDetailPageState extends State { _addLog("Value", data); } - void _handlePairingStateChange( - String deviceId, bool isPaired, String? error) { + void _handlePairingStateChange(String deviceId, bool isPaired) { print('isPaired $deviceId, $isPaired'); - if (error != null && error.isNotEmpty) { - _addLog("PairingStateChangeError", "(Paired: $isPaired): $error "); - } else { - _addLog("PairingStateChange - isPaired", isPaired); - } + _addLog("PairingStateChange - isPaired", isPaired); } Future _discoverServices() async { diff --git a/lib/src/universal_ble.dart b/lib/src/universal_ble.dart index 508c6db..8354982 100644 --- a/lib/src/universal_ble.dart +++ b/lib/src/universal_ble.dart @@ -325,7 +325,7 @@ class UniversalBle { bool commandResult = await _executeBleCommand(deviceId, services, bleCommand); if (updateCallbackValue) { - _platform.updatePairingState(deviceId, commandResult, null); + _platform.updatePairingState(deviceId, commandResult); } return commandResult; } @@ -334,7 +334,7 @@ class UniversalBle { "FailedToPerform EncryptedCharOperation: $e", ); if (updateCallbackValue) { - _platform.updatePairingState(deviceId, false, e.toString()); + _platform.updatePairingState(deviceId, false); } return false; } diff --git a/lib/src/universal_ble_linux/universal_ble_linux.dart b/lib/src/universal_ble_linux/universal_ble_linux.dart index 1cfa442..55f2cee 100644 --- a/lib/src/universal_ble_linux/universal_ble_linux.dart +++ b/lib/src/universal_ble_linux/universal_ble_linux.dart @@ -281,7 +281,7 @@ class UniversalBleLinux extends UniversalBlePlatform { await device.pair(); return true; } catch (error) { - updatePairingState(deviceId, false, error.toString()); + updatePairingState(deviceId, false); return false; } } @@ -448,7 +448,7 @@ class UniversalBleLinux extends UniversalBlePlatform { updateScanResult(device.toBleDevice()); break; case BluezProperty.paired: - updatePairingState(device.address, device.paired, null); + updatePairingState(device.address, device.paired); break; // Ignored these properties updates case BluezProperty.bonded: diff --git a/lib/src/universal_ble_pigeon/universal_ble_pigeon_channel.dart b/lib/src/universal_ble_pigeon/universal_ble_pigeon_channel.dart index b8dcd35..ad33bf3 100644 --- a/lib/src/universal_ble_pigeon/universal_ble_pigeon_channel.dart +++ b/lib/src/universal_ble_pigeon/universal_ble_pigeon_channel.dart @@ -190,7 +190,7 @@ class _UniversalBleCallbackHandler extends UniversalBleCallbackChannel { @override void onPairStateChange(String deviceId, bool isPaired, String? error) => - pairStateChange(deviceId, isPaired, error); + pairStateChange(deviceId, isPaired); } extension _UniversalBleScanResultExtension on UniversalBleScanResult { diff --git a/lib/src/universal_ble_platform_interface.dart b/lib/src/universal_ble_platform_interface.dart index 1adce7c..e8bf9c2 100644 --- a/lib/src/universal_ble_platform_interface.dart +++ b/lib/src/universal_ble_platform_interface.dart @@ -6,6 +6,7 @@ import 'package:universal_ble/universal_ble.dart'; abstract class UniversalBlePlatform { ScanFilter? _scanFilter; StreamController? _connectionStreamController; + final Map _pairStateMap = {}; Future getBluetoothAvailabilityState(); @@ -93,8 +94,10 @@ abstract class UniversalBlePlatform { onAvailabilityChange?.call(state); } - void updatePairingState(String deviceId, bool isPaired, String? error) { - onPairingStateChange?.call(deviceId, isPaired, error); + void updatePairingState(String deviceId, bool isPaired) { + if (_pairStateMap[deviceId] == isPaired) return; + _pairStateMap[deviceId] = isPaired; + onPairingStateChange?.call(deviceId, isPaired); } // Do not use these directly to push updates @@ -135,7 +138,6 @@ typedef OnScanResult = void Function(BleDevice scanResult); typedef OnAvailabilityChange = void Function(AvailabilityState state); -typedef OnPairingStateChange = void Function( - String deviceId, bool isPaired, String? error); +typedef OnPairingStateChange = void Function(String deviceId, bool isPaired); typedef OnQueueUpdate = void Function(String id, int remainingQueueItems);