我无法扫描我正在作为组项目的一部分构建的应用程序上的蓝牙设备。视图的代码如下:蓝牙控制器列出了多个版本的设备 - 斯威夫特3.1
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()
}
}
这给了我以下结果使用蓝牙
有时它会列出我的笔记本电脑只有一次扫描时,但它是间歇性的,请让我知道我在做什么错了?
之前被列入似乎要在外设阵列加入相同CBPeripheral对象。您必须将didDiscover方法中获得的CBPeripheral对象的标识符属性与数组中CBPeripheral对象的标识符属性进行比较。 –
这是相同的问题在那里解释:http://stackoverflow.com/questions/43351664/why-is-corebluetooth-discovering-the-same-peripheral-again-and-again-and-again“积极的广告”。 – Larme