Skip to content

Instantly share code, notes, and snippets.

@cbrevik
Created June 16, 2021 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbrevik/8eda471efb02b62d0b7fca4fc33387a3 to your computer and use it in GitHub Desktop.
Save cbrevik/8eda471efb02b62d0b7fca4fc33387a3 to your computer and use it in GitHub Desktop.
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