mihral
mihral

Reputation: 353

CoreBluetooth peripheral:didDiscoverServices not firing

I have a simple app where I implemented a Central that can connect to a Peripheral and read or write to him.

For some reasons always this worked, but now I don't know why the delegate method didDiscoverServices is not firing. I am calling peripheral.discoverServices() in didConnect method. When discoverServices method is called I keep getting the error that the delegate method is not implemented or is nil. But the method is implemented and I am looking for services that exists.

This is my code

class SearchForPeripheralsTableViewController: UITableViewController, CBCentralManagerDelegate, CBPeripheralDelegate {

var centralManager: CBCentralManager?
var cbPeripheral: CBPeripheral!
var peripherals: [DiscoveryPeripherals] = []

 var listOfServices: [ServiceIdentifier] = []


 fileprivate func fillServices(){
        listOfServices.append(ServiceIdentifier(CBUUID(string: "FFF0")))
    }

 override func viewWillAppear(_ animated: Bool) {
    tableViewSearchPeripherals.dataSource = self
    tableViewSearchPeripherals.delegate = self

    initCentralManager()
}

override func viewDidAppear(_ animated: Bool) {
    tableViewSearchPeripherals.reloadData()
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
    print("Peripheral connected")
    let service = listOfServices.first(where: { $0.uuid == CBUUID(string:"FFF0")})
     peripheral.discoverServices([(service?.uuid)!])
}

override func viewDidLoad() {
    super.viewDidLoad()
    fillServices()
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
    if error != nil {
        print("Error discovering services: \(String(describing: error?.localizedDescription))")
        return
    }

    if let services = peripheral.services {
        for service in services {
            let characteristics = listOfCharacteristics.map({ (element) -> CBUUID in return element.uuid})
           //   let characteristic = GetCharacteristicByCBUUID(uuid: CBUUID(string: "FFF1"))
            peripheral.discoverCharacteristics(characteristics, for: service)
        }
    }
}

}

Upvotes: 2

Views: 3748

Answers (3)

Deepak Tagadiya
Deepak Tagadiya

Reputation: 2245

swift 3

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

    peripheral.delegate = self

    self.selectPeripheral.discoverServices(nil)
}

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

    if let errorService = error{

        print(errorService)

        return
    }   
    else if let services = peripheral.services as [CBService]!{

        for service in services{

            peripheral.discoverCharacteristics(nil, for: service)
        }
    }
    print("==>",peripheral.services!)
}

Upvotes: 2

Paulw11
Paulw11

Reputation: 115083

You must set your object as the CBPeripheral's delegate in order to get calls to the CBPeripheralDelegate functions:

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
    print("Peripheral connected")
    peripheral.delegate = self
    let service = listOfServices.first(where: { $0.uuid == CBUUID(string:"FFF0")})
     peripheral.discoverServices([(service?.uuid)!])
}

Upvotes: 2

Waseem
Waseem

Reputation: 130

You also need to initialize CBCentralManagera and add "Privacy - Bluetooth Peripheral Usage Description" message in info.plist

Please check this code in Swift 3.0 will help you. https://github.com/JCGuidi/BLE-iOS-Swift-3.0/blob/master/Project/BluetoothLE/ViewController.swift

Upvotes: -2

Related Questions