如何在iphone中获得蓝牙(开/关)的状态

我试图以编程方式获取iPhone / iPod蓝牙的状态,无论它是打开还是closures。 是否有可能使用一些苹果API或第三方API。

对Sam的答案进行一点研究,我想我会分享您可以在不使用私有API的情况下这样做,但是有一些注意事项:

  • 它只能在iOS 5.0+上运行
  • 它只能在支持蓝牙LE规范(iPhone 4S +,第五代iPod +,iPad第三代+)的设备上工作,
  • 简单地分配类将导致你的应用程序要求用户使用蓝牙堆栈的权限(可能不需要),如果他们拒绝,你将看到的唯一的东西是CBCentralManagerStateUnauthorized
  • 蓝牙状态的检索是asynchronous的,并且是连续的。 您将需要设置一个委托来获取状态更改,因为检查新分配的蓝牙pipe理器的状态将返回CBCentralManagerStateUnknown

这就是说,这个方法似乎提供了蓝牙堆栈状态的实时更新。

包括CoreBluetooth框架之后,

#import <CoreBluetooth/CoreBluetooth.h> 

这些testing很容易使用:

 - (void)detectBluetooth { if(!self.bluetoothManager) { // Put on main queue so we can call UIAlertView from 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 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; } UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Bluetooth state" message:stateString delegate:nil cancelButtonTitle:@"ok" otherButtonTitles: nil]; [alert show]; } 

要禁用默认警报消息,只需要在实例化CBPeripheralManager时通过选项字典:

SWIFT在iOS8 +上进行testing

 import CoreBluetooth //Define class variable in your VC/AppDelegate var bluetoothPeripheralManager: CBPeripheralManager? //On viewDidLoad/didFinishLaunchingWithOptions let options = [CBCentralManagerOptionShowPowerAlertKey:0] //<-this is the magic bit! bluetoothPeripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: options) 

显然你也需要实现CKManagerDelegate委托方法peripheralManagerDidUpdateState,如上所述:

 func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!) { var statusMessage = "" switch peripheral.state { case .poweredOn: statusMessage = "Bluetooth Status: Turned On" case .poweredOff: statusMessage = "Bluetooth Status: Turned Off" case .resetting: statusMessage = "Bluetooth Status: Resetting" case .unauthorized: statusMessage = "Bluetooth Status: Not Authorized" case .unsupported: statusMessage = "Bluetooth Status: Not Supported" case .unknown: statusMessage = "Bluetooth Status: Unknown" } print(statusMessage) if peripheral.state == .poweredOff { //TODO: Update this property in an App Manager class } } 

这个答案已经从最初的Objective-C更新到Swift 4.0。

假设您已经创build了一个蓝牙pipe理器,并将该委托分配给ViewController类。

 import CoreBluetooth extension ViewController : CBCentralManagerDelegate { func centralManagerDidUpdateState(_ central: CBCentralManager) { switch central.state { case .poweredOn: print("powered on") case .poweredOff: print("powered off") case .resetting: print("resetting") case .unauthorized: print("unauthorized") case .unsupported: print("unsupported") case .unknown: print("unknown") } } } 

一些关于BadPirate的答案的更新,在iOS7中,您可以设置中央pipe理器在分配pipe理器对象时不给出警报,方法是为其提供一个具有密钥“CBCentralManagerOptionShowPowerAlertKey”设置为0的NSDictionary。

 self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options: [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:CBCentralManagerOptionShowPowerAlertKey]]; 

使用CoreBluetooth的iOS 5和以上版本有一个方法。 您可以使用的类是CBCentralManager。 它有一个属性“状态”,您可以检查蓝牙是否打开。 (枚举CBCentralManagerState具有您要检查的值)。

这个解决scheme有点老,在苹果推出核心蓝牙之前

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. Class BluetoothManager = objc_getClass( "BluetoothManager" ) ; id btCont = [BluetoothManager sharedInstance] ; [self performSelector:@selector(status:) withObject:btCont afterDelay:1.0f] ; return YES ; } - (void)status:(id)btCont { BOOL currentState = [btCont enabled] ; //check the value of currentState }