abriggs
abriggs

Reputation: 744

xcode 5 CoreBluetooth not discovering services for peripheral

I'm building several BLE apps, and have encountered this problem with my BLE protocols as well as external vendors. For the most part, BLE works as intended, but sometimes it will fail to discover services for a specific peripheral and will not work again until I restart or reset my phone.

Has anyone else experienced this problem?

When I'm done with my peripheral, the manager will call the closePeripheralConnection method. The relevant code:

// Disconnect from peripheral
- (void)closePeripheralConnection
{
    NSLog(@"== closePeripheralConnection ==");
    // If the peripheral is connected
    if (self.peripheral.state == CBPeripheralStateConnected && self.peripheral != nil) {
        // Cancel connection
        [self.manager cancelPeripheralConnection:self.peripheral];
    } else {
        [self clearPeripheralSettings];
    }
}

// Clear peripheral settings
- (void)clearPeripheralSettings
{
    // Clear peripheral variables
    self.peripheral = 0;
    // Clear services
    self.service = 0;
    // Clear characteristic values
    self.data = 0;
    self.notifyCharacteristic = 0;
}

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
    [self clearPeripheralSettings];
}

Upvotes: 1

Views: 2422

Answers (2)

eNeF
eNeF

Reputation: 3280

When do you call this method? - closePeripheralConnection

You might probably want to add this to ask for the peripheral to discover its services upon its connection in one the the central manager callback delegates

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {

    NSLog(@"peripheral: %@ state: %@", peripheral.name, peripheral.state == CBPeripheralStateConnected ? @"Connected" : @"Disconnected");

    if (peripheral.state == CBPeripheralStateConnected) {
        peripheral.delegate = self;
        [peripheral discoverServices:nil];
    }

}

Upvotes: 0

allprog
allprog

Reputation: 16790

Based on the code and your comment you are not doing anything bad. It's still the instability of the Core Bluetooth framework that hinders your work. With iOS 6 such issues were more common than with iOS 7 but serious stress can still blow things up. The Apple Developer Forum and the bluetooth-dev mailing list is full of similar reports. If the kind of usage you do while testing is not common for every day usage, then the problem may not be so relevant for end users. (Though still annoying for sure.)

I suggest you try to collect statistics. Register the number of times you call each API and try to establish some proof for Apple to show that the framework is still unstable. Storing the stats in NSUserDefaults may be the easiest solution.

Anyway if you have strong data that can serve as evidence for malfunction, then report the bug to Apple, post in the developer forum, send mail to the bluetooth-dev list. Make your sound be heard by as many people as possible.

Upvotes: 2

Related Questions