From 4cd5c6ec08dc82af79094f23834328f977311229 Mon Sep 17 00:00:00 2001 From: ShreeThaanu Raveendran Date: Mon, 27 Feb 2023 19:52:23 +0530 Subject: [PATCH 1/2] feat(#546): [Shree] [iOS] Implement Destroy connection when Emit event happens --- ios/Openid4vpBle/Openid4vpBle.swift | 5 +++-- ios/Wallet/Wallet.swift | 12 ++++++------ ios/ble/Utility/TransferHandler.swift | 2 ++ ios/ble/central/CentralManagerDelegate.swift | 2 +- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/ios/Openid4vpBle/Openid4vpBle.swift b/ios/Openid4vpBle/Openid4vpBle.swift index 60db1545..05cd73a4 100644 --- a/ios/Openid4vpBle/Openid4vpBle.swift +++ b/ios/Openid4vpBle/Openid4vpBle.swift @@ -57,7 +57,8 @@ class Openid4vpBle: RCTEventEmitter { @objc(destroyConnection:) func destroyConnection(withCallback callback: @escaping RCTResponseSenderBlock) -> Any { - Wallet.shared.destroyConnection() + Wallet.shared.destroyConnection(isManualDisconnect: true) + callback([]) return "check" as! Any } @@ -113,7 +114,7 @@ class Openid4vpBle: RCTEventEmitter { } fileprivate func handleError(_ message: String) { - Wallet.shared.destroyConnection() + Wallet.shared.destroyConnection(isManualDisconnect: false) EventEmitter.sharedInstance.emitNearbyErrorEvent(message: message) } diff --git a/ios/Wallet/Wallet.swift b/ios/Wallet/Wallet.swift index e64d6922..55bd2d4c 100644 --- a/ios/Wallet/Wallet.swift +++ b/ios/Wallet/Wallet.swift @@ -30,8 +30,8 @@ class Wallet: NSObject { verifierPublicKey = publicKeyData } - func destroyConnection(){ - onDeviceDisconnected(isManualDisconnect: false) + func destroyConnection(isManualDisconnect: Bool){ + onDeviceDisconnected(isManualDisconnect: isManualDisconnect) } func isSameAdvIdentifier(advertisementPayload: Data) -> Bool { @@ -98,10 +98,10 @@ class Wallet: NSObject { } func onDeviceDisconnected(isManualDisconnect: Bool) { + if let connectedPeripheral = central?.connectedPeripheral { + central?.centralManager.cancelPeripheralConnection(connectedPeripheral) + } if(!isManualDisconnect) { - if let connectedPeripheral = central?.connectedPeripheral { - central?.centralManager.cancelPeripheralConnection(connectedPeripheral) - } EventEmitter.sharedInstance.emitNearbyEvent(event: "onDisconnected") } } @@ -118,7 +118,7 @@ extension Wallet: WalletProtocol { let connStatusID = Int(data[0]) if connStatusID == 1 { print("con statusid:", connStatusID) - destroyConnection() + destroyConnection(isManualDisconnect: false) } } else { print("weird reason!!") diff --git a/ios/ble/Utility/TransferHandler.swift b/ios/ble/Utility/TransferHandler.swift index b0d87c2e..ee14dad1 100644 --- a/ios/ble/Utility/TransferHandler.swift +++ b/ios/ble/Utility/TransferHandler.swift @@ -90,6 +90,7 @@ class TransferHandler { if (r.type == .SUCCESS) { currentState = States.TransferVerified EventEmitter.sharedInstance.emitNearbyMessage(event: "send-vc:response", data: "\"RECEIVED\"") + sendMessage(message: imessage(msgType: .RESPONSE_TRANSFER_COMPLETE)) print("Emitting send-vc:response RECEIVED message") } else if r.type == .MISSING_CHUNKS { currentState = .PartiallyTransferred @@ -193,6 +194,7 @@ extension TransferHandler: PeripheralCommunicatorProtocol { } else if status == 1 { EventEmitter.sharedInstance.emitNearbyMessage(event: "send-vc:response", data: "\"REJECTED\"") } + Wallet.shared.destroyConnection(isManualDisconnect: true) } } } diff --git a/ios/ble/central/CentralManagerDelegate.swift b/ios/ble/central/CentralManagerDelegate.swift index 73b24694..631bdaea 100644 --- a/ios/ble/central/CentralManagerDelegate.swift +++ b/ios/ble/central/CentralManagerDelegate.swift @@ -34,7 +34,7 @@ extension Central { if let connectedPeripheral = connectedPeripheral { central.cancelPeripheralConnection(connectedPeripheral) } - Wallet.shared.destroyConnection() + Wallet.shared.destroyConnection(isManualDisconnect: false) } func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) { From d226c2f8f6991cfa74a4cb02ca527ce780c71eba Mon Sep 17 00:00:00 2001 From: ShreeThaanu Raveendran Date: Mon, 27 Feb 2023 19:56:40 +0530 Subject: [PATCH 2/2] feat(#546): [Shree] [iOS] Refactored manualdisconnect to selfdisconnect --- ios/Openid4vpBle/Openid4vpBle.swift | 4 ++-- ios/Wallet/Wallet.swift | 10 +++++----- ios/ble/Utility/TransferHandler.swift | 2 +- ios/ble/central/CentralManagerDelegate.swift | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/ios/Openid4vpBle/Openid4vpBle.swift b/ios/Openid4vpBle/Openid4vpBle.swift index 05cd73a4..04c6e49b 100644 --- a/ios/Openid4vpBle/Openid4vpBle.swift +++ b/ios/Openid4vpBle/Openid4vpBle.swift @@ -57,7 +57,7 @@ class Openid4vpBle: RCTEventEmitter { @objc(destroyConnection:) func destroyConnection(withCallback callback: @escaping RCTResponseSenderBlock) -> Any { - Wallet.shared.destroyConnection(isManualDisconnect: true) + Wallet.shared.destroyConnection(isSelfDisconnect: true) callback([]) return "check" as! Any } @@ -114,7 +114,7 @@ class Openid4vpBle: RCTEventEmitter { } fileprivate func handleError(_ message: String) { - Wallet.shared.destroyConnection(isManualDisconnect: false) + Wallet.shared.destroyConnection(isSelfDisconnect: false) EventEmitter.sharedInstance.emitNearbyErrorEvent(message: message) } diff --git a/ios/Wallet/Wallet.swift b/ios/Wallet/Wallet.swift index 55bd2d4c..6b812517 100644 --- a/ios/Wallet/Wallet.swift +++ b/ios/Wallet/Wallet.swift @@ -30,8 +30,8 @@ class Wallet: NSObject { verifierPublicKey = publicKeyData } - func destroyConnection(isManualDisconnect: Bool){ - onDeviceDisconnected(isManualDisconnect: isManualDisconnect) + func destroyConnection(isSelfDisconnect: Bool){ + onDeviceDisconnected(isSelfDisconnect: isSelfDisconnect) } func isSameAdvIdentifier(advertisementPayload: Data) -> Bool { @@ -97,11 +97,11 @@ class Wallet: NSObject { central?.write(serviceUuid: Peripheral.SERVICE_UUID, charUUID: NetworkCharNums.IDENTIFY_REQUEST_CHAR_UUID, data: iv + publicKey) } - func onDeviceDisconnected(isManualDisconnect: Bool) { + func onDeviceDisconnected(isSelfDisconnect: Bool) { if let connectedPeripheral = central?.connectedPeripheral { central?.centralManager.cancelPeripheralConnection(connectedPeripheral) } - if(!isManualDisconnect) { + if(!isSelfDisconnect) { EventEmitter.sharedInstance.emitNearbyEvent(event: "onDisconnected") } } @@ -118,7 +118,7 @@ extension Wallet: WalletProtocol { let connStatusID = Int(data[0]) if connStatusID == 1 { print("con statusid:", connStatusID) - destroyConnection(isManualDisconnect: false) + destroyConnection(isSelfDisconnect: false) } } else { print("weird reason!!") diff --git a/ios/ble/Utility/TransferHandler.swift b/ios/ble/Utility/TransferHandler.swift index ee14dad1..97e9ed89 100644 --- a/ios/ble/Utility/TransferHandler.swift +++ b/ios/ble/Utility/TransferHandler.swift @@ -194,7 +194,7 @@ extension TransferHandler: PeripheralCommunicatorProtocol { } else if status == 1 { EventEmitter.sharedInstance.emitNearbyMessage(event: "send-vc:response", data: "\"REJECTED\"") } - Wallet.shared.destroyConnection(isManualDisconnect: true) + Wallet.shared.destroyConnection(isSelfDisconnect: true) } } } diff --git a/ios/ble/central/CentralManagerDelegate.swift b/ios/ble/central/CentralManagerDelegate.swift index 631bdaea..49d873ce 100644 --- a/ios/ble/central/CentralManagerDelegate.swift +++ b/ios/ble/central/CentralManagerDelegate.swift @@ -34,7 +34,7 @@ extension Central { if let connectedPeripheral = connectedPeripheral { central.cancelPeripheralConnection(connectedPeripheral) } - Wallet.shared.destroyConnection(isManualDisconnect: false) + Wallet.shared.destroyConnection(isSelfDisconnect: false) } func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {