CoreBluetooth CBCentralManager cannot connect to discovered periphearl

I am making an app that communicates between WatchOS and MacOS. I was able to connect and discover service characteristics one night and almost succeeded in showing the bluetooth transmited data on MacOS part, before xcode could no longer install newer app to WatchOS.

After I reboot my mac, calling central!.connect(peripheral) on WatchOS will attempt to connect but can't trigger the didConnect call back anymore.

I've installed bluetooth packet logger and BT debugging profile, but they are not that helpful for me at least.

Answered by MacLegacy in 815811022

This callback helped me : public func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?){...}

And the error description it provides seems to indicate a system level bug that you need to unpair and pair your watch again

So, what exactly is the problem right now? Can you install the newer app to watchOS?

Did things break after that?

There isn't much to go here, and the issue could be anywhere. Are you using a fresh CBPeripheral object with a new scan? Or are you trying to connect to a CBPeripheral that you scanned before you rebooted the Mac?

So many things that could be wrong. So you may want to do some more debugging and let us know what you have found, preferably with some logs.

Hi, thanks for helping

I did not mention that xcode can install the app to WatchOS again, and was able to monitor and debug. But The issue is that I right after I click on a discovered peripheral:

public func onPeripheralClicked(Peripheral per : CBPeripheral){

        if central!.state == .poweredOn {

            central!.connect(per);

            print("Attempting to connect to peripheral: \(per)")

        }

    }

It will just print the line above and can no longer trigger the didConnect callback:


  public func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        print("connected and trying to discover services for:\(peripheral)")
        self.connected = true
        peripheral.delegate = self
        connectedPeripheral = peripheral
        connectedPeripheral?.discoverServices(nil)  // Passing nil discovers all services
        //Timer to disconnect after action
    }

I would provide some logs, but not sure what's the best way to provide.

I am not sure if I understand the process of creating CBPeripheral is correct but basically this is how I add to an array of peripherals during scan on central manager

public func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        if !peripherials.contains(peripheral){
            self.peripherials.append(peripheral)
            print("Discovered peripheral: \(peripheral)")
        }
    }

As for the questions:

Are you using a fresh CBPeripheral object with a new scan?

I think so, otherwise there would not be a call-back with a discovered peripheral object?

Or are you trying to connect to a CBPeripheral that you scanned before you rebooted the Mac?

I always test by rebuilding the CBPeriphearl app on MacOS and making sure its advertisement starts, when the central on WatchOS starts to scan.

Accepted Answer

This callback helped me : public func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?){...}

And the error description it provides seems to indicate a system level bug that you need to unpair and pair your watch again

The connection error started again after a few connections. This seems to be an error that happens since watchOS11 as I recently updated to. Never had this issue before. I am running MacOS 15.1.1 (24B91), and iOS 18.1(22B83)

CoreBluetooth CBCentralManager cannot connect to discovered periphearl
 
 
Q