Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

517
Vistas
Can't discover any services, but can connect to device

I'm able to connect to a BLE device, but .discoverServices() doesn't work. My logs show an unhelpfully generic error: [CoreBluetooth] XPC connection invalid

Connecting to it does work:

    func connectToPeripheral(id: Int) -> Bool {
        guard id >= 0 && id <= peripherals.count else {
            return false
        }
                
        let arrayItem = peripherals[id]
        connectedPeripheral = arrayItem.peripheral
        connectedPeripheral!.delegate = self

        myCentral.connect(connectedPeripheral!, options: nil)
        print("connected to \(arrayItem.name)")
        
        myCentral.stopScan()
        return true
    }
    
    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        print("Connected to \(String(describing: peripheral.name)) \(peripheral.description)!")
        print(peripheral.services as Any)
        peripheral.discoverServices(nil) 
    }

But this function never gets called. Why????

    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
        print("Services:")
        print(peripheral.services as Any)
    }

Not sure what I'm doing wrong, help?


Here's the full code:

import Foundation
import CoreBluetooth

class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeripheralDelegate {

    var myCentral: CBCentralManager!

    @Published var isSwitchedOn = false
    @Published var peripherals = [Peripheral]()
    @Published var connectedPeripheral: CBPeripheral?

    override init() {
        super.init()

        myCentral = CBCentralManager(delegate: self, queue: nil)
        myCentral.delegate = self
    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        var peripheralName: String!
       
        if let name = advertisementData[CBAdvertisementDataLocalNameKey] as? String {
            peripheralName = name
        } else {
            peripheralName = "Unknown"
        }
       
        if peripherals.contains(where: { p in p.name == peripheralName }) {
            return
        }
        
        let newPeripheral = Peripheral(id: peripherals.count, name: peripheralName, rssi: RSSI.intValue, peripheral: peripheral)
        peripherals.append(newPeripheral)
    }

    func startScanning() {
        print("startScanning")
        peripherals.removeAll()
        myCentral.scanForPeripherals(withServices: nil, options: nil)
    }

    func stopScanning() {
        print("stopScanning")
        myCentral.stopScan()
    }

    func connectToPeripheral(id: Int) -> Bool {
        guard id >= 0 && id <= peripherals.count else {
            return false
        }
                
        let arrayItem = peripherals[id]
        connectedPeripheral = arrayItem.peripheral
        connectedPeripheral!.delegate = self

        myCentral.connect(connectedPeripheral!, options: nil)
        print("connected to \(arrayItem.name)")
        
        myCentral.stopScan()
        return true
    }
    
    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        print("Connected to \(String(describing: peripheral.name)) \(peripheral.description)!")
        print(peripheral.services as Any)
        peripheral.discoverServices(nil)
    }

    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
        print("Services:")
        print(peripheral.services as Any)
    }
    
    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
        print("got Characteristics...")        
    }

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        print("centralManagerDidUpdateState: poweredOn=\(central.state == .poweredOn)")
        
        if central.state == .poweredOn {
            //if it wasn't previously on, start scanning
            if !isSwitchedOn {
                isSwitchedOn = true
                startScanning()
            }
        } else {
            isSwitchedOn = false
        }
    }
}

struct Peripheral: Identifiable {
    let id: Int
    let name: String
    let rssi: Int
    let peripheral: CBPeripheral
}

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I figured out two bugs in my code.

  1. I was calling startScanning() when centralManagerDidUpdateState() was called with central.state == .poweredOn. This causing scanning to start again right after calling myCentral.connect(connectedPeripheral!, options: nil)
  2. Calling myCentral.stopScan() after myCentral.connect(connectedPeripheral!, options: nil) was causing disconnections

I don't understand why either of those were happening, but fixing them allowed me to now connect and get services and characteristics!

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda