Last active
August 29, 2015 14:23
-
-
Save sebk/0b50d0ce62353fb019fb to your computer and use it in GitHub Desktop.
Bluetooth on/off test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewController.h" | |
@import CoreBluetooth; | |
@interface ViewController () <CBCentralManagerDelegate> | |
@property(nonatomic, strong) CBCentralManager *cbManager; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// disable or enabled default system alert | |
NSDictionary *options = [NSDictionary dictionaryWithObject:@(NO) forKey:CBCentralManagerOptionShowPowerAlertKey]; | |
self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options]; | |
// see docu of this method | |
// [self.cbManager scanForPeripheralsWithServices:nil options:nil]; | |
} | |
- (void)centralManagerDidUpdateState:(CBCentralManager *)central { | |
if ([central state] == CBCentralManagerStatePoweredOn) { | |
NSLog(@"BL is enabled"); | |
} | |
else { | |
NSLog(@"BL is disabled"); | |
} | |
} | |
// http://stackoverflow.com/a/21696963/470964 | |
- (void)checkBluetoothAccess { | |
if(!self.cbManager) { | |
self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; | |
} | |
/* | |
We can ask the bluetooth manager ahead of time what the authorization status is for our bundle and take the appropriate action. | |
*/ | |
CBCentralManagerState state = [self.cbManager state]; | |
if(state == CBCentralManagerStateUnknown) { | |
NSLog(@"state UNKNOWN"); | |
} | |
else if(state == CBCentralManagerStateUnauthorized) { | |
NSLog(@"state DENIED"); | |
} | |
else { | |
NSLog(@"state GRANTED"); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment