Skip to content

Commit 0592023

Browse files
authored
Merge pull request #94 from NordicSemiconductor/bugfix/connection
Bug fix: Fixed connection to already connected devices
2 parents 78be2df + e846d82 commit 0592023

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

CoreBluetoothMock/CBMCentralManagerMock.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,13 @@ open class CBMCentralManagerMock: CBMCentralManager {
724724
}
725725
// Connection is pending.
726726
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+
}
727734
}
728735

729736
open override func cancelPeripheralConnection(_ peripheral: CBMPeripheral) {
@@ -899,6 +906,9 @@ open class CBMPeripheralMock: CBMPeer, CBMPeripheral {
899906
/// ``CBMCentralManagerScanOptionAllowDuplicatesKey`` flag is set.
900907
fileprivate var wasScanned: Bool = false
901908
fileprivate var lastAdvertisedName: String? = nil
909+
fileprivate var isAlreadyConnected: Bool {
910+
return mock.isConnected
911+
}
902912

903913
open var delegate: CBMPeripheralDelegate?
904914

@@ -947,6 +957,22 @@ open class CBMPeripheralMock: CBMPeer, CBMPeripheral {
947957
// into range. To cancel pending connection, call disconnect().
948958
return
949959
}
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.
950976
let result = delegate.peripheralDidReceiveConnectionRequest(mock)
951977
queue.asyncAfter(deadline: .now() + interval) { [weak self] in
952978
if let self = self, self.state == .connecting {

0 commit comments

Comments
 (0)