执行扫描后,我有UITableView
列表CBPeripheral
。当您点击该行时,该应用将连接到CBPeripheral
,Destination
视图一旦连接就会显示CBPeripheral
。
Destination
视图是另一个UITableView
,其中包含CBService
列表。在此视图中,我有一个自定义标题,其中包含有关CBPeripheral
的信息。我想更新该自定义标题单元格上的RSSI
标签,而不必tableView.reloadData()
。
我尝试在单元格中添加peripheral.readRSSI()
,但代理peripheral(peripheral:didReadRSSI:error)
似乎永远不会被调用。
我做错了什么?这可不是这样做的吗?
任何帮助表示感谢。
目的地视图
import UIKit
import CoreBluetooth
class ServicesTableViewController: UITableViewController, CBPeripheralDelegate {
var peripheral: Peripheral?
var sectionHeaderHeight: CGFloat = 110
override func viewDidLoad() {
super.viewDidLoad()
if let peripheral = peripheral {
peripheral.delegate = self
peripheral.readRSSI()
peripheral.discoverServices(nil)
}
}
// MARK: - Table View
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return peripheral?.services?.count ?? 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let services = peripheral?.services else {
return UITableViewCell()
}
let cell = tableView.dequeueReusableCell(withIdentifier: "ServiceTableViewCell", for: indexPath)
let service = services[indexPath.row]
cell.textLabel!.text = service.uuid.description
cell.detailTextLabel?.text = service.uuid.uuidString
return cell
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return false
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "ServiceHeaderTableViewCell") as! PeripheralTableViewCell
if let peripheral = peripheral {
cell.setupWithPeripheral(peripheral: peripheral)
}
return cell
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return sectionHeaderHeight
}
// MARK: - Peripheral Delegate
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
print("Did discover services")
if let error = error {
print("Error: \(error)")
} else {
print("Discovered service:\n \(String(describing: peripheral.services))")
tableView.reloadData()
}
}
PeripheralTableViewCell
import UIKit
import CoreBluetooth
class PeripheralTableViewCell: UITableViewCell, CBPeripheralDelegate {
@IBOutlet weak var NameLabel: UILabel!
@IBOutlet weak var UUIDLabel: UILabel!
@IBOutlet weak var RSSILabel: UILabel!
func setupWithPeripheral(peripheral: Peripheral) {
NameLabel.text = peripheral.name ?? "No name"
UUIDLabel.text = peripheral.identifier.uuidString
if peripheral.state == .connected {
print("Is connected... attempt to read RSSI")
peripheral.readRSSI()
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
// MARK: - Peripheral Delegate
func peripheral(_ peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: Error?) {
print("Did read RSSI")
if let error = error {
print("Error: \(error)")
} else {
print("RSSI: \(RSSI)")
RSSILabel.text = RSSI.stringValue
if peripheral.state == .connected {
//peripheral.readRSSI() \\ turn this into a timed call
} else {
}
}
}
}