Skip to content

Commit

Permalink
Merge pull request #44 from netguru/delay-bluetooth-authorization-popup
Browse files Browse the repository at this point in the history
Delay iOS Bluetooth authorisation popup
  • Loading branch information
filip-zielinski authored Apr 14, 2022
2 parents 061838a + 058e09f commit 79edf91
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 11 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.0'
spec.version = '1.1.1'
spec.summary = 'Easy and lightweight CoreBluetooth wrapper written in Swift'
spec.homepage = 'https://github.com/netguru/BlueSwift'

Expand Down
4 changes: 4 additions & 0 deletions Bluetooth.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
38FE6BE7200689AB00809A06 /* ConfigurationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38FE6BA82006898100809A06 /* ConfigurationTests.swift */; };
3A369634210F1C4E007C62F1 /* CoreBluetooth.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3A369633210F1C4E007C62F1 /* CoreBluetooth.framework */; };
3A369636210F1C57007C62F1 /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3A369635210F1C57007C62F1 /* XCTest.framework */; };
4B62466A280811CA009A8CC7 /* BluetoothAuthorizationStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B624669280811CA009A8CC7 /* BluetoothAuthorizationStatus.swift */; };
4BEB6E6F2800735D0061C702 /* Sequence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BEB6E6E2800735D0061C702 /* Sequence.swift */; };
4BEB6E72280077010061C702 /* SequenceExtensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BEB6E70280075510061C702 /* SequenceExtensionTests.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -152,6 +153,7 @@
3A142D24210F0EEE000A6089 /* Sample-Base.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Sample-Base.xcconfig"; sourceTree = "<group>"; };
3A369633210F1C4E007C62F1 /* CoreBluetooth.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreBluetooth.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS11.4.sdk/System/Library/Frameworks/CoreBluetooth.framework; sourceTree = DEVELOPER_DIR; };
3A369635210F1C57007C62F1 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Platforms/iPhoneOS.platform/Developer/Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
4B624669280811CA009A8CC7 /* BluetoothAuthorizationStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BluetoothAuthorizationStatus.swift; sourceTree = "<group>"; };
4BEB6E6E2800735D0061C702 /* Sequence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Sequence.swift; sourceTree = "<group>"; };
4BEB6E70280075510061C702 /* SequenceExtensionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SequenceExtensionTests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -410,6 +412,7 @@
38FE6BC12006898100809A06 /* Configuration.swift */,
382516552049899F0097F9D7 /* AdvertisementData.swift */,
3825165720498C450097F9D7 /* BluetoothError.swift */,
4B624669280811CA009A8CC7 /* BluetoothAuthorizationStatus.swift */,
);
path = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -624,6 +627,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4B62466A280811CA009A8CC7 /* BluetoothAuthorizationStatus.swift in Sources */,
38BE35DC203DD67D0018EA0D /* ConnectablePeripheral.swift in Sources */,
3863BE69200BDDBA00FD4EC9 /* CBCharacteristic.swift in Sources */,
4BEB6E6F2800735D0061C702 /* Sequence.swift in Sources */,
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# Change Log
All notable changes to this project will be documented in this file.

## [1.1.1] - 2022-04-14

### Added

- `bluetoothAuthorizationStatus` public property was added to `BluetoothConnection` to determine current Bluetooth authorization status.
- `requestBluetoothAuthorization()` public method was added to `BluetoothConnection` to requests User for authorization to use Bluetooth.
- `BluetoothAuthorizationStatus` enum describing Bluetooth authorization status

### Changed

- `ConnectionService`: centralManager (`CBCentralManager`) is instantiated lazily to postpone showing Bluetooth authorization popup until it is needed.

## [1.1.0] - 2022-04-13

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.0
_BUILD_VERSION = 1.1.1
_BUILD_NUMBER = 1

_DEPLOYMENT_TARGET_IOS = 11.0
Expand Down
10 changes: 10 additions & 0 deletions Framework/Source Files/Connection/BluetoothConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public final class BluetoothConnection: NSObject {
}
}

/// Current Bluetooth authorization status.
public var bluetoothAuthorizationStatus: BluetoothAuthorizationStatus {
connectionService.bluetoothAuthorizationStatus
}

/// Primary method used to connect to a device. Can be called multiple times to connect more than on device at the same time.
///
/// - Parameters:
Expand Down Expand Up @@ -80,4 +85,9 @@ public final class BluetoothConnection: NSObject {
public func stopScanning() {
connectionService.stopScanning()
}

/// Requests User for authorization to use Bluetooth.
public func requestBluetoothAuthorization() {
connectionService.requestBluetoothAuthorization()
}
}
31 changes: 24 additions & 7 deletions Framework/Source Files/Connection/ConnectionService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ internal final class ConnectionService: NSObject {
return connectedDevicesAmount >= deviceConnectionLimit
}

/// Current Bluetooth authorization status.
internal var bluetoothAuthorizationStatus: BluetoothAuthorizationStatus {
if #available(iOSApplicationExtension 13.1, *) {
return CBManager.authorization.bluetoothAuthorizationStatus
} else if #available(iOSApplicationExtension 13.0, *) {
return centralManager.authorization.bluetoothAuthorizationStatus
} else {
// Until iOS 12 applications could access Bluetooth without the user’s authorization
return .allowedAlways
}
}

/// Maximum amount of devices capable of connecting to a iOS device.
private let deviceConnectionLimit = 8

Expand All @@ -50,15 +62,15 @@ internal final class ConnectionService: NSObject {
private lazy var scanningOptions = [CBCentralManagerScanOptionAllowDuplicatesKey : true]

/// CBCentralManager instance. Allows peripheral connection.
private let centralManager = CBCentralManager()

/// iOS displays Bluetooth authorization popup when `CBCentralManager` is instantiated and authorization status is not determined.
private lazy var centralManager: CBCentralManager = {
let manager = CBCentralManager()
manager.delegate = self
return manager
}()

/// Set of advertisement UUID central manager should scan for.
private var scanParameters: Set<CBUUID> = Set()

override init() {
super.init()
centralManager.delegate = self
}
}

extension ConnectionService {
Expand Down Expand Up @@ -99,6 +111,11 @@ extension ConnectionService {
internal func stopScanning() {
centralManager.stopScan()
}

/// Triggers showing Bluetooth authorization popup by instantiating the central manager.
internal func requestBluetoothAuthorization() {
_ = centralManager
}
}

private extension ConnectionService {
Expand Down
76 changes: 76 additions & 0 deletions Framework/Source Files/Model/BluetoothAuthorizationStatus.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// Copyright © 2018 Netguru Sp. z o.o. All rights reserved.
// Licensed under the MIT License.
//

import CoreBluetooth

/// The current authorization state of a Core Bluetooth manager.
@objc public enum BluetoothAuthorizationStatus: Int {
/// Bluetooth authorization status is not determined.
case notDetermined = 0

/// Bluetooth connection is restricted.
case restricted = 1

/// Bluetooth connection is denied.
case denied = 2

/// Bluetooth connection is allowed always.
case allowedAlways = 3
}

@available(iOSApplicationExtension 13.0, *)
extension BluetoothAuthorizationStatus {

/// `CBManagerAuthorization` representation of current authorization status.
var cbManagerAuthorization: CBManagerAuthorization {
switch self {
case .notDetermined:
return .notDetermined
case .restricted:
return .restricted
case .denied:
return .denied
case .allowedAlways:
return .allowedAlways
}
}
}

@available(iOSApplicationExtension 13.0, *)
extension CBManagerAuthorization {

/// `BluetoothAuthorizationStatus` representation of current authorization status.
var bluetoothAuthorizationStatus: BluetoothAuthorizationStatus {
switch self {
case .notDetermined:
return .notDetermined
case .restricted:
return .restricted
case .denied:
return .denied
case .allowedAlways:
return .allowedAlways
@unknown default:
return .notDetermined
}
}
}

extension BluetoothAuthorizationStatus: CustomStringConvertible {

/// SeeAlso: `CustomStringConvertible.description`
public var description: String {
switch self {
case .notDetermined:
return "notDetermined"
case .restricted:
return "restricted"
case .denied:
return "denied"
case .allowedAlways:
return "allowedAlways"
}
}
}
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.0'`)
(but probably you'd like to pin it to the nearest major release, so `pod 'BlueSwift' , '~> 1.1.1'`)

### ![](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.0`
, or including version - `github 'netguru/BlueSwift' ~> 1.1.1`

## 📄 License

Expand Down

0 comments on commit 79edf91

Please sign in to comment.