0

The logic goes like this:

if (blueTooth is on){
    performSegueToPageA
}
else{
    performSegueToPageB
}

But I can't for the life of me figure out how to do it. First, there's these overly complex (and as far as Stack Overflow is to be believed, doesn't always work, depending on the iOS version or the wind direction) CBCentralManager crap.

Second, how do I call and structure it in such a way that it will return a BOOL value?

Let's assume I'm only going to target iOS7+.

Here's what I got so far:

login.h:

@interface bla bla blah <AmongOtherThings, CBCentralManagerDelegate>
@property (nonatomic, strong) CBCentralManager* blueToothManager;

login.m:

#import <CoreBluetooth/CoreBluetooth.h>
- (void)viewDidLoad {
    [self detectBluetooth]
}

- (void)detectBluetooth
{
    if(!self.blueToothManager)
    {
        // Put on main queue so we can call UIAlertView from delegate callbacks.
        self.blueToothManager = [[[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
    }
    [self centralManagerDidUpdateState:self.blueToothManager]; // Show initial state
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    NSString *stateString = nil;
    switch(_blueToothManager.state)
    {
        case CBCentralManagerStateResetting: stateString = @"The connection with the system service was momentarily lost, update imminent."; break;
        case CBCentralManagerStateUnsupported: stateString = @"The platform doesn't support Bluetooth Low Energy."; break;
        case CBCentralManagerStateUnauthorized: stateString = @"The app is not authorized to use Bluetooth Low Energy."; break;
        case CBCentralManagerStatePoweredOff: stateString = @"Bluetooth is currently powered off."; break;
        case CBCentralManagerStatePoweredOn: stateString = @"Bluetooth is currently powered on and available to use."; break;
        default: stateString = @"State unknown, update imminent."; break;
    }
    NSLog(@"%@", stateString);
}

For reference, on the code, look here. Of note is that I had to add an underscore to blueToothManager.state in centralManagerDidUpdateState simply because the code wouldn't build without it.

As is, it works well enough. Now, how do I get it to return BOOL values?

I take it it's not as simple as substituting void for BOOL (which I tried).

Thanks.

Community
  • 1
  • 1
zack_falcon
  • 4,186
  • 20
  • 62
  • 108
  • 1
    You just have to read the state of `self.blueToothManager` and check if it's equal to `CBCentralManagerStatePoweredOn`. – Larme Jul 29 '15 at 09:31
  • You could use `self.blueToothManager` instead of `_blueToothManager` or even use the variable `central` that was passed to your delegate method - it will be the same object as your `blueToothManager` property – Paulw11 Jul 29 '15 at 09:59
  • You can't get the delegate method to return a BOOL because it is invoked asynchronously when the state changes – Paulw11 Jul 29 '15 at 10:11

1 Answers1

1

In your centralManagerDidUpdateState function, you can simply check :

if (self.blueToothManager.state == CBCentralManagerStatePoweredOn)
{
        //Do something
}
else 
{
        //Do something else
}
Munahil
  • 2,381
  • 1
  • 14
  • 24
  • That did it. It should be noted that my code in `viewDidLoad` was insufficient. I needed to add the following, to get it working properly: `self.blueToothManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];` – zack_falcon Jul 29 '15 at 12:25