ios - 蓝牙 Controller 列出多个版本的设备 - Swift 3.1

标签 ios swift3 bluetooth-lowenergy core-bluetooth cbcentralmanager

我在作为小组项目的一部分构建的应用程序上扫描蓝牙设备时遇到问题。 View 的代码如下:

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

偶尔它只会列出我的笔记本电脑一次,但它是断断续续的,请告诉我我做错了什么?

最佳答案

在 didDiscover 方法中,每次你需要比较 CDUUID,它是 CBPeripheral 的属性,它已经在外围数组中可用。 如果不存在,则需要插入数组,否则保留它。

你使用这条线得到了 CBUUID CBUUID *uuid = peripheral.UUID; 并检查此 uuid 是否已存在于数组中。

关于ios - 蓝牙 Controller 列出多个版本的设备 - Swift 3.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43366877/

相关文章:

ios - iPhone 应用程序提交 - 拒绝二进制文件并上传新的二进制文件

ios - 在 UIScrollView Swift3 中加载 UIViewController

html - 如何在 Safari 上打开本地 html 文件?

ios - swift 3 : UIViewControllerTransitioningDelegate stopped working

android - fragment 中的蓝牙启用 Intent

ios - 从第一个 UIAlertController 打开第二个 UIAlertController

android - Nativescript tns 部署命令

ios - UITabBarController 中的 View 具有 16 像素缓冲区

android - Android API 20 是否允许支持蓝牙 LE 的设备充当外围设备?

c# - 桌面 C# - 引用 Windows.Devices