【问题标题】:iOS 6 peripheral disconnect immediately after connectediOS 6 外围设备连接后立即断开连接
【发布时间】:2013-05-09 03:09:39
【问题描述】:

好的,我正在使用 Core Bluetooth 将我的 i-Pad 连接到另一个 BLE 传感器。 我很容易扫描并检测到它,但是当我想建立连接时,“确实连接外围设备”方法仅在 30 秒内被调用,就好像我已经调用了断开连接一样...... 即使我在必需的后台模式中选择或取消选择“App Communicates using 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

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

【问题讨论】:

  • 我有一个错误:尝试连接外围传感器断开连接:指定设备已与我们断开连接。
  • 您是否使用CBPeripheral 对象,当连接时:yourPeripheral = peripheraldidConnect 委托中并设置其委托:yourPeripheral setDelegate:self(自身或其他类)?
  • 您连接的实际 BLE 设备是什么?

标签: ios6 connection core-bluetooth cbperipheral


【解决方案1】:

你必须在 didDiscoverPeripheral 上做 [peripheral retain] 或者把它放到retain 属性中。 BLE框架连接后立即释放。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 2020-02-07
    • 2023-01-24
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多