ios - How to get the status of bluetooth (ON/OFF) in iphone programmatically -


i trying status of iphone/ipod bluetooth whether on or off programmatically. possible using apple api or third party api.

a little bit of research sam's answer thought i'd share can without utilizing private api, few caveats:

  • it work on ios 5.0+
  • it work on devices support bluetooth le spec (iphone 4s+, 5th generation ipod+, ipad 3rd generation+)
  • simply allocating class cause application ask permission use bluetooth stack user (may not desired), , if refuse, thing you'll see cbcentralmanagerstateunauthorized
  • retrieval of bluetooth state async, , continuous. need setup delegate state changes, checking state of freshly allocated bluetooth manager return cbcentralmanagerstateunknown

that being said, method seem provide real time updates of bluetooth stack state.

after including corebluetooth framework,

#import <corebluetooth/corebluetooth.h> 

these tests easy perform using:

- (void)detectbluetooth {     if(!self.bluetoothmanager)     {         // put on main queue can call uialertview 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(self.bluetoothmanager.state)     {         case cbcentralmanagerstateresetting: statestring = @"the connection system service momentarily lost, update imminent."; break;         case cbcentralmanagerstateunsupported: statestring = @"the platform doesn't support bluetooth low energy."; break;         case cbcentralmanagerstateunauthorized: statestring = @"the app not authorized use bluetooth low energy."; break;         case cbcentralmanagerstatepoweredoff: statestring = @"bluetooth powered off."; break;         case cbcentralmanagerstatepoweredon: statestring = @"bluetooth powered on , available use."; break;         default: statestring = @"state unknown, update imminent."; break;     }     uialertview *alert = [[uialertview alloc] initwithtitle:@"bluetooth state"                                                      message:statestring                                                     delegate:nil                                           cancelbuttontitle:@"ok" otherbuttontitles: nil];     [alert show]; } 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -