委托是nil或者没有实现peripheral:didUpdateValueForCharacteristic:error:?

问题描述 投票:0回答:1

在我的应用程序中,我想通过蓝牙从BLE设备获取电池详细信息。我是CoreBluetooth框架的新手。

请给我解决方案,了解如何从应用程序中的BLE设备获取数据。

下面是我的ViewController:

import UIKit
import CoreBluetooth

class BluetoothDevicesViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {

    @IBOutlet var bluetoothDevicesTableView: UITableView!

    var peripheralsArray = [CBPeripheral]()
    var centralManager: CBCentralManager?

    let YOUR_CHARACTERISTIC_UUID = "47e9ee00-47e9-11e4-8939-164230d1df67"

    var peripheralData = PeripheralData.sharedInstance

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewDidAppear(_ animated: Bool)
    {
        super.viewDidAppear(animated)
        if peripheralData.peripherals != nil {
            peripheralsArray = peripheralData.peripherals
            bluetoothDevicesTableView.reloadData()
        }
    }

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        print(central.state)
    }
}

extension BluetoothDevicesViewController: UITableViewDelegate, UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return peripheralsArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DevicesTableViewCell", for: indexPath) as? DevicesTableViewCell

        let peripheral = peripheralsArray[indexPath.row]
        centralManager?.connect(peripheral, options: nil)

        if peripheral.state == .connected {

            cell?.circleView.backgroundColor = Common.blueColor

            peripheral.delegate = self
            peripheral.discoverServices(nil)

        } else {
            cell?.circleView.backgroundColor = Common.redColor
        }

        cell?.deviceNameLabel.text = peripheral.name
        return cell!
    }

}

当我运行这个时,我在控制台上得到低于警告。

API MISUSE: Discovering services for peripheral <CBPeripheral: 0x282e82a80, identifier = C8C81899-0AC7-B5B6-72F4-5B9631B22D1E, name = mote 200, state = connected> while delegate is either nil or does not implement peripheral:didDiscoverServices:
ios swift core-bluetooth
1个回答
1
投票

viewDidLoad

centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.global())

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

你可以关注bluetooth

© www.soinside.com 2019 - 2024. All rights reserved.