Created
June 16, 2021 11:18
-
-
Save cbrevik/8eda471efb02b62d0b7fca4fc33387a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BLECheck { | |
private static var manager: CBCentralManager? | |
private static func getManager() -> CBCentralManager { | |
if (manager == nil) { | |
manager = CBCentralManager(delegate: nil, queue: nil, options: [CBCentralManagerOptionShowPowerAlertKey: false]) | |
} | |
return manager! | |
} | |
static func getState() -> CBManagerState? { | |
if (!isBluetoothPermissionGranted()) { | |
return nil | |
} | |
return getManager().state | |
} | |
private static func isBluetoothPermissionGranted() -> Bool { | |
if (!getIfUsageSet()) { | |
return false | |
} | |
if #available(iOS 13.1, *) { | |
return CBCentralManager.authorization == .allowedAlways | |
} else if #available(iOS 13.0, *) { | |
return getManager().authorization == .allowedAlways | |
} | |
// Before iOS 13, Bluetooth permissions are not required to check permission | |
return true | |
} | |
private static func getIfUsageSet() -> Bool { | |
if #available(iOS 13.0, *) { | |
return Bundle.main.object(forInfoDictionaryKey: "NSBluetoothAlwaysUsageDescription") != nil | |
} else { | |
return Bundle.main.object(forInfoDictionaryKey: "NSBluetoothPeripheralUsageDescription") != nil | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment