diff --git a/darwin/Classes/UniversalBlePlugin.swift b/darwin/Classes/UniversalBlePlugin.swift index f573fc3..178b38b 100644 --- a/darwin/Classes/UniversalBlePlugin.swift +++ b/darwin/Classes/UniversalBlePlugin.swift @@ -84,13 +84,13 @@ private class BleCentralDarwin: NSObject, UniversalBlePlatformChannel, CBCentral } func connect(deviceId: String) throws { - let peripheral = try deviceId.getPeripheral() + let peripheral = try deviceId.getPeripheral(manager: manager) peripheral.delegate = self manager.connect(peripheral) } func disconnect(deviceId: String) throws { - let peripheral = try deviceId.getPeripheral() + let peripheral = try deviceId.getPeripheral(manager: manager) if peripheral.state != CBPeripheralState.disconnected { manager.cancelPeripheralConnection(peripheral) } @@ -98,7 +98,7 @@ private class BleCentralDarwin: NSObject, UniversalBlePlatformChannel, CBCentral } func getConnectionState(deviceId: String) throws -> Int64 { - let peripheral = try deviceId.getPeripheral() + let peripheral = try deviceId.getPeripheral(manager: manager) switch peripheral.state { case .connecting: return BlueConnectionState.connecting.rawValue @@ -154,7 +154,7 @@ private class BleCentralDarwin: NSObject, UniversalBlePlatformChannel, CBCentral } func discoverServices(deviceId: String, completion: @escaping (Result<[UniversalBleService], Error>) -> Void) { - guard let peripheral = discoveredPeripherals[deviceId] else { + guard let peripheral = deviceId.findPeripheral(manager: manager) else { completion( Result.failure(PigeonError(code: "IllegalArgument", message: "Unknown deviceId:\(self)", details: nil)) ) @@ -192,7 +192,7 @@ private class BleCentralDarwin: NSObject, UniversalBlePlatformChannel, CBCentral } func setNotifiable(deviceId: String, service: String, characteristic: String, bleInputProperty: Int64, completion: @escaping (Result) -> Void) { - guard let peripheral = discoveredPeripherals[deviceId] else { + guard let peripheral = deviceId.findPeripheral(manager: manager) else { completion(Result.failure(PigeonError(code: "IllegalArgument", message: "Unknown deviceId:\(self)", details: nil))) return } @@ -218,7 +218,7 @@ private class BleCentralDarwin: NSObject, UniversalBlePlatformChannel, CBCentral } func readValue(deviceId: String, service: String, characteristic: String, completion: @escaping (Result) -> Void) { - guard let peripheral = discoveredPeripherals[deviceId] else { + guard let peripheral = deviceId.findPeripheral(manager: manager) else { completion(Result.failure(PigeonError(code: "IllegalArgument", message: "Unknown deviceId:\(self)", details: nil))) return } @@ -235,7 +235,7 @@ private class BleCentralDarwin: NSObject, UniversalBlePlatformChannel, CBCentral } func writeValue(deviceId: String, service: String, characteristic: String, value: FlutterStandardTypedData, bleOutputProperty: Int64, completion: @escaping (Result) -> Void) { - guard let peripheral = discoveredPeripherals[deviceId] else { + guard let peripheral = deviceId.findPeripheral(manager: manager) else { completion(Result.failure(PigeonError(code: "IllegalArgument", message: "Unknown deviceId:\(self)", details: nil))) return } @@ -268,7 +268,7 @@ private class BleCentralDarwin: NSObject, UniversalBlePlatformChannel, CBCentral } func requestMtu(deviceId: String, expectedMtu _: Int64, completion: @escaping (Result) -> Void) { - guard let peripheral = discoveredPeripherals[deviceId] else { + guard let peripheral = deviceId.findPeripheral(manager: manager) else { completion(Result.failure(PigeonError(code: "IllegalArgument", message: "Unknown deviceId:\(self)", details: nil))) return } @@ -298,7 +298,7 @@ private class BleCentralDarwin: NSObject, UniversalBlePlatformChannel, CBCentral } var filterCBUUID = servicesFilter.map { CBUUID(string: $0) } let bleDevices = manager.retrieveConnectedPeripherals(withServices: filterCBUUID) - bleDevices.forEach { discoveredPeripherals[$0.uuid.uuidString] = $0 } + bleDevices.forEach { $0.saveCache() } completion(Result.success(bleDevices.map { UniversalBleScanResult( deviceId: $0.uuid.uuidString, @@ -319,7 +319,7 @@ private class BleCentralDarwin: NSObject, UniversalBlePlatformChannel, CBCentral public func centralManager(_: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) { // Store the discovered peripheral using its UUID as the key - discoveredPeripherals[peripheral.uuid.uuidString] = peripheral + peripheral.saveCache() // Extract manufacturer data and service UUIDs from the advertisement data let manufacturerData = advertisementData[CBAdvertisementDataManufacturerDataKey] as? Data @@ -459,13 +459,32 @@ private class BleCentralDarwin: NSObject, UniversalBlePlatformChannel, CBCentral } } +extension CBPeripheral { + func saveCache(){ + discoveredPeripherals[self.uuid.uuidString] = self + } +} + extension String { - func getPeripheral() throws -> CBPeripheral { - guard let peripheral = discoveredPeripherals[self] else { + func getPeripheral(manager: CBCentralManager) throws -> CBPeripheral { + guard let peripheral = findPeripheral(manager: manager) else { throw PigeonError(code: "IllegalArgument", message: "Unknown deviceId:\(self)", details: nil) } return peripheral } + + func findPeripheral(manager: CBCentralManager) -> CBPeripheral? { + if let peripheral = discoveredPeripherals[self] { + return peripheral + } + if let uuid = UUID(uuidString: self) { + let peripherals = manager.retrievePeripherals(withIdentifiers: [uuid]) + if let peripheral = peripherals.first { + return peripheral + } + } + return nil + } } extension [String] { diff --git a/example/pubspec.lock b/example/pubspec.lock index 5a6a141..ab3cccc 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -53,10 +53,10 @@ packages: dependency: transitive description: name: collection - sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a + sha256: a1ace0a119f20aabc852d165077c036cd864315bd99b7eaa10a60100341941bf url: "https://pub.dev" source: hosted - version: "1.18.0" + version: "1.19.0" convert: dependency: "direct main" description: @@ -179,18 +179,18 @@ packages: dependency: transitive description: name: leak_tracker - sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05" + sha256: "7bb2830ebd849694d1ec25bf1f44582d6ac531a57a365a803a6034ff751d2d06" url: "https://pub.dev" source: hosted - version: "10.0.5" + version: "10.0.7" leak_tracker_flutter_testing: dependency: transitive description: name: leak_tracker_flutter_testing - sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806" + sha256: "9491a714cca3667b60b5c420da8217e6de0d1ba7a5ec322fab01758f6998f379" url: "https://pub.dev" source: hosted - version: "3.0.5" + version: "3.0.8" leak_tracker_testing: dependency: transitive description: @@ -323,7 +323,7 @@ packages: dependency: transitive description: flutter source: sdk - version: "0.0.99" + version: "0.0.0" source_span: dependency: transitive description: @@ -336,10 +336,10 @@ packages: dependency: transitive description: name: stack_trace - sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b" + sha256: "9f47fd3630d76be3ab26f0ee06d213679aa425996925ff3feffdec504931c377" url: "https://pub.dev" source: hosted - version: "1.11.1" + version: "1.12.0" stream_channel: dependency: transitive description: @@ -352,10 +352,10 @@ packages: dependency: transitive description: name: string_scanner - sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde" + sha256: "688af5ed3402a4bde5b3a6c15fd768dbf2621a614950b17f04626c431ab3c4c3" url: "https://pub.dev" source: hosted - version: "1.2.0" + version: "1.3.0" sync_http: dependency: transitive description: @@ -376,10 +376,10 @@ packages: dependency: transitive description: name: test_api - sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb" + sha256: "664d3a9a64782fcdeb83ce9c6b39e78fd2971d4e37827b9b06c3aa1edc5e760c" url: "https://pub.dev" source: hosted - version: "0.7.2" + version: "0.7.3" typed_data: dependency: transitive description: @@ -394,7 +394,7 @@ packages: path: ".." relative: true source: path - version: "0.14.0" + version: "0.15.0" vector_math: dependency: transitive description: @@ -407,10 +407,10 @@ packages: dependency: transitive description: name: vm_service - sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d" + sha256: f6be3ed8bd01289b34d679c2b62226f63c0e69f9fd2e50a6b3c1c729a961041b url: "https://pub.dev" source: hosted - version: "14.2.5" + version: "14.3.0" web: dependency: transitive description: @@ -423,10 +423,10 @@ packages: dependency: transitive description: name: webdriver - sha256: "003d7da9519e1e5f329422b36c4dcdf18d7d2978d1ba099ea4e45ba490ed845e" + sha256: "3d773670966f02a646319410766d3b5e1037efb7f07cc68f844d5e06cd4d61c8" url: "https://pub.dev" source: hosted - version: "3.0.3" + version: "3.0.4" win32: dependency: transitive description: