我無法掃描我正在作爲組項目的一部分構建的應用程序上的藍牙設備。視圖的代碼如下:藍牙控制器列出了多個版本的設備 - 斯威夫特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