【问题标题】:CoreBluetooth peripheral:didDiscoverServices not firingCoreBluetooth 外围设备:didDiscoverServices 未触发
【发布时间】:2017-11-08 08:22:34
【问题描述】:

我有一个简单的应用程序,我在其中实现了一个可以连接到外围设备并对其进行读取或写入的 Central。

由于某些原因,这总是有效的,但现在我不知道为什么委托方法 didDiscoverServices 没有触发。我在 didConnect 方法中调用 peripheral.discoverServices()。当调用 discoverServices 方法时,我不断收到 委托方法未实现或为 nil 的错误。但是方法已经实现了,我正在寻找存在的服务。

这是我的代码

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)
        }
    }
}

}

【问题讨论】:

  • 您是否将您的班级设置为外围设备的代表?我在任何地方都看不到。
  • 是的,我做到了。我现在编辑了
  • 在哪里?我还是看不到peripheral.delegate = selfself.cbPeripheral.delegate = self
  • 天哪,我太笨了。我忘了这样做。谢谢

标签: ios swift core-bluetooth


【解决方案1】:

您必须将您的对象设置为CBPeripheral 的委托,才能调用CBPeripheralDelegate 函数:

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)!])
}

【讨论】:

    【解决方案2】:

    斯威夫特 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!)
    }
    

    【讨论】:

      【解决方案3】:

      您还需要初始化 CBCentralManagera 并在 info.plist 中添加“Privacy - Bluetooth Peripheral Usage Description”消息

      请检查 Swift 3.0 中的这段代码会对您有所帮助。 https://github.com/JCGuidi/BLE-iOS-Swift-3.0/blob/master/Project/BluetoothLE/ViewController.swift

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-08
        • 2013-10-13
        • 1970-01-01
        • 2015-03-13
        • 2016-04-22
        • 2014-06-19
        相关资源
        最近更新 更多