2011-02-10 24 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 
    } 
}