@@ -724,6 +724,13 @@ open class CBMCentralManagerMock: CBMCentralManager {
724
724
}
725
725
// Connection is pending.
726
726
mock. state = . connecting
727
+ // If the device is already connected, there is no need to waiting for
728
+ // advertising packet.
729
+ if mock. isAlreadyConnected {
730
+ mock. connect ( ) { _ in
731
+ self . delegate? . centralManager ( self , didConnect: mock)
732
+ }
733
+ }
727
734
}
728
735
729
736
open override func cancelPeripheralConnection( _ peripheral: CBMPeripheral ) {
@@ -899,6 +906,9 @@ open class CBMPeripheralMock: CBMPeer, CBMPeripheral {
899
906
/// ``CBMCentralManagerScanOptionAllowDuplicatesKey`` flag is set.
900
907
fileprivate var wasScanned : Bool = false
901
908
fileprivate var lastAdvertisedName : String ? = nil
909
+ fileprivate var isAlreadyConnected : Bool {
910
+ return mock. isConnected
911
+ }
902
912
903
913
open var delegate : CBMPeripheralDelegate ?
904
914
@@ -947,6 +957,22 @@ open class CBMPeripheralMock: CBMPeer, CBMPeripheral {
947
957
// into range. To cancel pending connection, call disconnect().
948
958
return
949
959
}
960
+ // If the device is already connected (using a different central manager),
961
+ // report success immediatly. The device already has the connection with central
962
+ // and will not be notified about another virtual client connection.
963
+ if isAlreadyConnected {
964
+ queue. async { [ weak self] in
965
+ if let self = self , self . state == . connecting {
966
+ self . state = . connected
967
+ self . _canSendWriteWithoutResponse = true
968
+ self . mock. wasConnected = true
969
+ self . mock. virtualConnections += 1
970
+ completion ( . success( ( ) ) )
971
+ }
972
+ }
973
+ return
974
+ }
975
+ // If the device wasn't connected emulate connection request.
950
976
let result = delegate. peripheralDidReceiveConnectionRequest ( mock)
951
977
queue. asyncAfter ( deadline: . now( ) + interval) { [ weak self] in
952
978
if let self = self , self . state == . connecting {
0 commit comments