Skip to content

Commit

Permalink
Fix end listen for write and listen (#131)
Browse files Browse the repository at this point in the history
* Fix a few end listen errors not propagating correctly

* Bump version to 0.4.3
  • Loading branch information
Jeremy Chiang authored Feb 6, 2018
1 parent 19ac7af commit aabd1c9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Bluejay.podspec
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Pod::Spec.new do |spec|
spec.name = 'Bluejay'
spec.version = '0.4.2'
spec.version = '0.4.3'
spec.license = { type: 'MIT', file: 'LICENSE' }
spec.homepage = 'https://github.com/steamclock/bluejay'
spec.authors = { 'Jeremy Chiang' => 'jeremy@steamclock.com' }
spec.summary = 'Bluejay is a simple Swift framework for building reliable Bluetooth apps.'
spec.homepage = 'https://github.com/steamclock/bluejay'
spec.source = { git: 'https://github.com/steamclock/bluejay.git', tag: 'v0.4.2' }
spec.source = { git: 'https://github.com/steamclock/bluejay.git', tag: 'v0.4.3' }
spec.source_files = 'Bluejay/Bluejay/*.{h,swift}'
spec.framework = 'SystemConfiguration'
spec.platform = :ios, '9.3'
Expand Down
5 changes: 5 additions & 0 deletions Bluejay/Bluejay/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public enum BluejayError {
case listenCacheEncoding(Error)
/// Bluejay has failed to decode a listen cache.
case listenCacheDecoding(Error)
/// Bluejay has cancelled an expected end listen request.
case endListenCancelled
}

extension BluejayError: LocalizedError {
Expand Down Expand Up @@ -99,6 +101,8 @@ extension BluejayError: LocalizedError {
return "Listen cache encoding failed with error: \(error.localizedDescription)"
case let .listenCacheDecoding(error):
return "Listen cache decoding failed with error: \(error.localizedDescription)"
case let .endListenCancelled:
return "End listen cancelled."
}
}
}
Expand Down Expand Up @@ -132,6 +136,7 @@ extension BluejayError: CustomNSError {
case .multipleBackgroundTaskNotSupported: return 19
case .listenCacheEncoding: return 20
case .listenCacheDecoding: return 21
case .endListenCancelled: return 22
}
}

Expand Down
2 changes: 1 addition & 1 deletion Bluejay/Bluejay/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.4.2</string>
<string>0.4.3</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
47 changes: 41 additions & 6 deletions Bluejay/Bluejay/SynchronizedPeripheral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,27 @@ public class SynchronizedPeripheral {
}

if error != nil || action == .done {
sem.signal()
if self.parent.isListening(to: characteristicIdentifier) && self.bluetoothAvailable {
self.parent.endListen(to: characteristicIdentifier, error: nil)
self.parent.endListen(to: characteristicIdentifier, error: nil, completion: { (result) in
switch result {
case .success:
break
case .cancelled:
// Don't overwrite the more important error from the original listen call.
if error == nil {
error = BluejayError.endListenCancelled
}
case .failure(let e):
// Don't overwrite the more important error from the original listen call.
if error == nil {
error = e
}
}

sem.signal()
})
} else {
sem.signal()
}
}
})
Expand All @@ -135,7 +153,7 @@ public class SynchronizedPeripheral {
case .success:
break
case .cancelled:
errorToThrow = BluejayError.cancelled
errorToThrow = BluejayError.endListenCancelled
case .failure(let endListenError):
errorToThrow = endListenError
}
Expand Down Expand Up @@ -273,9 +291,27 @@ public class SynchronizedPeripheral {
}

if error != nil || action == .done {
sem.signal()
if self.parent.isListening(to: charToListenTo) && self.bluetoothAvailable {
self.parent.endListen(to: charToListenTo, error: nil)
self.parent.endListen(to: charToListenTo, error: nil, completion: { (result) in
switch result {
case .success:
break
case .cancelled:
// Don't overwrite the more important error from the original listen call.
if error == nil {
error = BluejayError.endListenCancelled
}
case .failure(let e):
// Don't overwrite the more important error from the original listen call.
if error == nil {
error = e
}
}

sem.signal()
})
} else {
sem.signal()
}
}
})
Expand Down Expand Up @@ -416,4 +452,3 @@ extension SynchronizedPeripheral: ConnectionObserver {
}

}

0 comments on commit aabd1c9

Please sign in to comment.