无法连接到iOS中的BLE设备

时间:2013-12-20 13:43:28

标签: ios objective-c core-bluetooth

我实现了代码,但在连接到BLE设备时,它显示警告:

2013-12-20 16:58:57.975 TestPeripheral[1054:60b] CoreBluetooth[WARNING] <CBPeripheral: 635 identifier = Addfew23-424-E653-8E58-424343, Name = "BLE", state = connecting> is being dealloc'ed while connecting

我使用了以下代码:

    - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {


    NSLog (@"Discovered peripheral: %@", [peripheral name]);

    NSLog (@"UUID peripheral: %@", [peripheral UUID]);

    NSLog (@"peripheral services before connected: %@", [peripheral services]);


    deviceName.text = [NSString stringWithFormat:@"Device Name: %@", [peripheral name]];
    deviceUUID.text = [NSString stringWithFormat:@"Device UUID: %@",[peripheral UUID]];

    [self.manager stopScan];
    NSLog(@"Scanning stopped");

    NSLog (@"start connecting to device");
    [self.manager connectPeripheral:peripheral options:nil];
    self.activePeripheral = peripheral;
}

- (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals
{
    for (CBPeripheral *peripheral in peripherals) {
        NSLog(@"Retrieved Peripheral %@", peripheral.name);
        [foundArray addObject:peripheral.name];
    }
}

我将附件添加到foundarray并将它们显示在tableview中,如下所示:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [foundArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;

        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }


    cell.textLabel.text = [foundArray objectAtIndex:indexPath.row];

    return cell;
}

当用户选择要连接的设备时,它将显示消息等待:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSInteger row = indexPath.row;
        UIAlertView *alertPair = [[UIAlertView alloc] initWithTitle:@"Connecting" message:@"Waiting to connect" delegate:nil cancelButtonTitle:@"OK", nil];

        [alertPair show];
    }

请给我一些建议。非常感谢。

1 个答案:

答案 0 :(得分:2)

  

CoreBluetooth [警告]是   在连接时被解除分配

如果不保留新连接的外围设备,则会出现此警告。连接到外设后,必须保留对它的引用(保留在mrc中或保留在弧形中)。否则,系统只会清理本地范围,并假设您不关心外围设备。所以基本上,只要保持对它的引用,你就会好起来。