Search code examples
iphoneioscocoa-touchgamekitcore-bluetooth

Checking if Bluetooth is Disabled on iOS 5 without BluetoothManager


I have seen that in iOS 5, CoreBluetooth provides the capability to check if Bluetooth is disabled. From what I have seen of the documentation, it is clearly aimed at bluetooth peripheral use. However, I am attempting to check if bluetooth is on because I am using GameKit (GKPeerPickerController) that will search endlessly for bluetooth connections if it is not enabled, which is an issue.

I tried to do this like so:

CBCentralManager * manager = [[CBCentralManager alloc] init];

if (manager.state == CBCentralManagerStatePoweredOn ) {

      //go ahead with GameKit
}

This does not work, and manager.state is always equal to null. What am I doing wrong here? Or alternatively, are there better ways to check the status of bluetooth on the iPhone?

EDIT: I don't want to call any private APIs because I will be submitting this app to the app store. I have edited my question title to clarify this.


Solution

  • Ok, I discovered that by doing this:

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:FALSE], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
    NSMutableArray * discoveredPeripherals = [NSMutableArray new];
    CBCentralManager * manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
    [manager scanForPeripheralsWithServices:discoveredPeripherals options:options];
    [manager stopScan];
    

    If bluetooth is off, the system will pop up an alert view which will offer the choice to turn bluetooth on. Otherwise, if it finds a peripheral it will call a corresponding delegate method, but if there is nothing in that implementation you don't need to worry about it.