gpt4 book ai didi

ios - 使用外围对象作为另一个函数的参数

转载 作者:行者123 更新时间:2023-11-29 01:35:45 25 4
gpt4 key购买 nike

我是 Swift 的新手,所以我的问题的答案对您来说可能很容易。我想创建一个界面来扫描 BLE 设备,将它们列在 TableView 中,并连接到此列表中的选定设备。到目前为止,我实现了扫描和列表,但我想知道如何才能连接到您刚刚在表格 View 中单击的所需设备。我假设我必须将外围对象发送到 tableview DidSelectRowAtIndexPath 函数,但我真的不知道该怎么做。或者肯定有更聪明的方法来做到这一点?感谢您的回答和时间

这是我到目前为止所做的:

import UIKit
import CoreBluetooth

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CBCentralManagerDelegate, CBPeripheralDelegate, UITextViewDelegate {
@IBOutlet var status: UITextView!
@IBOutlet
var manager:CBCentralManager!
var tableView: UITableView!
var items: [String] = [""]

override func viewDidLoad() {
super.viewDidLoad()
manager = CBCentralManager(delegate: self, queue: nil)
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
status!.delegate = self
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.items.count; }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
status.text = "Connexion en cours";
self.manager.connectPeripheral(peripheral, options: nil) // HERE IS THE THING...
}

func centralManagerDidUpdateState(central: CBCentralManager) {
if central.state == CBCentralManagerState.PoweredOn {
central.scanForPeripheralsWithServices(nil, options: nil)
status.text = "Recherche d'appareil Bluetooth";
}
if central.state == CBCentralManagerState.PoweredOff { status.text = "Le Bluetooth est éteint"; }
if central.state == CBCentralManagerState.Unsupported { status.text = "Le Bluetooth n'est pas supporté"; }
if central.state == CBCentralManagerState.Unauthorized { status.text = "Le Bluetooth n'est pas autorisé"; }
}
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
items += ["\(peripheral.name)"]
self.tableView.reloadData()
self.manager.stopScan()
}
func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
status.text = "Connecté au périphérique";
//peripheral.discoverServices(nil)
}
}

顺便说一下,我到处都读到 CoreBluetooth 正在处理 BLE。那么我用什么来连接没有 BLE 设备呢??

最佳答案

在数组中添加 CBPeripheral 项而不是名称,然后将该对象传递给 connect 。像下面这样的一些可能会为您解决。首先在 didDiscoverPeripheral 中更改这一行

items += ["\(peripheral.name)"]

items += [peripheral]

然后更改 cellForRowAtIndexPath 中的这一行

cell.textLabel?.text = self.items[indexPath.row]

cell.textLabel?.text = self.items[indexPath.row].name

然后在你的 didSelectRowAtIndexPath 中

self.manager.connectPeripheral(peripheral, options: nil)

self.manager.connectPeripheral(self.items[indexPath.row], options: nil)

关于ios - 使用外围对象作为另一个函数的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32993855/

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