CoreBluetooth does not detect reconnection services

We have an application that communicates with our own BLE device. When we push the new firmware, we reboot and we get centralManager: didDisconnectPeripheral: error:and centralManager: didConnectPeripheral:, but when we call [peripheral discoverServices:nil], we never get a callback for peripheral: didDiscoverServices:. We get it when we initially connect, but not when reconnecting. CoreBluetooth seems to know what services and features are, because we can interact with the device, but we want to be able to write a feature right after a reboot, but cannot find a reliable way to tell when it will be.

- = EDIT = - This is the code we use to write the characteristics. When we try to call him on the second call didConnect, it fails because the peripheral device does not have any services:

+(void)writeData:(NSData*)data toPeripheral:(CBPeripheral*)peripheral service:(NSString*)sCBUUID characteristic:(NSString*)cCBUUID
{
    // Sends data to BLE peripheral to process HID and send EHIF command to PC
    for(CBService *service in peripheral.services)
    {
        if([service.UUID.stringValue isEqualToString:sCBUUID])
        {
            for(CBCharacteristic *characteristic in service.characteristics)
            {
                if([characteristic.UUID.stringValue isEqualToString:cCBUUID])
                {
                    /* EVERYTHING IS FOUND, WRITE characteristic ! */
                    [peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
                    return;
                }
            }
        }
    }
}
+4
source share
1 answer

I remember from my work with CoreBluetooth that this behavior is intended because it is documented in this way. We must not reuse the same peripheral instance after disconnection. Instead, we should ask CBCentralManager to provide us with a new CBPeripheral using its well-known peripheral UUID. See also: CBCentralManager.retrievePeripheralsWithIdentifiers:

+1
source

All Articles