0

我无法扫描我正在作为组项目的一部分构建的应用程序上的蓝牙设备。视图的代码如下:蓝牙控制器列出了多个版本的设备 - 斯威夫特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() 
    } 
} 

这给了我以下结果使用蓝牙

UITableView displaying Bluetooth devices

有时它会列出我的笔记本电脑只有一次扫描时,但它是间歇性的,请让我知道我在做什么错了?

+0

之前被列入似乎要在外设阵列加入相同CBPeripheral对象。您必须将didDiscover方法中获得的CBPeripheral对象的标识符属性与数组中CBPeripheral对象的标识符属性进行比较。 –

+0

这是相同的问题在那里解释:http://stackoverflow.com/questions/43351664/why-is-corebluetooth-discovering-the-same-peripheral-again-and-again-and-again“积极的广告”。 – Larme

回答

1

看起来,相同的CBPeripheral对象再次插入到外围阵列中。

在添加之前,您必须比较didDiscover方法中获得的CBPeripheral对象的标识属性和外设数组中CBPeripheral对象的标识属性。

0

在didDiscover方法中,每次您需要比较已在外设阵列中可用的CBPeripheral的属性CDUUID。 如果不存在,那么你需要插入数组,否则离开它。

你得到CBUUID使用这条线 CBUUID * uuid = peripheral.UUID; 并且你检查这个uuid是否已经存在于数组中或者没有。

0

外围设备将继续广播数据,因此委托方法centralManager(_:didDiscover:advertisementData:rssi:)将多次接收。

为了解决这个问题,可以使用替换的Setperipherals类型,或者判定是否外设具有附加

+0

我会检查peripheral.identifier的唯一性,而不是假设CBPeripheral实例映射到发现的设备。我相信该框架可以为同一设备提供不同的实例。 – CuriousRabbit