Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active October 1, 2015 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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>
@end
@interface ViewController () {
@property (nonatomic) CBCentralManager *bluetoothManager;
@end
@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];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment