2

I am using my app to connect with a BLE device and keeping track of the connection status as follows

//Called when device is connected
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    [self.activePeripheral discoverServices:nil];
    if (peripheral.isConnected)
    {
        currentStatusLbl.text = [NSString stringWithFormat:@"Connected"];
    }
}

//Called if device disconnects
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
    currentStatusLbl.text = [NSString stringWithFormat:@"Disconnected"];
    [self.centralManager connectPeripheral:peripheral options:nil]; //Connect again
}

//Device found, request connection
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
     NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
     devieName.text = [localName stringByAppendingString:@" Found"];
     if (localName && ![localName isEqual:@""])
     {
         [self.centralManager stopScan];
         self.activePeripheral = peripheral;
         self.activePeripheral.delegate = self;
         [self.centralManager connectPeripheral:peripheral options:nil];
     }
}

The problem that I face with an iPhone 5 running 7.1.1 is that the status on the label keeps switching between connected and disconnected. The Bluetooth icon on the status bar confirms this (keeps showing connected and disconnected status). I tried turning the device on/off (both the BLE device and a hard reset of the phone). Also turned Bluetooth on/off. But I still get this problem.

Also I'm following the suggestion provided here in this question but no luck.

Can someone let me know if there's anything I am doing wrong or if this is an OS issue?

Thanks.

Community
  • 1
  • 1
Amit Singh
  • 2,698
  • 21
  • 49

1 Answers1

1

Write [activePeripheral retain]; inside the if condition in didDiscoverPeripheral. Hope it will help

Tarun
  • 11
  • 1