Reputation: 665
Not getting What is the difference between CBCentralManagerStatePoweredOn and CBPeripheralManagerState? At what situations we should use these variables? Can anyone make me understand through Objective-C samples/code?
I am developing an App, where i should know bluetooth power status of my iPhone device through Objective-C. I just want bluetooth power status(ON/OFF).
Upvotes: 3
Views: 3161
Reputation: 13549
Basic rundown:
CBCentralManagerState
is an enum representing the current state of the CBCentralManager
. The CBCentralManager
is in charge of scannning and connecting to external devices. It will only be CBCentralManagerStatePoweredOn
if the device has LE supported hardware and the user has granted permission to the app.
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
is an enum representing the state of the CBPeripheralManager
. The CBPeripheralManager
controls the ability behind advertising and displaying the phone itself to other LE devices in the area. (ie. the CBPeripheralManager
allows you to emulate a CBPeripheral
). It will likewise only be CBPeripheralManagerStatePoweredOn
if the user has explicitly granted permission previously and the device has LE capable hardware.
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.
Important Note: Neither of these states will ever be updated if you are not setting the CBPeripheralManager
and CBCentralManager
delegates. It is only then that you will receive the respective delegate callbacks where you can check the state of your central and peripheral.
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
and
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral;
Upvotes: 3
Reputation: 26036
Why create a new question?
What I use (well, more or less, I simplified it).
Before doing a scan, I call this :if ([self isBLECapableHardware]){[self startScan)];}
with managerBLE
an instance of 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;
}
So, you check [CBCentralManager state]
and its values can be CBCentralManagerStateUnsupported (which is equal to 2), CBCentralManagerStateUnknown (which is equal to 0), etc.
By isBLECapableHardware, I mean "is Bluetooth Low Energy capable (iPhone 4S and newer, iPad 3 and newer, iPad mini etc. which has the chip) and ready to use".
Upvotes: 0