2011-02-10 26 views

回答

0

该解决方案是有点陈旧,苹果导入纤芯蓝牙

- (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 

    } 
2

有在iOS 5及以上使用CoreBluetooth的方式之前。您可以使用的课程是CBCentralManager。它有一个属性“状态”,您可以检查蓝牙是否打开。 (枚举CBCentralManagerState具有要检查的值)。

+0

这只适用于BT LE的设备,例如iPhone 4S +,iPad 3+ – domsom 2012-10-30 14:37:17

41

的一点点研究为Sam's answer,我想我会分享 你可以这样做,不使用私人API,但有几个注意事项:

  • 它只会在iOS 5.0+
  • 工作
  • 它只会在 支持蓝牙LE规范(iPhone 4S +,第5代iPod +,iPad的 第三代+)
  • 简单地分配班会导致应用程序请求允许使用的蓝牙堆栈从用户设备上工作(可能没有如果他们拒绝,唯一会看到的是CBCentralManagerStateUnauthorized
  • 蓝牙状态的检索是异步的,并且是连续的。您将需要设置一个代理获得状态的改变,如检查一个新分配的蓝牙管理器的状态将返回CBCentralManagerStateUnknown

话虽这么说,这个方法似乎提供蓝牙协议栈状态的实时更新。

包括CoreBluetooth框架后,

#import <CoreBluetooth/CoreBluetooth.h> 

这些测试很容易使用执行:

- (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]; 
} 
+2

您提到了这一点,但为了保持一致性:声明属性@property(非原子,强大)CBCentralManager * bluetoothManager;`并将您的类设置为符合协议`CBCentralManagerDelegate` – 2014-01-14 17:19:46

+1

这不适合我。有人得到这个为他们工作? iOS7 + – achi 2014-09-24 05:56:14

5

一些更新的BadPirate的答案,与iOS7可以设置中央管理器不显示通过为其分配管理对象的NSDictionary具有密钥“CBCentralManagerOptionShowPowerAlertKey”设置为0时发出警报。

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

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

假设你已经创建了一个蓝牙管理器并指派了代表对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") 
     } 
    } 
} 
21

要当你实例化CBPeripheralManager禁用你只需要通过一个选项字典中的默认警报消息:

SWIFT在iOS8上测试+

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 
    } 
}