iPhone does not discover Services on a Bluetooth LE tag on reconnection

Viewed 15483

I am working on a Bluetooth LE application for iOS. I am using the Core Bluetooth framework within iOS to handle all communications.

Question & Description:

When I use a single tag, despite numerous connections and disconnections, the single tag connects seamlessly and the phone discovers it services.

Also, when multiple Bluetooth LE tags connect for the first time, they connect seamlessly and the phone discovers their services.

When the tags get disconnected and then reconnect to the phone, the tags connect fine. But one of the two tags (either one) does not seem to advertise its services. i.e when the app is open and the tag reconnects, the DiscoverServices method does not call the didDiscoverServices delegate.

Why is this happening only when connection with multiple devices takes place.

I have set the peripheral.delegate correctly. I have tried everything, including doing repeated re-connect, repeated DiscoverServices calls to the tag. Nothing seems to work.

How can I re-connect to multiple tags to the phone and still discover all services.

Please help

Thanks,
Manju

8 Answers

I was trying to apply all above answers but it wasn't working for me because I was missing CBPeripheralDelegate on class.

class BLEHandler : NSObject, CBCentralManagerDelegate, CBPeripheralDelegate{

  func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {

    peripheral.delegate = self

    peripheral.discoverServices(nil)

    print("Connected: \(peripheral.state == .connected ? "YES" : "NO")")
  }

  func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {

    for service: CBService in peripheral.services! {
        peripheral.discoverCharacteristics(nil, for: service)
        print(service)
    }
  }

}

My headache was caused by lack of strong reference to peripheral

So I've add the reference and the problem is gone

private var activePeripheral: CBPeripheral?

func connect(_ peripheral: CBPeripheral) {
        disconnect(peripheral)
        activePeripheral = peripheral
        self.central.connect(peripheral, options: nil)
    }
Related