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

时间:2016-11-08 22:58:34

标签: ios swift bluetooth core-bluetooth

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

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

这是我的表视图代码:

    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!)
    }

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

1 个答案:

答案 0 :(得分:2)

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

您列出的设备是蓝牙2.1设备,无法通过核心蓝牙看到,也不会宣传GATT服务。

相关问题