2012-11-27 12 views

Odpowiedz

14

Zakładając, że urządzenie iOS5 lub iOS6 i że masz obiekt CBCentralManager, można sprawdzić jego CBCentralManagerState z następujących czynności:

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."; 

} 

Będziesz także chciał obejrzeć aktualizacje delegacji centralManagerDidUpdateState:central, a następnie podejmij odpowiednie działania w swojej aplikacji.

+0

Jeśli dostanę '' CBCentralManagerStatePoweredOff' CBCentralManagerStatePoweredOn' czy jest to gwarancja, że ​​BLE jest obsługiwany? –

3

Inną opcją jest sprawdzenie, czy urządzenie obsługuje iBeacon. Jest tak dlatego, że urządzenie musi obsługiwać Bluetooth LE (tj. Bluetooth 4), aby znaleźć iBeacon. Wystarczy zaimportować CoreLocation i użyć następujących:

SWIFT:

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

Cel C:

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

To zadziałało dla mnie. W ten sposób można uzyskać prosty wynik, zamiast próbować wykonać bardziej skomplikowany wzorzec przy użyciu stanu. – HotFudgeSunday

Powiązane problemy