Ios 蓝牙控制器列出了设备的多个版本-Swift 3.1

Ios 蓝牙控制器列出了设备的多个版本-Swift 3.1,ios,swift3,bluetooth-lowenergy,core-bluetooth,cbcentralmanager,Ios,Swift3,Bluetooth Lowenergy,Core Bluetooth,Cbcentralmanager,我在作为团队项目一部分构建的应用程序上扫描蓝牙设备时遇到问题。视图的代码如下所示: import UIKit import CoreBluetooth class bluetoothConnectViewController: UIViewController, UITableViewDelegate, CBCentralManagerDelegate, UITableViewDataSource { //----------------------- // MARK: Va

我在作为团队项目一部分构建的应用程序上扫描蓝牙设备时遇到问题。视图的代码如下所示:

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()
    }
}
导入UIKit
导入核心蓝牙
Bluetooth ConnectViewController类:UIViewController、UITableViewDelegate、CBCentralManagerDelegate、UITableViewDataSource{
//-----------------------
//马克:变量
//-----------------------
var centralManager:CBCentralManager?
var peripherals=Array()
//-----------------------
//马克:出口
//-----------------------
@IBVAR表格视图:UITableView!
//-----------------------
//马克:核心职能
//-----------------------
重写func viewDidLoad(){
super.viewDidLoad()
//初始化核心蓝牙中央管理器
centralManager=CBCentralManager(委托:self,队列:DispatchQueue.main,选项:nil)
//表拉刷新
让refreshControl=UIRefreshControl()
refreshControl.addTarget(self,action:#选择器(refresh(:)),for:.valueChanged)
tableView.refreshControl=refreshControl
tableView.delegate=self
tableView.dataSource=self
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
//-----------------------
//标记:自定义函数
//-----------------------
func刷新(refreshControl:UIRefreshControl){
//做好你的工作,完成后:
refreshControl.endRefreshing()
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回外围设备计数
}
func tableView(tableView:UITableView,titleForHeaderInSection:Int)->String{
返回“连接到设备”
}
func numberOfSections(在tableView:UITableView中)->Int{
返回1
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
var cell=tableView.dequeueReusableCell(带有标识符:“cell”)
如果单元格==nil{
cell=UITableViewCell(样式:。默认值,reuseIdentifier:“单元格”)
}
let peripheral=peripherals[indexPath.row]
单元格?.textLabel?.text=peripheral.name
返回单元!
}
//-----------------------
//标记:可扩展函数
//-----------------------
func CentralManager数据状态(uCentral:CBCentralManager){
如果(central.state==.poweredOn){
self.centralManager?.scanForPeripherals(带服务:nil,选项:nil)
}
否则{
let alert=UIAlertController(标题:“未启用蓝牙”,消息:“启用蓝牙扫描devuice”,首选样式:。操作表)
addAction(UIAlertAction(标题:“确定”,样式:。取消,处理程序:nil))
addAction(UIAlertAction)(标题:“设置”,样式:。默认,处理程序:{(UIAlertAction)在
让url=url(字符串:“prefs:root=Bluetooth”)!
UIApplication.shared.open(url,选项:[:],completionHandler:nil)
}))
}
}
func centralManager(ucentral:CBCentralManager,didDiscover peripal:cbperipal,advertisementData:[String:Any],rssi-rssi:NSNumber){
外围设备。附加(外围设备)
tableView.reloadData()
}
}
当使用蓝牙进行扫描时,这给了我以下结果



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

似乎相同的CBPeripheral对象再次插入到外围阵列中


在添加之前,必须将在
didDiscover
方法中获得的CBPeripheral对象的标识符属性与外围阵列中CBPeripheral对象的标识符属性进行比较。

似乎相同的CBPeripheral对象再次插入到外围阵列中


在添加之前,您必须将在
didDiscover
方法中获得的CBPeripheral对象的标识符属性与外围阵列中CBPeripheral对象的标识符属性进行比较。

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

你用这条线得到了CBUUID CBUUID*uuid=peripheral.uuid;
并检查此uuid是否已存在于阵列中

每次需要比较CDUUID时,请使用didDiscover方法,CDUUID是CBPeripheral的属性,该属性已在外围阵列中可用。 如果不存在,则需要在数组中插入,否则将其保留

你用这条线得到了CBUUID CBUUID*uuid=peripheral.uuid;
并检查此uuid是否已存在于阵列中

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


要解决此问题,您可以使用
Set
替换
外围设备的类型,或者确定在附加外围设备之前是否已包含该外围设备将继续广播数据,因此委托方法
centralManager(uuxDiscover:advertisementData:rssi:)
将接收多次


要解决此问题,您可以使用
Set
替换
peripherals
的类型,或者在追加之前确定是否已包含该外围设备

似乎您正在将相同的CBperipherals对象添加到peripherals数组中。您必须将didDiscover方法中获得的CBPeripheral对象的标识符属性与数组中CBPeripheral对象的标识符属性进行比较。这与此处解释的问题相同:“积极广告”。似乎您正在添加相同的CBPeri