From 921dc0fa880dafa989f0c243e7c816d8068b6c37 Mon Sep 17 00:00:00 2001 From: iAMD Date: Wed, 17 Jan 2024 19:06:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20CoW=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20(#40)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bluetooth_low_energy/CHANGELOG.md | 4 + bluetooth_low_energy/example/lib/main.dart | 28 +++--- bluetooth_low_energy/example/pubspec.lock | 6 +- bluetooth_low_energy/pubspec.yaml | 4 +- bluetooth_low_energy_darwin/CHANGELOG.md | 4 + .../darwin/Classes/MyCentralManager.swift | 14 +-- .../darwin/Classes/MyPeripheralManager.swift | 97 ++++++++----------- .../example/pubspec.lock | 2 +- bluetooth_low_energy_darwin/pubspec.yaml | 2 +- 9 files changed, 74 insertions(+), 87 deletions(-) diff --git a/bluetooth_low_energy/CHANGELOG.md b/bluetooth_low_energy/CHANGELOG.md index b3f6ffe..ddabe39 100644 --- a/bluetooth_low_energy/CHANGELOG.md +++ b/bluetooth_low_energy/CHANGELOG.md @@ -1,3 +1,7 @@ +## 5.0.4 + +* `iOS` Fix issues caused by CoW. + ## 5.0.3 * `Android` Fix the wrong blutooth low energy state caused by multi permission requests at the same time. diff --git a/bluetooth_low_energy/example/lib/main.dart b/bluetooth_low_energy/example/lib/main.dart index 0273d62..4dbf7a9 100644 --- a/bluetooth_low_energy/example/lib/main.dart +++ b/bluetooth_low_energy/example/lib/main.dart @@ -979,9 +979,21 @@ class _AdvertiserViewState extends State Future startAdvertising() async { await PeripheralManager.instance.clearServices(); + final batteryService = GattService( + uuid: UUID.short(0x180f), + characteristics: [ + GattCharacteristic( + uuid: UUID.short(0x2A19), + properties: [GattCharacteristicProperty.read], + value: Uint8List.fromList([75]), + descriptors: [], + ), + ], + ); + await PeripheralManager.instance.addService(batteryService); final elements = List.generate(1000, (i) => i % 256); final value = Uint8List.fromList(elements); - final service = GattService( + final myService = GattService( uuid: UUID.short(100), characteristics: [ GattCharacteristic( @@ -1028,19 +1040,7 @@ class _AdvertiserViewState extends State ), ], ); - await PeripheralManager.instance.addService(service); - final batteryService = GattService( - uuid: UUID.short(0x180f), - characteristics: [ - GattCharacteristic( - uuid: UUID.short(0x2A19), - properties: [GattCharacteristicProperty.read], - value: Uint8List.fromList([75]), - descriptors: [], - ), - ], - ); - await PeripheralManager.instance.addService(batteryService); + await PeripheralManager.instance.addService(myService); final advertisement = Advertisement( name: 'le12138', manufacturerSpecificData: ManufacturerSpecificData( diff --git a/bluetooth_low_energy/example/pubspec.lock b/bluetooth_low_energy/example/pubspec.lock index 5539692..ff1603c 100644 --- a/bluetooth_low_energy/example/pubspec.lock +++ b/bluetooth_low_energy/example/pubspec.lock @@ -23,7 +23,7 @@ packages: path: ".." relative: true source: path - version: "5.0.3" + version: "5.0.4" bluetooth_low_energy_android: dependency: transitive description: @@ -36,10 +36,10 @@ packages: dependency: transitive description: name: bluetooth_low_energy_darwin - sha256: "876fd1c7eadd5e4eea92257dd3d2ec31f3c0bb68f8e96c564ed28ecde7382bd1" + sha256: e85bc6e97a087f2bb934a1b12953a0f6aab8dfdbe57302cb2bf6d61b996fa2ee url: "https://pub.dev" source: hosted - version: "5.0.2" + version: "5.0.3" bluetooth_low_energy_linux: dependency: transitive description: diff --git a/bluetooth_low_energy/pubspec.yaml b/bluetooth_low_energy/pubspec.yaml index a7f0184..5350fda 100644 --- a/bluetooth_low_energy/pubspec.yaml +++ b/bluetooth_low_energy/pubspec.yaml @@ -1,6 +1,6 @@ name: bluetooth_low_energy description: A Flutter plugin for controlling the bluetooth low energy, supports central and peripheral apis. -version: 5.0.3 +version: 5.0.4 homepage: https://github.com/yanshouwang/bluetooth_low_energy environment: @@ -12,7 +12,7 @@ dependencies: sdk: flutter bluetooth_low_energy_platform_interface: ^5.0.0 bluetooth_low_energy_android: ^5.0.2 - bluetooth_low_energy_darwin: ^5.0.2 + bluetooth_low_energy_darwin: ^5.0.3 bluetooth_low_energy_windows: ^5.0.0 bluetooth_low_energy_linux: ^5.0.0 diff --git a/bluetooth_low_energy_darwin/CHANGELOG.md b/bluetooth_low_energy_darwin/CHANGELOG.md index 5d77c6d..b7118d4 100644 --- a/bluetooth_low_energy_darwin/CHANGELOG.md +++ b/bluetooth_low_energy_darwin/CHANGELOG.md @@ -1,3 +1,7 @@ +## 5.0.3 + +* Fix issues caused by CoW. + ## 5.0.2 * Fix the issue that [discoverGATT failed caused by CoW](https://github.com/yanshouwang/bluetooth_low_energy/issues/36). diff --git a/bluetooth_low_energy_darwin/darwin/Classes/MyCentralManager.swift b/bluetooth_low_energy_darwin/darwin/Classes/MyCentralManager.swift index f27daa3..fc931aa 100644 --- a/bluetooth_low_energy_darwin/darwin/Classes/MyCentralManager.swift +++ b/bluetooth_low_energy_darwin/darwin/Classes/MyCentralManager.swift @@ -67,7 +67,7 @@ class MyCentralManager: MyCentralManagerHostApi { if _centralManager.delegate == nil { _centralManager.delegate = _centralManagerDelegate } - _onStateChanged() + didUpdateState(central: _centralManager) } func startDiscovery() throws { @@ -259,7 +259,10 @@ class MyCentralManager: MyCentralManagerHostApi { } func didUpdateState(central: CBCentralManager) { - _onStateChanged() + let state = central.state + let stateArgs = state.toArgs() + let stateNumberArgs = stateArgs.rawValue.toInt64() + _api.onStateChanged(stateNumberArgs: stateNumberArgs) {_ in } } func didDiscover(central: CBCentralManager, peripheral: CBPeripheral, advertisementData: [String : Any], rssi: NSNumber) { @@ -588,11 +591,4 @@ class MyCentralManager: MyCentralManagerHostApi { } return descriptors[hashCodeArgs] } - - private func _onStateChanged() { - let state = _centralManager.state - let stateArgs = state.toArgs() - let stateNumberArgs = stateArgs.rawValue.toInt64() - _api.onStateChanged(stateNumberArgs: stateNumberArgs) {_ in } - } } diff --git a/bluetooth_low_energy_darwin/darwin/Classes/MyPeripheralManager.swift b/bluetooth_low_energy_darwin/darwin/Classes/MyPeripheralManager.swift index e5d212b..e34f8da 100644 --- a/bluetooth_low_energy_darwin/darwin/Classes/MyPeripheralManager.swift +++ b/bluetooth_low_energy_darwin/darwin/Classes/MyPeripheralManager.swift @@ -60,7 +60,7 @@ class MyPeripheralManager: MyPeripheralManagerHostApi { if _peripheralManager.delegate == nil { _peripheralManager.delegate = _peripheralManagerDelegate } - _onStateChanged() + didUpdateState(peripheral: _peripheralManager) } func addService(serviceArgs: MyGattServiceArgs, completion: @escaping (Result) -> Void) { @@ -102,23 +102,48 @@ class MyPeripheralManager: MyPeripheralManagerHostApi { } func removeService(hashCodeArgs: Int64) throws { - guard let service = _services[hashCodeArgs] else { - throw MyError.illegalArgument - } - let hashCode = service.hash - guard let serviceArgs = _servicesArgs[hashCode] else { + guard let service = _services.removeValue(forKey: hashCodeArgs) else { throw MyError.illegalArgument } _peripheralManager.remove(service) - _clearService(serviceArgs: serviceArgs) + let hashCode = service.hash + guard let serviceArgs = _servicesArgs.removeValue(forKey: hashCode) else { + throw MyError.illegalArgument + } + for args in serviceArgs.characteristicsArgs { + guard let characteristicArgs = args else { + continue + } + let characteristicHashCodeArgs = characteristicArgs.hashCodeArgs + guard let characteristic = _characteristics.removeValue(forKey: characteristicHashCodeArgs) else { + throw MyError.illegalArgument + } + let characteristicHashCode = characteristic.hash + _characteristicsArgs.removeValue(forKey: characteristicHashCode) + for args in characteristicArgs.descriptorsArgs { + guard let descriptorArgs = args else { + continue + } + let descriptorHashCodeArgs = descriptorArgs.hashCodeArgs + guard let descriptor = _descriptors.removeValue(forKey: descriptorHashCodeArgs) else { + throw MyError.illegalArgument + } + let descriptorHashCode = descriptor.hash + _descriptorsArgs.removeValue(forKey: descriptorHashCode) + } + } } func clearServices() throws { _peripheralManager.removeAllServices() - let servicesArgs = _servicesArgs.values - for serviceArgs in servicesArgs { - _clearService(serviceArgs: serviceArgs) - } + + _services.removeAll() + _characteristics.removeAll() + _descriptors.removeAll() + + _servicesArgs.removeAll() + _characteristicsArgs.removeAll() + _descriptors.removeAll() } func startAdvertising(advertisementArgs: MyAdvertisementArgs, completion: @escaping (Result) -> Void) { @@ -181,7 +206,10 @@ class MyPeripheralManager: MyPeripheralManagerHostApi { } func didUpdateState(peripheral: CBPeripheralManager) { - _onStateChanged() + let state = peripheral.state + let stateArgs = state.toArgs() + let stateNumberArgs = stateArgs.rawValue.toInt64() + _api.onStateChanged(stateNumberArgs: stateNumberArgs) {_ in } } func didAdd(peripheral: CBPeripheralManager, service: CBService, error: Error?) { @@ -193,11 +221,6 @@ class MyPeripheralManager: MyPeripheralManagerHostApi { completion(.success(())) } else { completion(.failure(error!)) - let hashCode = service.hash - guard let serviceArgs = _servicesArgs[hashCode] else { - return - } - _clearService(serviceArgs: serviceArgs) } } @@ -317,44 +340,4 @@ class MyPeripheralManager: MyPeripheralManagerHostApi { _startAdvertisingCompletion = nil _isReadyToUpdateSubscribersCallbacks.removeAll() } - - private func _clearService(serviceArgs: MyGattServiceArgs) { - let characteristicsArgs = serviceArgs.characteristicsArgs - for args in characteristicsArgs { - guard let characteristicArgs = args else { - continue - } - let descriptorsArgs = characteristicArgs.descriptorsArgs - for args in descriptorsArgs { - guard let descriptorArgs = args else { - continue - } - let descriptorHashCodeArgs = descriptorArgs.hashCodeArgs - guard let descriptor = _descriptors.removeValue(forKey: descriptorHashCodeArgs) else { - continue - } - let descriptorHashCode = descriptor.hash - _descriptorsArgs.removeValue(forKey: descriptorHashCode) - } - let characteristicHashCodeArgs = characteristicArgs.hashCodeArgs - guard let characteristic = _characteristics.removeValue(forKey: characteristicHashCodeArgs) else { - continue - } - let characteristicHashCode = characteristic.hash - _characteristicsArgs.removeValue(forKey: characteristicHashCode) - } - let serviceHashCodeArgs = serviceArgs.hashCodeArgs - guard let service = _services.removeValue(forKey: serviceHashCodeArgs) else { - return - } - let serviceHashCode = service.hash - _servicesArgs.removeValue(forKey: serviceHashCode) - } - - private func _onStateChanged() { - let state = _peripheralManager.state - let stateArgs = state.toArgs() - let stateNumberArgs = stateArgs.rawValue.toInt64() - _api.onStateChanged(stateNumberArgs: stateNumberArgs) {_ in } - } } diff --git a/bluetooth_low_energy_darwin/example/pubspec.lock b/bluetooth_low_energy_darwin/example/pubspec.lock index 17db728..688c6ed 100644 --- a/bluetooth_low_energy_darwin/example/pubspec.lock +++ b/bluetooth_low_energy_darwin/example/pubspec.lock @@ -15,7 +15,7 @@ packages: path: ".." relative: true source: path - version: "5.0.2" + version: "5.0.3" bluetooth_low_energy_platform_interface: dependency: "direct main" description: diff --git a/bluetooth_low_energy_darwin/pubspec.yaml b/bluetooth_low_energy_darwin/pubspec.yaml index b3f5ded..1f79d24 100644 --- a/bluetooth_low_energy_darwin/pubspec.yaml +++ b/bluetooth_low_energy_darwin/pubspec.yaml @@ -1,6 +1,6 @@ name: bluetooth_low_energy_darwin description: iOS and macOS implementation of the bluetooth_low_energy plugin. -version: 5.0.2 +version: 5.0.3 homepage: https://github.com/yanshouwang/bluetooth_low_energy environment: