修复 CoW 代码问题 (#40)

This commit is contained in:
iAMD
2024-01-17 19:06:36 +08:00
committed by GitHub
parent 5b308f044b
commit 921dc0fa88
9 changed files with 74 additions and 87 deletions

View File

@ -1,3 +1,7 @@
## 5.0.4
* `iOS` Fix issues caused by CoW.
## 5.0.3 ## 5.0.3
* `Android` Fix the wrong blutooth low energy state caused by multi permission requests at the same time. * `Android` Fix the wrong blutooth low energy state caused by multi permission requests at the same time.

View File

@ -979,9 +979,21 @@ class _AdvertiserViewState extends State<AdvertiserView>
Future<void> startAdvertising() async { Future<void> startAdvertising() async {
await PeripheralManager.instance.clearServices(); 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 elements = List.generate(1000, (i) => i % 256);
final value = Uint8List.fromList(elements); final value = Uint8List.fromList(elements);
final service = GattService( final myService = GattService(
uuid: UUID.short(100), uuid: UUID.short(100),
characteristics: [ characteristics: [
GattCharacteristic( GattCharacteristic(
@ -1028,19 +1040,7 @@ class _AdvertiserViewState extends State<AdvertiserView>
), ),
], ],
); );
await PeripheralManager.instance.addService(service); await PeripheralManager.instance.addService(myService);
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( final advertisement = Advertisement(
name: 'le12138', name: 'le12138',
manufacturerSpecificData: ManufacturerSpecificData( manufacturerSpecificData: ManufacturerSpecificData(

View File

@ -23,7 +23,7 @@ packages:
path: ".." path: ".."
relative: true relative: true
source: path source: path
version: "5.0.3" version: "5.0.4"
bluetooth_low_energy_android: bluetooth_low_energy_android:
dependency: transitive dependency: transitive
description: description:
@ -36,10 +36,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: bluetooth_low_energy_darwin name: bluetooth_low_energy_darwin
sha256: "876fd1c7eadd5e4eea92257dd3d2ec31f3c0bb68f8e96c564ed28ecde7382bd1" sha256: e85bc6e97a087f2bb934a1b12953a0f6aab8dfdbe57302cb2bf6d61b996fa2ee
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "5.0.2" version: "5.0.3"
bluetooth_low_energy_linux: bluetooth_low_energy_linux:
dependency: transitive dependency: transitive
description: description:

View File

@ -1,6 +1,6 @@
name: bluetooth_low_energy name: bluetooth_low_energy
description: A Flutter plugin for controlling the bluetooth low energy, supports central and peripheral apis. 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 homepage: https://github.com/yanshouwang/bluetooth_low_energy
environment: environment:
@ -12,7 +12,7 @@ dependencies:
sdk: flutter sdk: flutter
bluetooth_low_energy_platform_interface: ^5.0.0 bluetooth_low_energy_platform_interface: ^5.0.0
bluetooth_low_energy_android: ^5.0.2 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_windows: ^5.0.0
bluetooth_low_energy_linux: ^5.0.0 bluetooth_low_energy_linux: ^5.0.0

View File

@ -1,3 +1,7 @@
## 5.0.3
* Fix issues caused by CoW.
## 5.0.2 ## 5.0.2
* Fix the issue that [discoverGATT failed caused by CoW](https://github.com/yanshouwang/bluetooth_low_energy/issues/36). * Fix the issue that [discoverGATT failed caused by CoW](https://github.com/yanshouwang/bluetooth_low_energy/issues/36).

View File

@ -67,7 +67,7 @@ class MyCentralManager: MyCentralManagerHostApi {
if _centralManager.delegate == nil { if _centralManager.delegate == nil {
_centralManager.delegate = _centralManagerDelegate _centralManager.delegate = _centralManagerDelegate
} }
_onStateChanged() didUpdateState(central: _centralManager)
} }
func startDiscovery() throws { func startDiscovery() throws {
@ -259,7 +259,10 @@ class MyCentralManager: MyCentralManagerHostApi {
} }
func didUpdateState(central: CBCentralManager) { 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) { func didDiscover(central: CBCentralManager, peripheral: CBPeripheral, advertisementData: [String : Any], rssi: NSNumber) {
@ -588,11 +591,4 @@ class MyCentralManager: MyCentralManagerHostApi {
} }
return descriptors[hashCodeArgs] return descriptors[hashCodeArgs]
} }
private func _onStateChanged() {
let state = _centralManager.state
let stateArgs = state.toArgs()
let stateNumberArgs = stateArgs.rawValue.toInt64()
_api.onStateChanged(stateNumberArgs: stateNumberArgs) {_ in }
}
} }

View File

@ -60,7 +60,7 @@ class MyPeripheralManager: MyPeripheralManagerHostApi {
if _peripheralManager.delegate == nil { if _peripheralManager.delegate == nil {
_peripheralManager.delegate = _peripheralManagerDelegate _peripheralManager.delegate = _peripheralManagerDelegate
} }
_onStateChanged() didUpdateState(peripheral: _peripheralManager)
} }
func addService(serviceArgs: MyGattServiceArgs, completion: @escaping (Result<Void, Error>) -> Void) { func addService(serviceArgs: MyGattServiceArgs, completion: @escaping (Result<Void, Error>) -> Void) {
@ -102,23 +102,48 @@ class MyPeripheralManager: MyPeripheralManagerHostApi {
} }
func removeService(hashCodeArgs: Int64) throws { func removeService(hashCodeArgs: Int64) throws {
guard let service = _services[hashCodeArgs] else { guard let service = _services.removeValue(forKey: hashCodeArgs) else {
throw MyError.illegalArgument
}
let hashCode = service.hash
guard let serviceArgs = _servicesArgs[hashCode] else {
throw MyError.illegalArgument throw MyError.illegalArgument
} }
_peripheralManager.remove(service) _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 { func clearServices() throws {
_peripheralManager.removeAllServices() _peripheralManager.removeAllServices()
let servicesArgs = _servicesArgs.values
for serviceArgs in servicesArgs { _services.removeAll()
_clearService(serviceArgs: serviceArgs) _characteristics.removeAll()
} _descriptors.removeAll()
_servicesArgs.removeAll()
_characteristicsArgs.removeAll()
_descriptors.removeAll()
} }
func startAdvertising(advertisementArgs: MyAdvertisementArgs, completion: @escaping (Result<Void, Error>) -> Void) { func startAdvertising(advertisementArgs: MyAdvertisementArgs, completion: @escaping (Result<Void, Error>) -> Void) {
@ -181,7 +206,10 @@ class MyPeripheralManager: MyPeripheralManagerHostApi {
} }
func didUpdateState(peripheral: CBPeripheralManager) { 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?) { func didAdd(peripheral: CBPeripheralManager, service: CBService, error: Error?) {
@ -193,11 +221,6 @@ class MyPeripheralManager: MyPeripheralManagerHostApi {
completion(.success(())) completion(.success(()))
} else { } else {
completion(.failure(error!)) 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 _startAdvertisingCompletion = nil
_isReadyToUpdateSubscribersCallbacks.removeAll() _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 }
}
} }

View File

@ -15,7 +15,7 @@ packages:
path: ".." path: ".."
relative: true relative: true
source: path source: path
version: "5.0.2" version: "5.0.3"
bluetooth_low_energy_platform_interface: bluetooth_low_energy_platform_interface:
dependency: "direct main" dependency: "direct main"
description: description:

View File

@ -1,6 +1,6 @@
name: bluetooth_low_energy_darwin name: bluetooth_low_energy_darwin
description: iOS and macOS implementation of the bluetooth_low_energy plugin. 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 homepage: https://github.com/yanshouwang/bluetooth_low_energy
environment: environment: