Skip to content
This repository has been archived by the owner on Feb 27, 2019. It is now read-only.

Minor Bluetooth fixes #108

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion PermissionScope/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ enum Constants {
}

struct NSUserDefaultsKeys {
static let requestedInUseToAlwaysUpgrade = "PS_requestedInUseToAlwaysUpgrade"
static let requestedAlwaysLocation = "PS_requestedAlwaysLocation"
static let requestedBluetooth = "PS_requestedBluetooth"
static let requestedMotion = "PS_requestedMotion"
static let requestedNotifications = "PS_requestedNotifications"
Expand Down
48 changes: 20 additions & 28 deletions PermissionScope/PermissionScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ typealias resultsForConfigClosure = ([PermissionResult]) -> Void
return lm
}()

lazy var bluetoothManager:CBPeripheralManager = {
return CBPeripheralManager(delegate: self, queue: nil, options:[CBPeripheralManagerOptionShowPowerAlertKey: true])
}()

lazy var motionManager:CMMotionActivityManager = {
return CMMotionActivityManager()
}()
Expand All @@ -78,7 +74,10 @@ typealias resultsForConfigClosure = ([PermissionResult]) -> Void
lazy var defaults:NSUserDefaults = {
return .standardUserDefaults()
}()


/// Bluetooth manager, initialized only when we need to check Bluetooth state
var bluetoothManager: CBPeripheralManager?

/// Default status for Core Motion Activity
var motionPermissionStatus: PermissionStatus = .Unknown

Expand Down Expand Up @@ -398,9 +397,7 @@ typealias resultsForConfigClosure = ([PermissionResult]) -> Void
case .Restricted, .Denied:
return .Unauthorized
case .AuthorizedWhenInUse:
// Curious why this happens? Details on upgrading from WhenInUse to Always:
// [Check this issue](https://github.com/nickoneill/PermissionScope/issues/24)
if defaults.boolForKey(Constants.NSUserDefaultsKeys.requestedInUseToAlwaysUpgrade) {
if defaults.boolForKey(Constants.NSUserDefaultsKeys.requestedAlwaysLocation) {
return .Unauthorized
} else {
return .Unknown
Expand All @@ -417,13 +414,10 @@ typealias resultsForConfigClosure = ([PermissionResult]) -> Void
let hasAlwaysKey:Bool = !NSBundle.mainBundle()
.objectForInfoDictionaryKey(Constants.InfoPlistKeys.locationAlways).isNil
assert(hasAlwaysKey, Constants.InfoPlistKeys.locationAlways + " not found in Info.plist.")

switch statusLocationAlways() {
case .Unknown:
if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse {
defaults.setBool(true, forKey: Constants.NSUserDefaultsKeys.requestedInUseToAlwaysUpgrade)
defaults.synchronize()
}
defaults.setBool(true, forKey: Constants.NSUserDefaultsKeys.requestedAlwaysLocation)
defaults.synchronize()
locationManager.requestAlwaysAuthorization()
case .Unauthorized:
self.showDeniedAlert(.LocationAlways)
Expand Down Expand Up @@ -812,22 +806,21 @@ typealias resultsForConfigClosure = ([PermissionResult]) -> Void
}
}

/// Returns whether PermissionScope is waiting for the user to enable/disable bluetooth access or not.
private var waitingForBluetooth = false

/**
Returns the current permission status for accessing Bluetooth.

- returns: Permission status for the requested type.
*/
public func statusBluetooth() -> PermissionStatus {
// if already asked for bluetooth before, do a request to get status, else wait for user to request
if askedBluetooth{
if askedBluetooth {
triggerBluetoothStatusUpdate()
} else {
return .Unknown
}

guard let bluetoothManager = bluetoothManager else {
return .Unknown
}
switch (bluetoothManager.state, CBPeripheralManager.authorizationStatus()) {
case (.Unsupported, _), (.PoweredOff, _), (_, .Restricted):
return .Disabled
Expand Down Expand Up @@ -860,16 +853,14 @@ typealias resultsForConfigClosure = ([PermissionResult]) -> Void
}

/**
Start and immediately stop bluetooth advertising to trigger
its permission dialog.
Trigger Bluetooth permission dialog.
*/
private func triggerBluetoothStatusUpdate() {
if !waitingForBluetooth && bluetoothManager.state == .Unknown {
bluetoothManager.startAdvertising(nil)
bluetoothManager.stopAdvertising()
askedBluetooth = true
waitingForBluetooth = true
guard bluetoothManager == nil else {
return
}
bluetoothManager = CBPeripheralManager(delegate: self, queue: nil, options: [CBPeripheralManagerOptionShowPowerAlertKey: false])
askedBluetooth = true
}

// MARK: Core Motion Activity
Expand Down Expand Up @@ -958,7 +949,7 @@ typealias resultsForConfigClosure = ([PermissionResult]) -> Void
onCancel = cancelled

dispatch_async(dispatch_get_main_queue()) {
while self.waitingForBluetooth || self.waitingForMotion { }
while self.waitingForMotion { }
// call other methods that need to wait before show
// no missing required perms? callback and do nothing
self.requiredAuthorized({ areAuthorized in
Expand Down Expand Up @@ -1061,8 +1052,9 @@ typealias resultsForConfigClosure = ([PermissionResult]) -> Void
// MARK: Bluetooth delegate

public func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager) {
waitingForBluetooth = false
detectAndCallback()
dispatch_async(dispatch_get_main_queue()) {
self.detectAndCallback()
}
}

// MARK: - UI Helpers
Expand Down