From 6f0bc77ac987c0c2ed6f32be5d56704dddb4d1c3 Mon Sep 17 00:00:00 2001 From: iAMD Date: Tue, 16 Jan 2024 17:59:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20CoW=20=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=20discoverGATT=20=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(#38)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复 CoW 导致 discoverGATT 失败的问题 * 修复 CoW 导致 discoverGATT 失败的问题 --- bluetooth_low_energy/CHANGELOG.md | 4 +++ bluetooth_low_energy/example/lib/main.dart | 12 +++++++++ bluetooth_low_energy/example/pubspec.lock | 6 ++--- bluetooth_low_energy/pubspec.yaml | 4 +-- bluetooth_low_energy_darwin/CHANGELOG.md | 4 +++ .../darwin/Classes/MyCentralManager.swift | 27 +++++-------------- .../example/pubspec.lock | 2 +- bluetooth_low_energy_darwin/pubspec.yaml | 2 +- 8 files changed, 33 insertions(+), 28 deletions(-) diff --git a/bluetooth_low_energy/CHANGELOG.md b/bluetooth_low_energy/CHANGELOG.md index 2a8881a..e13010d 100644 --- a/bluetooth_low_energy/CHANGELOG.md +++ b/bluetooth_low_energy/CHANGELOG.md @@ -1,3 +1,7 @@ +## 5.0.2 + +* Fix the issue that [discoverGATT failed caused by CoW](https://github.com/yanshouwang/bluetooth_low_energy/issues/36). + ## 5.0.1 * Fix the issue that [completion was called duplicately caused by CoW](https://github.com/yanshouwang/bluetooth_low_energy/issues/36). diff --git a/bluetooth_low_energy/example/lib/main.dart b/bluetooth_low_energy/example/lib/main.dart index 47ced48..0273d62 100644 --- a/bluetooth_low_energy/example/lib/main.dart +++ b/bluetooth_low_energy/example/lib/main.dart @@ -1029,6 +1029,18 @@ 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); final advertisement = Advertisement( name: 'le12138', manufacturerSpecificData: ManufacturerSpecificData( diff --git a/bluetooth_low_energy/example/pubspec.lock b/bluetooth_low_energy/example/pubspec.lock index cfb0e6a..3852912 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.1" + version: "5.0.2" bluetooth_low_energy_android: dependency: transitive description: @@ -36,10 +36,10 @@ packages: dependency: transitive description: name: bluetooth_low_energy_darwin - sha256: "8cbb1a9ac97a217f6f45dca58510df457e8b70f861c36d3bf8cf2640c669e1cd" + sha256: "876fd1c7eadd5e4eea92257dd3d2ec31f3c0bb68f8e96c564ed28ecde7382bd1" url: "https://pub.dev" source: hosted - version: "5.0.1" + version: "5.0.2" bluetooth_low_energy_linux: dependency: transitive description: diff --git a/bluetooth_low_energy/pubspec.yaml b/bluetooth_low_energy/pubspec.yaml index 530e1c7..63b6e36 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.1 +version: 5.0.2 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.0 - bluetooth_low_energy_darwin: ^5.0.1 + bluetooth_low_energy_darwin: ^5.0.2 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 a4461ef..5d77c6d 100644 --- a/bluetooth_low_energy_darwin/CHANGELOG.md +++ b/bluetooth_low_energy_darwin/CHANGELOG.md @@ -1,3 +1,7 @@ +## 5.0.2 + +* Fix the issue that [discoverGATT failed caused by CoW](https://github.com/yanshouwang/bluetooth_low_energy/issues/36). + ## 5.0.1 * Fix the issue that [completion was called duplicately 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 c4924f7..f27daa3 100644 --- a/bluetooth_low_energy_darwin/darwin/Classes/MyCentralManager.swift +++ b/bluetooth_low_energy_darwin/darwin/Classes/MyCentralManager.swift @@ -384,16 +384,11 @@ class MyCentralManager: MyCentralManagerHostApi { if error == nil { let services = peripheral.services ?? [] let servicesArgs = services.map { service in service.toArgs() } - let elements = services.flatMap { service in + let values = services.flatMap { service in let hashCodeArgs = service.hash.toInt64() return [hashCodeArgs: service] } - var items = _services[uuidArgs] - if items == nil { - _services[uuidArgs] = Dictionary(uniqueKeysWithValues: elements) - } else { - items!.merge(elements) { service1, service2 in service2 } - } + _services[uuidArgs] = Dictionary(uniqueKeysWithValues: values) completion(.success(servicesArgs)) } else { completion(.failure(error!)) @@ -409,16 +404,11 @@ class MyCentralManager: MyCentralManagerHostApi { if error == nil { let characteristics = service.characteristics ?? [] let characteristicsArgs = characteristics.map { characteristic in characteristic.toArgs() } - let elements = characteristics.flatMap { characteristic in + let values = characteristics.flatMap { characteristic in let hashCodeArgs = characteristic.hash.toInt64() return [hashCodeArgs: characteristic] } - var items = _characteristics[uuidArgs] - if items == nil { - _characteristics[uuidArgs] = Dictionary(uniqueKeysWithValues: elements) - } else { - items!.merge(elements) { characteristic1, characteristic2 in characteristic2 } - } + _characteristics[uuidArgs, default: [:]].merge(values) { value1, value2 in value2 } completion(.success(characteristicsArgs)) } else { completion(.failure(error!)) @@ -434,16 +424,11 @@ class MyCentralManager: MyCentralManagerHostApi { if error == nil { let descriptors = characteristic.descriptors ?? [] let descriptorsArgs = descriptors.map { descriptor in descriptor.toArgs() } - let elements = descriptors.flatMap { descriptor in + let values = descriptors.flatMap { descriptor in let hashCodeArgs = descriptor.hash.toInt64() return [hashCodeArgs: descriptor] } - var items = _descriptors[uuidArgs] - if items == nil { - _descriptors[uuidArgs] = Dictionary(uniqueKeysWithValues: elements) - } else { - items!.merge(elements) { descriptor1, descriptor2 in descriptor2 } - } + _descriptors[uuidArgs, default: [:]].merge(values) { value1, value2 in value2 } completion(.success(descriptorsArgs)) } else { completion(.failure(error!)) diff --git a/bluetooth_low_energy_darwin/example/pubspec.lock b/bluetooth_low_energy_darwin/example/pubspec.lock index 8d12d70..17db728 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.1" + version: "5.0.2" 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 8f6a1e5..b3f5ded 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.1 +version: 5.0.2 homepage: https://github.com/yanshouwang/bluetooth_low_energy environment: