2

好的,我正在使用 Core Bluetooth 将我的 i-Pad 连接到另一个 BLE 传感器。我很容易扫描并检测到它,但是当我想建立连接时,“确实连接外围设备”方法仅在 30 秒内被调用,就好像我已经调用了断开连接一样......即使我选择或取消选择,这个结果也保持不变必需后台模式下的“应用程序使用 CoreBluetooth 进行通信”。

- (id) init
{
    self = [super init];
    if (self) 
    {
         pendingInit = YES;
         centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];

         foundPeripherals = [[NSMutableArray alloc] init];
         connectedServices = [[NSMutableArray alloc] init];
    }
    return self;
}



- (void) centralManagerDidUpdateState:(CBCentralManager *)central
{
     static CBCentralManagerState previousState = -1;
     switch ([centralManager state]) 
     {
         case CBCentralManagerStateUnsupported:
         {
             NSLog(@"NSCentralManager State Unsupported...");
             break;
         }
         case CBCentralManagerStatePoweredOff:
         {
             NSLog(@"NSCentralManager State Powered OFF");
             [self clearDevices];
             [discoverPeripheralDelegate discoveryDidRefresh];

             if (previousState != -1) 
             {
                 [discoverPeripheralDelegate discoveryStatePoweredOff];
             }
             break;
         }

         case CBCentralManagerStateUnauthorized:
         {
             NSLog(@"NSCentralManager State Unauthorized: the application is not allowed");
             break;
         }

         case CBCentralManagerStateUnknown:
         {
             NSLog(@"NSCentralManager state unknown... Bad News...");
             break;
         }

         case CBCentralManagerStatePoweredOn:
         {
             NSLog(@"NSCentralManager State Powered ON!");
             pendingInit = NO;
             [self loadSavedDevices];

             [centralManager retrieveConnectedPeripherals];
             [discoverPeripheralDelegate discoveryDidRefresh];
             break;
         }

         case CBCentralManagerStateResetting:
         {
             NSLog(@"CBCentralManager State Resetting");
             [self clearDevices];
             [discoverPeripheralDelegate discoveryDidRefresh];
             [peripheralDelegate alarmServiceDidReset];

             pendingInit = YES;
             break;
         }
     }
     previousState = [centralManager state];
}


- (void) loadSavedDevices
{
     storedDevices = [[NSArray alloc] initWithObjects:kPressureServiceUUIDString, nil];

     for (id deviceUUIDString in storedDevices) {

          if (![deviceUUIDString isKindOfClass:[NSString class]])
              continue;

          CFUUIDRef uuid = CFUUIDCreateFromString(NULL, (CFStringRef)deviceUUIDString);
          if (!uuid)
              continue;

          storedUUIDs = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"StoredUUIDS"]];
          for (int i = 0; i < storedUUIDs.count; i++) {
              NSArray * tempStoredUUIDs = storedUUIDs[i];
              storedUUIDs[i] = [tempStoredUUIDs mutableCopy];
          }

          [centralManager scanForPeripheralsWithServices:nil options:nil];
          storedUUIDs = [[NSMutableArray alloc] init];
          [[NSUserDefaults standardUserDefaults] setObject:storedUUIDs forKey:@"StoredUUIDS"];
      }
} 




- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSString * finaluuids = [NSString stringWithFormat:@"%@",peripheral.UUID];
    if (storedUUIDs.count == 0)
    {
        storedUUIDs = [[NSMutableArray alloc] initWithObjects:finaluuids, nil];
        [centralManager stopScan];
        [centralManager connectPeripheral:peripheral options:nil];
    }
}




- (void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    NSLog(@"connected");
}

这是我的控制台。我没有任何警告,任何错误或释放只是:

2013-05-14 09:21:14.925 PRESSURE[262:907] connected
2013-05-14 09:21:14.927 PRESSURE[262:907] Service (Sensor) connected
2013-05-14 09:21:44.994 PRESSURE[262:907] Service (Sensor) disconnected

请问如何避免我的外围设备在连接后立即断开连接?它让我疯狂!

4

1 回答 1

0

您必须在 didDiscoverPeripheral 上执行 [外围保留] 或将其放入保留属性中。它在通过 BLE 框架连接后立即发布。

于 2013-12-05T08:21:33.913 回答