查看所有附近可发现的蓝牙设备 - Swift iOS XCode

时间:2020-12-27 22:50:59

I found a cool tutorial to code an app that lists all available Bluetooth Devices on an iOS 10 iPhone in Swift. It works O-K. Running the app on my iPhone with bluetooth on, it finds and presents my Macbook, a linux computer, and random 'unnamed' devices in a tableView. However, it cannot find my BeatsByDre wireless headphones, Raspberry Pi in Discoverable Mode nor a simple bluetooth dongle plugged into a computer (running linux).

我发现了一个很酷的教程来编写一个应用程序,该应用程序列出了Swift中iOS 10 iPhone上的所有可用蓝牙设备。它适用于O-K。在蓝牙上运行我的iPhone上的应用程序,它在tableView中找到并呈现我的Macbook,Linux计算机和随机的“未命名”设备。然而,它无法找到我的BeatsByDre无线耳机,Raspberry Pi处于可发现模式,也没有一个简单的蓝牙加密狗插入电脑(运行linux)。

My question is: what the hell am I not understanding/doing wrong?

我的问题是:我到底在理解/做错了什么?

Here's my code for the Table View:

这是我的表视图代码:

    import CoreBluetooth
    import UIKit

    class ScanTableViewController: UITableViewController,CBCentralManagerDelegate {

    var peripherals:[CBPeripheral] = []
    var manager: CBCentralManager? = nil
    var parentView:MainViewController? = nil


    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(_ animated: Bool) {
        scanBLEDevice()
    }
    func scanBLEDevice(){
        manager?.scanForPeripherals(withServices: nil, options: nil)

        DispatchQueue.main.asyncAfter(deadline: .now() + 60.0) {
            self.stopScanForBLEDevice()
        }

    }
    func stopScanForBLEDevice(){
        manager?.stopScan()
        print("scan stopped")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return peripherals.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "scanTableCell", for: indexPath)
        let peripheral = peripherals[indexPath.row]
        cell.textLabel?.text = peripheral.name
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let peripheral = peripherals[indexPath.row]
        manager?.connect(peripheral, options: nil)
    }

    //CBCentralMaganerDelegate code
    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        if (!peripherals.contains(peripheral)){
        peripherals.append(peripheral)
            }
        self.tableView.reloadData()   
        }
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        print(central.state)
    }
    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        // pass reference to connected peripheral to parentview
        parentView?.mainPeripheral = peripheral
        peripheral.delegate = parentView
        peripheral.discoverServices(nil)
        // set manager's delegate view to parent so it can call relevant disconnect methods
        manager?.delegate = parentView
        parentView?.customiseNavigationBar()

        if let navController = self.navigationController{
            navController.popViewController(animated: true)

        }
        print("Connected to "+peripheral.name!)
    }

    func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
        print(error!)
    }

Basically, I look for peripherals and list them to this Table View Controller. How come I can't see my wireless headphones in the list but I can see them under Settings>Bluetooth? Am I completely misunderstanding what a Peripheral is? I've read the Apple docs and then some. What am I supposed to look for in my code? Should I be using Beacons/iBeacon?

基本上,我查找外设并将它们列出到此表视图控制器。为什么我在列表中看不到我的无线耳机,但我可以在设置>蓝牙下看到它们?我完全误解了外围设备是什么吗?我已经阅读了Apple文档,然后阅读了一些。我应该在我的代码中寻找什么?我应该使用Beacons / iBeacon吗?

1 个解决方案

#1


2  

You will only be able to discover Bluetooth Low Energy (BLE) devices.

您将只能发现蓝牙低功耗(BLE)设备。

The devices you listed are Bluetooth 2.1 devices and cannot be seen by Core Bluetooth or are not advertising GATT services.

您列出的设备是蓝牙2.1设备,Core Bluetooth无法看到或者没有广告GATT服务。

#1


2  

You will only be able to discover Bluetooth Low Energy (BLE) devices.

您将只能发现蓝牙低功耗(BLE)设备。

The devices you listed are Bluetooth 2.1 devices and cannot be seen by Core Bluetooth or are not advertising GATT services.

您列出的设备是蓝牙2.1设备,Core Bluetooth无法看到或者没有广告GATT服务。