ios 随时检测蓝牙是否开启_检查的iOS蓝牙状态

I have an application (I am not going to submit this app to apple app store) using which I want to check whether bluetooth is turned on. If it is turned on then I have to display an alert.

– (void)centralManagerDidUpdateState:(CBCentralManager *)central{
switch (central.state) {
case CBCentralManagerStatePoweredOn:{
//alert view

break;

}

}

And in viewdidload I did like this

CBCentralManager * manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

but this is not working in ipad2 with ios 5.1.

the problem is central.state is always null.

I want the same scenario to work from ios 3.0 to ios 6 beta. Is there any common code for checking bluetooth state.

Any possible code is welcome, even code with private api.

解决方案

CBCentralManager is for using Bluetooth Smart (the Low Energy part in Bluetooth 4.0). This is a new technology only recently introduced in iOS / OS X devices. Current support is in iPhone 4s and the new iPad. The iPad 2 does NOT have support for this technology. Also the CBCentralManager is only available from iOS 5 and up.

If you want to check traditional Bluetooth state you will have to find another way to do that.

In your case central.state should actually equal CBCentralManagerStateUnsupported.