CBCentralManagerStatePoweredOn和CBPeripheralManagerState有什么区别?

时间:2013-09-04 13:31:14

标签: ios core-bluetooth

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

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

2 个答案:

答案 0 :(得分:3)

基本纲要:

CBCentralManagerState是一个表示CBCentralManager当前状态的枚举。 CBCentralManager负责扫描和连接外部设备。如果设备具有LE支持的硬件并且用户已授予该应用程序的权限,则它仅为CBCentralManagerStatePoweredOn

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是表示CBPeripheralManager状态的枚举。 CBPeripheralManager控制广告背后的能力,并将手机本身显示给该地区的其他LE设备。 (即CBPeripheralManager允许您模拟CBPeripheral)。如果用户先前已明确授予权限且设备具有LE硬件,则同样只有CBPeripheralManagerStatePoweredOn

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.

重要说明:如果您未设置CBPeripheralManagerCBCentralManager代表,则不会更新这些状态。只有这样,您才能收到相应的代表回调,您可以在其中检查中央和外围设备的状态。

- (void)centralManagerDidUpdateState:(CBCentralManager *)central

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral;

答案 1 :(得分:0)

为什么要创建新问题?
我使用的是什么(好吧,或多或少,我简化了它)。 在进行扫描之前,我称之为:if ([self isBLECapableHardware]){[self startScan)];} managerBLE CBCentralManager的实例。

-(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等具有芯片功能)并随时可用”。