Skip to content

Instantly share code, notes, and snippets.

Last active October 1, 2015 14:54
Show Gist options
  • Save KentarouKanno/e7d51771dca7b303d699 to your computer and use it in GitHub Desktop.
Save KentarouKanno/e7d51771dca7b303d699 to your computer and use it in GitHub Desktop.
#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
@interface ViewController ()<CBCentralManagerDelegate>
@interface ViewController () {
@property (nonatomic) CBCentralManager *bluetoothManager;
@implementation ViewController
- (void)viewDidLoad {
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerOptionShowPowerAlertKey, nil];
_bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];
[_bluetoothManager scanForPeripheralsWithServices:nil options:options];
[super viewDidLoad];
- (IBAction)detectBluetoothb {
[self centralManagerDidUpdateState:self.bluetoothManager];
- (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;
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Bluetooth state" message:stateString preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self presentViewController:alertController animated:YES completion:nil];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment