Bluetooth LE ask permission?

17,306

Solution 1

The case is slightly different when explicitly asking for permissions for Bluetooth. There is no method you can call to request authorization for bluetooth.You just have to include it in info.plist file.The system automatically displays a request for user authorization when your app first attempts to use Bluetooth services to share data.

Solution 2

Use the following code and call the function [self detectBluetooth]; where you want to check for bluetooth connection.

#import <CoreBluetooth/CoreBluetooth.h>


#pragma mark - setUp bluetoothManager

//check bluetooth connection
- (void)detectBluetooth
{
    if(!self.bluetoothManager)
    {
        // Put on main queue so we can call UIAlertView from delegate callbacks.
        self.bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()] ;
    }
    [self centralManagerDidUpdateState:self.bluetoothManager]; // Show initial state
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    NSString *stateString = nil;
    switch(bluetoothManager.state)
    {
        case CBCentralManagerStateResetting:
            [self alertStatus:@"The connection with the system service was momentarily lost, update imminent." :@"update imminent" :0];
            break;

        case CBCentralManagerStateUnsupported:
            [self alertStatus:@"The platform doesn't support Bluetooth Low Energy.":@"weak Bluetooth Connection" :0];
            break;

        case CBCentralManagerStateUnauthorized:
            [self alertStatus:@"The app is not authorized to use Bluetooth Low Energy.":@"weak Bluetooth Connection" :0];
            break;
        case CBCentralManagerStatePoweredOff:
            stateString = @"Bluetooth is currently powered off.";
            [self alertStatus:@"Bluetooth is currently powered off , powered ON first.":@"No Bluetooth Connection" :0];
            break;
        case CBCentralManagerStatePoweredOn:
            stateString = @"Bluetooth is currently powered on and available to use.";
            //  [self alertStatus:@"Bluetooth is currently powered ON.":@" Bluetooth available" :0];

            break;
        default:
            stateString = @"State unknown, update imminent.";
            break;
    }

    NSLog(@"Bluetooth State %@",stateString);        
}

@end
Share:
17,306
Curnelious
Author by

Curnelious

Updated on June 27, 2022

Comments

  • Curnelious
    Curnelious almost 2 years

    I have an app using BLE. In some cases, for example when installed on the iPhone 6, the app is running and not asking for permission to use the BLE.

    At other cases, like my iPad Air, the app starts to run, and it's not asking for user permission for BLE, and then the BLE is not working (although the bluetooth on the device is turned on).

    Bluetooth permission

    APP would like to make data available to nearby Bluetooth devices even when you're not using the app

    Below that is the Info.plist data from the app for the key NSBluetoothPeripheralUsageDescription or localized Privacy - Bluetooth Peripheral Usage Description

    I don't understand When should the auto permission request happen?

    • How can I enable specific app to use BLE in the iOS Settings?
    • How can I ask for permission inside the app?