Skip to content

Commit

Permalink
Merge pull request #46 from netguru/callback-for-central-manager-stat…
Browse files Browse the repository at this point in the history
…e-updates

Allow monitoring Central Manager state updates
  • Loading branch information
filip-zielinski authored Sep 9, 2022
2 parents 79edf91 + 34572a8 commit ba7caaf
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion BlueSwift.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |spec|

spec.name = 'BlueSwift'
spec.version = '1.1.1'
spec.version = '1.1.2'
spec.summary = 'Easy and lightweight CoreBluetooth wrapper written in Swift'
spec.homepage = 'https://github.com/netguru/BlueSwift'

Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Change Log
All notable changes to this project will be documented in this file.

## [1.1.2] - 2022-09-06

### Added

- added settable `centralManagerStateUpdateHandler` public property to `BluetoothConnection` to monitor updates to Central Manager state.

## [1.1.1] - 2022-04-14

### Added
Expand Down
2 changes: 1 addition & 1 deletion Configurations/Common/Common-Base.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "../xcconfigs/Common/Common.xcconfig"

_BUILD_VERSION = 1.1.1
_BUILD_VERSION = 1.1.2
_BUILD_NUMBER = 1

_DEPLOYMENT_TARGET_IOS = 11.0
Expand Down
12 changes: 10 additions & 2 deletions Framework/Source Files/Connection/BluetoothConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public final class BluetoothConnection: NSObject {
/// Connection service implementing native CoreBluetooth stack.
private var connectionService = ConnectionService()

/// A advertisement validation handler. Will be called upon every peripheral discovery. Return value from this closure
/// An advertisement validation handler. Will be called upon every peripheral discovery. Return value from this closure
/// will indicate if manager should or shouldn't start connection with the passed peripheral according to it's identifier
/// and advertising packet.
@available(*, deprecated, message: "This closure will be removed in future version. Please use `peripheralValidationHandler`.")
Expand All @@ -25,7 +25,7 @@ public final class BluetoothConnection: NSObject {
}
}

/// A advertisement validation handler. Will be called upon every peripheral discovery. Contains matched peripheral,
/// A peripheral validation handler. Will be called upon every peripheral discovery. Contains matched peripheral,
/// discovered peripheral from CoreBluetooth, advertisement data and RSSI value. Return value from this closure
/// will indicate if manager should or shouldn't start connection with the passed peripheral according to it's identifier
/// and advertising packet.
Expand All @@ -35,6 +35,14 @@ public final class BluetoothConnection: NSObject {
}
}

/// Callback for update to Bluetooth sensor state for current device.
/// Assign custom block to this property to monitor Central Manager state changes (`poweredOn`, `poweredOff`, `unauthorized` etc.).
public var centralManagerStateUpdateHandler: ((CBManagerState) -> Void)? {
didSet {
connectionService.centralManagerStateUpdateHandler = centralManagerStateUpdateHandler
}
}

/// A peripheral connection cancelled handler. Called when disconnecting a peripheral using `disconnect(_:)` is completed.
/// Contains matched peripheral and native peripheral from CoreBluetooth.
public var peripheralConnectionCancelledHandler: ((Peripheral<Connectable>, CBPeripheral) -> Void)? {
Expand Down
5 changes: 5 additions & 0 deletions Framework/Source Files/Connection/ConnectionService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ internal final class ConnectionService: NSObject {
/// Closure called when disconnecting a peripheral using `disconnect(_:)` is completed.
internal var peripheralConnectionCancelledHandler: ((Peripheral<Connectable>, CBPeripheral) -> ())?

/// Called when Bluetooth sensor state for current device was updated.
internal var centralManagerStateUpdateHandler: ((CBManagerState) -> Void)?

/// Returns the amount of devices already connected.
internal var connectedDevicesAmount: Int {
return peripherals.filter { $0.isConnected }.count
Expand Down Expand Up @@ -159,6 +162,8 @@ extension ConnectionService: CBCentralManagerDelegate {
/// Determines Bluetooth sensor state for current device.
/// - SeeAlso: CBCentralManagerDelegate
public func centralManagerDidUpdateState(_ central: CBCentralManager) {
centralManagerStateUpdateHandler?(central.state)

guard let handler = connectionHandler, let anyDevice = peripherals.first else { return }
do {
try central.validateState()
Expand Down
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ Just drop the line below to your Podfile:

`pod 'BlueSwift'`

(but probably you'd like to pin it to the nearest major release, so `pod 'BlueSwift' , '~> 1.1.1'`)
(but probably you'd like to pin it to the nearest major release, so `pod 'BlueSwift' , '~> 1.1.2'`)

### ![](https://img.shields.io/badge/carthage-compatible-green.svg)

The same as with Cocoapods, insert the line below to your Cartfile:

`github 'netguru/BlueSwift'`

, or including version - `github 'netguru/BlueSwift' ~> 1.1.1`
, or including version - `github 'netguru/BlueSwift' ~> 1.1.2`

## 📄 License

Expand Down

0 comments on commit ba7caaf

Please sign in to comment.