2013-09-30 132 views

回答

2

只需存儲外圍設備標識符或(UUID爲< iOS 7),檢索外圍設備,並在中央管理器更新狀態爲啓動時調用連接。

對於iOS 7:

- (void)centralManagerDidUpdateState:(CBCentralManager *)central 
    { 
     if(central.state == CBCentralManagerStatePoweredOn) 
     { 
      NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:savedUUID];//where savedUUID is the string version of the NSUUID you've saved somewhere 

      NSArray *peripherals = [_cbCentralManager retrievePeripheralsWithIdentifiers:@[uuid]]; 

      for(CBPeripheral *periph in peripherals) 
      { 
       [_cbCentralManager connectPeripheral:periph options:nil]; 
      } 
     } 
    } 

對於iOS 6:

- (void)centralManagerDidUpdateState:(CBCentralManager *)central 
    { 
     if(central.state == CBCentralManagerStatePoweredOn) 
     { 
      CFUUIDRef uuid;//the cfuuidref you've previously saved 
      [central retrievePeripherals:@[(id)uuid]];//now wait for the delegate callback below 
     } 
    } 

    - (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals 
    { 
     for(CBPeripheral *periph in peripherals) 
     { 
      [_centralManager connectPeripheral:periph options:nil]; 
     } 
    } 

注:這些只是代碼段。您還應該監視CBCentralManagerStatePoweredOff(其中包括)並取消所有當前的外圍設備連接。

相關問題