Skip to content

Commit

Permalink
Fix lack of disconnection event in case the connection failed with an…
Browse files Browse the repository at this point in the history
… error (#69)
  • Loading branch information
mikolak authored Nov 18, 2020
1 parent cb3194c commit b34630f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.1.7

* Fix lack of disconnection event on iOS when connection fails to be established
* Fix errors all errors being passed through onError callbacks on Android (thanks, @eliaslecomte)

# 0.1.6

* Fix BluetoothGattCharacteristicUuid collision
Expand Down
13 changes: 5 additions & 8 deletions iOS/classes/BleModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,13 @@ public class BleClientManager : NSObject {
timeout: Int?,
promise: SafePromise) {

var peripheral: Peripheral? = nil
var connectionObservable = manager.retrievePeripherals(withIdentifiers: [deviceId])
.flatMap { devices -> Observable<Peripheral> in
guard let device = devices.first else {
return Observable.error(BleError.peripheralNotFound(deviceId.uuidString))
}
peripheral = device
return Observable.just(device)
}
.flatMap { $0.connect() }
Expand All @@ -401,25 +403,20 @@ public class BleClientManager : NSObject {
connectionObservable = connectionObservable.timeout(Double(timeout) / 1000.0, scheduler: ConcurrentDispatchQueueScheduler(queue: queue))
}

var peripheralToConnect : Peripheral? = nil
let connectionDisposable = connectionObservable
.do(onSubscribe: { [weak self] in
self?.dispatchEvent(BleEvent.connectingEvent, value: deviceId.uuidString)
})
.subscribe(
onNext: { [weak self] peripheral in
// When device is connected we save it in dectionary and clear all old cached values.
peripheralToConnect = peripheral
// When device is connected we save it in dictionary and clear all old cached values.
self?.connectedPeripherals[deviceId] = peripheral
self?.clearCacheForPeripheral(peripheral: peripheral)
self?.dispatchEvent(BleEvent.connectedEvent, value: deviceId.uuidString)
},
onError: { [weak self] error in
if let rxerror = error as? RxError,
let peripheralToConnect = peripheralToConnect,
let strongSelf = self,
case RxError.timeout = rxerror {
_ = strongSelf.manager.cancelPeripheralConnection(peripheralToConnect).subscribe()
if let peripheral = peripheral {
self?.onPeripheralDisconnected(peripheral)
}
error.bleError.callReject(promise)
},
Expand Down

0 comments on commit b34630f

Please sign in to comment.