Ios CBPeripherageManager授权状态未确定

Ios CBPeripherageManager授权状态未确定,ios,bluetooth,core-bluetooth,Ios,Bluetooth,Core Bluetooth,我打开了蓝牙,但我使用CBPeripheralManagerAuthorizationStatus检查它 CBPeripheralManagerAuthorizationStatus status = [CBPeripheralManager authorizationStatus]; if (CBPeripheralManagerAuthorizationStatusAuthorized == status) { } else if (CBPeripheralManagerAuthoriza

我打开了蓝牙,但我使用CBPeripheralManagerAuthorizationStatus检查它

CBPeripheralManagerAuthorizationStatus status = [CBPeripheralManager authorizationStatus];
if (CBPeripheralManagerAuthorizationStatusAuthorized == status) {

}
else if (CBPeripheralManagerAuthorizationStatusNotDetermined == status) {
}
我发现状态是
CBPeripheralManagerAuthorizationStatusNotDetermined,不是CBPeripheralManagerAuthorizationStatusAuthorized,所以我不知道为什么,我使用的Xcode是7.3,我的设备是6sPlus,所以,这是因为设备是6s系列,我使用我的同伴设备,有时也无法获得蓝牙消息,我的同伴设备是6s,所以,我想得到您的帮助,谢谢。

因为应用程序中蓝牙的默认状态尚未确定,这意味着它尚未请求许可

这是三年前的原始答案,因此原始答案不再有价值。以下是iOS 13的更新答案。

首先,您必须确保通过info.plist文件设置隐私值以允许蓝牙使用

如果您的应用程序的Info.plist不包含使用情况,则该应用程序将崩溃 需要访问的数据类型的描述码,或 撞车。在iOS或iOS之后链接的应用程序上访问核心蓝牙API 13,包括NSBluetooth始终使用描述码。在iOS 12和 前面,包括NSBluetooth外围设备使用说明以访问 蓝牙外围数据

要请求权限,只需创建一个
CBCentralManager
,只要正确设置了上述键,就会调用权限提示

然后,您可以通过
CentralManagerDipDateState
delegate函数响应更改

以下代码段是一个MVP,演示了这一点:

import CoreBluetooth

final class BluetoothViewController: UIViewController {
    let centralManager = CBCentralManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        centralManager.delegate = self
    }
}

extension BluetoothViewController: CBCentralManagerDelegate {
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        switch central.state {
        case .poweredOn:
            print("ready to use")
        case .unauthorized:
            print("unauth")
        default:
            print("There are other states you should handle")
        }
    }
}

因为应用程序中蓝牙的默认状态尚未确定,这意味着它尚未请求许可

这是三年前的原始答案,因此原始答案不再有价值。以下是iOS 13的更新答案。

首先,您必须确保通过info.plist文件设置隐私值以允许蓝牙使用

如果您的应用程序的Info.plist不包含使用情况,则该应用程序将崩溃 需要访问的数据类型的描述码,或 撞车。在iOS或iOS之后链接的应用程序上访问核心蓝牙API 13,包括NSBluetooth始终使用描述码。在iOS 12和 前面,包括NSBluetooth外围设备使用说明以访问 蓝牙外围数据

要请求权限,只需创建一个
CBCentralManager
,只要正确设置了上述键,就会调用权限提示

然后,您可以通过
CentralManagerDipDateState
delegate函数响应更改

以下代码段是一个MVP,演示了这一点:

import CoreBluetooth

final class BluetoothViewController: UIViewController {
    let centralManager = CBCentralManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        centralManager.delegate = self
    }
}

extension BluetoothViewController: CBCentralManagerDelegate {
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        switch central.state {
        case .poweredOn:
            print("ready to use")
        case .unauthorized:
            print("unauth")
        default:
            print("There are other states you should handle")
        }
    }
}

@ÁlvaroAgüero我将答案更新为使用iOS 13,希望现在能帮助您。@lvaroAgüero我将答案更新为使用iOS 13,希望现在能帮助您。