import UIKit
import CoreBluetooth
class ScaningViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
var centralManager: CBCentralManager?
var peripheralDevice: CBPeripheral?
var devicesArray: [String] = []
var refreshControl = UIRefreshControl()
var selectedDevice: String?
@IBOutlet weak var DeviceListTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return devicesArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? DeviceListTableViewCell
cell?.deviceName.text = devicesArray[indexPath.item]
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedDevice = devicesArray[indexPath.item]
print(selectedDevice!)
centralManager?.stopScan()
self.decodePeripheralState(peripheralState: selectedDevice.state)
self.peripheralDevice = selectedDevice
self.peripheralDevice?.delegate = self
print(self.peripheralDevice!)
self.centralManager!.connect(self.peripheralDevice!)
}
在func tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath)方法中,当我选择一行的时候,我获得了字符串中的indexpath.item,但我希望此CBMeripheral类型的 selectedDevice 。 请给我任何解决方案。 预先感谢...