2012-11-27 99 views

回答

14

假設你有一個的iOS5或iOS6的設備和你有一個CBCentralManager對象,你可以用下面的檢查其CBCentralManagerState:

switch ([_manager state]) 
{ 
    case CBCentralManagerStateUnsupported: 
     state = @"This device does not support Bluetooth Low Energy."; 
     break; 
    case CBCentralManagerStateUnauthorized: 
     state = @"This app is not authorized to use Bluetooth Low Energy."; 
     break; 
    case CBCentralManagerStatePoweredOff: 
     state = @"Bluetooth on this device is currently powered off."; 
     break; 
    case CBCentralManagerStateResetting: 
     state = @"The BLE Manager is resetting; a state update is pending."; 
     break; 
    case CBCentralManagerStatePoweredOn: 
     state = @"Bluetooth LE is turned on and ready for communication."; 
     break; 
    case CBCentralManagerStateUnknown: 
     state = @"The state of the BLE Manager is unknown."; 
     break; 
    default: 
     state = @"The state of the BLE Manager is unknown."; 

} 

您還需要注意centralManagerDidUpdateState:central委託更新,然後在您的應用中採取適當的措施。

+0

如果我得到'CBCentralManagerStatePoweredOn'或'CBCentralManagerStatePoweredOff'是該BLE支持保證嗎? –

3

另一種方法是檢查設備是否支持iBeacons。這是因爲設備必須支持藍牙LE(即藍牙4)才能找到iBeacon。只需導入CoreLocation和使用以下命令:

斯威夫特:

if (CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self)){ 
    print("Bluetooth LE is supported") 
} 

目標C:

if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]){ 
    NSLog(@"Bluetooth LE is supported"); 
} 
+0

這對我有效。這樣你可以有一個簡單的getter,而不是試圖用狀態來做一些更復雜的模式。 – HotFudgeSunday