Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v0.6.0] Add stop and extract #149

Merged
merged 2 commits into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.5.1'
spec.version = '0.6.0'
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.5.1' }
spec.source = { git: 'https://github.com/steamclock/bluejay.git', tag: 'v0.6.0' }
spec.source_files = 'Bluejay/Bluejay/*.{h,swift}'
spec.framework = 'SystemConfiguration'
spec.platform = :ios, '9.3'
Expand Down
47 changes: 41 additions & 6 deletions Bluejay/Bluejay/Bluejay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ public class Bluejay: NSObject {
- Parameters:
- observer: An object interested in observing Bluetooth connection events and state changes. You can register more observers using the `register` function.
- restoreMode: Determines whether Bluejay will opt-in to state restoration, and if so, can optionally provide a listen restorer as well for restoring listens.
- coreBluetoothState: Allows starting Bluejay with an existing Core Bluetooth manager and peripheral.
*/
public func start(
connectionObserver observer: ConnectionObserver? = nil,
backgroundRestore restoreMode: BackgroundRestoreMode = .disable
backgroundRestore restoreMode: BackgroundRestoreMode = .disable,
coreBluetoothState: (manager: CBCentralManager, peripheral: CBPeripheral?)? = nil
)
{
/**
Expand Down Expand Up @@ -173,11 +175,44 @@ public class Bluejay: NSObject {
options[CBCentralManagerOptionRestoreIdentifierKey] = restoreIdentifier
}

cbCentralManager = CBCentralManager(
delegate: self,
queue: .main,
options: options
)
if let cbState = coreBluetoothState {
cbCentralManager = cbState.manager

if let peripheral = cbState.peripheral {
connectedPeripheral = Peripheral(bluejay: self, cbPeripheral: peripheral)
peripheral.delegate = connectedPeripheral
}
} else {
cbCentralManager = CBCentralManager(
delegate: self,
queue: .main,
options: options
)
}
}

/**
Stops all operations and clears all states in Bluejay before returning a Core Bluetooth state that can then be used by another library or code outside of Bluejay.

- Returns: Returns a CBCentralManager and possibly a CBPeripheral as well if there was one connected at the time of this call.
- Warning: Will crash if Bluejay has not been instantiated properly or if Bluejay is still connecting.
*/
public func stopAndExtractBluetoothState() -> (manager: CBCentralManager, peripheral: CBPeripheral?) {
precondition(cbCentralManager != nil)
precondition(!isConnecting)

defer {
connectedPeripheral?.cbPeripheral.delegate = nil
connectedPeripheral = nil

cbCentralManager.delegate = nil
cbCentralManager = nil
}

queue.cancelAll(BluejayError.stopped)
observers.removeAll()

return (manager: cbCentralManager, peripheral: connectedPeripheral?.cbPeripheral)
}

/// Check to see whether the "Uses Bluetooth LE accessories" capability is turned on in the residing Xcode project.
Expand Down
5 changes: 5 additions & 0 deletions Bluejay/Bluejay/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public enum BluejayError {
case endListenCancelled
/// Indefinite flush will not exit.
case indefiniteFlush
/// Bluejay has stopped.
case stopped
}

extension BluejayError: LocalizedError {
Expand Down Expand Up @@ -107,6 +109,8 @@ extension BluejayError: LocalizedError {
return "End listen cancelled."
case .indefiniteFlush:
return "Flush listen timeout cannot be none or zero."
case .stopped:
return "Bluejay stopped."
}
}
}
Expand Down Expand Up @@ -142,6 +146,7 @@ extension BluejayError: CustomNSError {
case .listenCacheDecoding: return 21
case .endListenCancelled: return 22
case .indefiniteFlush: return 23
case .stopped: return 24
}
}

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.5.1</string>
<string>0.6.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down