그룹 프로젝트의 일부로 구축중인 응용 프로그램에서 Bluetooth 장치를 검색하는 데 문제가 있습니다. 다음 뷰의 코드는 다음과 같습니다 때때로 그것은 단지 한 번 내 노트북을 나열합니다 블루투스Bluetooth 컨트롤러에 여러 버전의 장치 목록이 표시됨 - 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()
}
}
, 내가 뭘 잘못하고 있는지 알려주시겠습니까?
를 추가하기 전에 포함되어 있는지 여부를 판단한다. didDiscover 메서드에서 얻은 CBPeripheral 개체의 identifier 속성을 배열의 CBPeripheral 개체의 identifier 속성과 비교해야합니다. –
동일한 문제가 거기에 설명되어 있습니다 : http://stackoverflow.com/questions/43351664/why-is-corebluetooth-discovering-the-same-peripheral-again-and-again-and-again "적극적 광고". – Larme