gpt4 book ai didi

ios - while delegate 要么是 nil 要么不实现外设 :didUpdateValueForCharacteristic:error:?

转载 作者:行者123 更新时间:2023-11-28 05:43:17 26 4
gpt4 key购买 nike

在我的应用程序中,我想通过蓝牙从 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:

最佳答案

viewDidLoad 内部

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

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

可以关注bluetooth

关于ios - while delegate 要么是 nil 要么不实现外设 :didUpdateValueForCharacteristic:error:?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55826861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com