我在作为小组项目的一部分构建的应用程序上扫描蓝牙设备时遇到问题。 View 的代码如下:
import UIKit
import CoreBluetooth
class bluetoothConnectViewController: UIViewController, UITableViewDelegate, CBCentralManagerDelegate, UITableViewDataSource {
//-----------------------
// MARK: Variables
//-----------------------
var centralManager: CBCentralManager?
var peripherals = Array<CBPeripheral>()
//-----------------------
// MARK: Outlets
//-----------------------
@IBOutlet weak var tableView: UITableView!
//-----------------------
// MARK: Core Functions
//-----------------------
override func viewDidLoad() {
super.viewDidLoad()
//Initialise CoreBluetooth Central Manager
centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main, options: nil)
// Table pull to refresh
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(refresh(_:)), for: .valueChanged)
tableView.refreshControl = refreshControl
tableView.delegate = self
tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//-----------------------
// MARK: Custom Functions
//-----------------------
func refresh(_ refreshControl: UIRefreshControl) {
// Do your job, when done:
refreshControl.endRefreshing()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return peripherals.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Connect to device"
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
}
let peripheral = peripherals[indexPath.row]
cell?.textLabel?.text = peripheral.name
return cell!
}
//-----------------------
// MARK: BLE Functions
//-----------------------
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if (central.state == .poweredOn){
self.centralManager?.scanForPeripherals(withServices: nil, options: nil)
}
else {
let alert = UIAlertController(title: "Bluetooth not enabled", message: "Enable bluetooth to scan for devuices", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: { (UIAlertAction) in
let url = URL(string: "prefs:root=Bluetooth")!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}))
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
peripherals.append(peripheral)
tableView.reloadData()
}
}
当使用蓝牙扫描时,这给了我以下结果
偶尔它只会列出我的笔记本电脑一次,但它是断断续续的,请告诉我我做错了什么?
最佳答案
在 didDiscover 方法中,每次你需要比较 CDUUID,它是 CBPeripheral 的属性,它已经在外围数组中可用。 如果不存在,则需要插入数组,否则保留它。
你使用这条线得到了 CBUUID CBUUID *uuid = peripheral.UUID; 并检查此 uuid 是否已存在于数组中。
关于ios - 蓝牙 Controller 列出多个版本的设备 - Swift 3.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43366877/