2013-09-30 105 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(其中包括)并取消所有当前的外围设备连接。

相关问题