Last active
October 1, 2015 14:54
-
-
Save KentarouKanno/e7d51771dca7b303d699 to your computer and use it in GitHub Desktop.
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/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