Je rencontre des problèmes pour rechercher des appareils Bluetooth sur une application que je construis dans le cadre d'un projet de groupe. Le code de la vue est la suivante:Le contrôleur Bluetooth répertorie plusieurs versions de l'appareil - Swift 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()
}
}
Cela me donne le résultat suivant lors de la numérisation à l'aide Bluetooth
De temps en temps, il énumérera mon ordinateur portable une seule fois, mais il est intermittent, s'il vous plaît pouvez me faire savoir ce que je fais mal?
Semble vous ajoutez même objet CBPeripheral dans le tableau de périphériques. Vous devez comparer la propriété d'identifiant de l'objet CBPeripheral obtenu dans la méthode didDiscover avec la propriété d'identifiant des objets CBPeripheral dans le tableau. –
C'est le même problème expliqué ici: http://stackoverflow.com/questions/43351664/why-is-corebluetooth-discovering-the-same-peripheral-again-and-again-and-again "Publicité agressive". – Larme