@@ -30,6 +30,7 @@ enum ConnectionState: String, Codable {
3030
3131struct State {
3232 var bluetoothAvailable : Bool
33+ var bluetoothUnauthorized : Bool
3334 var scanning : Bool
3435 var discoveredPeripherals : [ UUID : PeripheralMetadata ]
3536}
@@ -61,6 +62,7 @@ class BLEConnectionContext {
6162class BluetoothManager : NSObject , ObservableObject , CBCentralManagerDelegate , CBPeripheralDelegate {
6263 private var state : State = State (
6364 bluetoothAvailable: false ,
65+ bluetoothUnauthorized: false ,
6466 scanning: false ,
6567 discoveredPeripherals: [ : ]
6668 )
@@ -124,12 +126,25 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
124126
125127 func centralManagerDidUpdateState( _ central: CBCentralManager ) {
126128 state. bluetoothAvailable = centralManager. state == . poweredOn
129+
130+ if #available( iOS 13 . 0 , * ) {
131+ // user denied permission in Settings,
132+ // or restricted by device policy
133+ let authorization = CBManager . authorization
134+ state. bluetoothUnauthorized = ( authorization == . denied || authorization == . restricted)
135+ }
136+
127137 updateBackendState ( )
128138
129139 switch central. state {
130140 case . poweredOn:
131- print ( " BLE: on " )
132- restartScan ( )
141+ if state. bluetoothUnauthorized {
142+ print ( " BLE: on but permission denied " )
143+ handleDisconnect ( )
144+ } else {
145+ print ( " BLE: on " )
146+ restartScan ( )
147+ }
133148 case . poweredOff, . unauthorized, . unsupported, . resetting, . unknown:
134149 print ( " BLE: unavailable or not supported " )
135150 handleDisconnect ( )
@@ -409,6 +424,7 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
409424
410425 struct StateJSON : Codable {
411426 let bluetoothAvailable : Bool
427+ let bluetoothUnauthorized : Bool
412428 let scanning : Bool
413429 let peripherals : [ PeripheralJSON ]
414430 }
@@ -425,6 +441,7 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
425441
426442 let state = StateJSON (
427443 bluetoothAvailable: state. bluetoothAvailable,
444+ bluetoothUnauthorized: state. bluetoothUnauthorized,
428445 scanning: state. scanning,
429446 peripherals: peripherals
430447 )
0 commit comments