3

没有得到 CBCentralManagerStatePoweredOn 和 CBPeripheralManagerState 有什么区别?在什么情况下我们应该使用这些变量?任何人都可以通过 Objective-C 示例/代码让我理解吗?

我正在开发一个应用程序,我应该通过 Objective-C 了解我的 iPhone 设备的蓝牙电源状态。我只想要蓝牙电源状态(开/关)。

4

2 回答 2

3

基本概要:

CBCentralManagerState是一个表示当前状态的枚举CBCentralManagerCBCentralManager负责扫描和连接外部设备。仅CBCentralManagerStatePoweredOn当设备具有支持 LE 的硬件并且用户已授予应用程序权限时才适用。

CBCentralManagerStateUnknown       State unknown, update imminent.
CBCentralManagerStateResetting     The connection with the system service was momentarily lost, update imminent.
CBCentralManagerStateUnsupported   The platform doesn't support the Bluetooth Low Energy Central/Client role.
CBCentralManagerStateUnauthorized  The application is not authorized to use the Bluetooth Low Energy Central/Client role.
CBCentralManagerStatePoweredOff    Bluetooth is currently powered off.
CBCentralManagerStatePoweredOn     Bluetooth is currently powered on and available to use.

CBPeripheralManagerState是一个表示状态的枚举CBPeripheralManagerCBPeripheralManager控制向该地区的其他 LE 设备宣传和显示手机本身的能力。(即CBPeripheralManager允许您模拟 a CBPeripheral)。同样,只有CBPeripheralManagerStatePoweredOn在用户之前明确授予权限并且设备具有支持 LE 的硬件时才会如此。

CBPeripheralManagerStateUnknown       State unknown, update imminent.
CBPeripheralManagerStateResetting     The connection with the system service was momentarily lost, update imminent.
CBPeripheralManagerStateUnsupported   The platform doesn't support the Bluetooth Low Energy Peripheral/Server role.
CBPeripheralManagerStateUnauthorized  The application is not authorized to use the Bluetooth Low Energy Peripheral/Server role.
CBPeripheralManagerStatePoweredOff    Bluetooth is currently powered off.
CBPeripheralManagerStatePoweredOn     Bluetooth is currently powered on and available to use.

CBPeripheralManager重要提示:如果您不设置和CBCentralManager委托,这些状态都不会更新。只有这样,您才会收到相应的委托回调,您可以在其中检查中央和外围设备的状态。

- (void)centralManagerDidUpdateState:(CBCentralManager *)central

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral;
于 2013-09-04T17:21:17.223 回答
0

为什么要创建一个新问题?
我使用的(嗯,或多或少,我简化了它)。在进行扫描之前,我将其称为:if ([self isBLECapableHardware]){[self startScan)];}与.managerBLECBCentralManager

-(BOOL)isBLECapableHardware
{
    BOOL        isBLECapable = FALSE;

    switch ([managerBLE state])
    {
        case CBCentralManagerStateUnsupported:
            isBLECapable = FALSE;
            break;
        case CBCentralManagerStateUnauthorized:
            isBLECapable = FALSE;
            break;
        case CBCentralManagerStatePoweredOff:
            isBLECapable = FALSE;
            break;
        case CBCentralManagerStatePoweredOn:
            isBLECapable = TRUE;
            break;
        case CBCentralManagerStateResetting:
            isBLECapable = FALSE;
            break;
        default:
            break;
    }
    return isBLECapable;
}

因此,您检查[CBCentralManager state]它的值可以是 CBCentralManagerStateUnsupported(等于 2)、CBCentralManagerStateUnknown(等于 0)等。
通过isBLECapableHardware,我的意思是“支持低功耗蓝牙(iPhone 4S 和更新版本,iPad 3 和更新版本, iPad mini 等有芯片) 并准备使用”。

于 2013-09-04T13:45:02.067 回答