user3114750
user3114750

Reputation: 41

Can not connect to BLE device in iOS

I implemented code but when connecting to BLE device, it show warning:

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

I used following code:

    - (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];
    }
}

I add peripheral to foundarray and show them to tableview as below :

- (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;
}

When user select device to connect, it will show Message waiting:

 - (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];
    }

Please give me some advices. Thanks so much.

Upvotes: 2

Views: 1681

Answers (1)

Tommy Devoy
Tommy Devoy

Reputation: 13549

CoreBluetooth[WARNING] is being dealloc'ed while connecting

This warning occurs when you do not retain a newly connected peripheral. After you connect to the peripheral, you have to keep a reference to it (retain in mrc or strong in arc). Otherwise, the system just cleans up the local scope and assumes you don't care about the peripheral. So basically, just keep a reference to it and you'll be good to go.

Upvotes: 2

Related Questions