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

查看附近所有可发现的蓝牙设备-Swift iOS XCode,ios,swift,bluetooth,core-bluetooth,Ios,Swift,Bluetooth,Core Bluetooth,我找到了一个代码,可以在Swift中列出iOS 10 iPhone上所有可用的蓝牙设备。它工作正常。在我的iPhone上运行该应用程序,并打开蓝牙,它会在桌面视图中查找并显示我的Macbook、linux电脑和随机的“未命名”设备。但是,它找不到我的BeatsByDre无线耳机、处于可发现模式的Raspberry Pi,也找不到插入计算机(运行linux)的简单蓝牙加密狗 我的问题是:我到底有什么不理解/做错了 以下是我的表视图代码: import CoreBluetooth i

我找到了一个代码,可以在Swift中列出iOS 10 iPhone上所有可用的蓝牙设备。它工作正常。在我的iPhone上运行该应用程序,并打开蓝牙,它会在桌面视图中查找并显示我的Macbook、linux电脑和随机的“未命名”设备。但是,它找不到我的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!)
    }

基本上,我查找外围设备,并将它们列在此表视图控制器中。为什么我在列表中看不到我的无线耳机,但我可以在“设置”>“蓝牙”下看到它们?我是否完全误解了外围设备是什么?我读过苹果的文档,还有一些。我应该在代码中查找什么?我应该使用信标/iBeacon吗?

您只能发现蓝牙低能(BLE)设备


您列出的设备是Bluetooth 2.1设备,核心Bluetooth无法看到,或者不是GATT服务的广告。

无线耳机是BLE4.0,BLE加密狗也是BLE4.0。我怎么才能找到那些?如中所述,我还将使用什么样的框架?他们需要宣传GATT服务。您可以使用Bluez在覆盆子Pi上执行此操作(我相信,我从来没有这样做过)。我怀疑耳机是否会使用GATT,即使它们是Bluetooth 4.0。您也可以使用app Store上的LightBlue应用程序进行确认。您知道Rasp-Pi3车载Bluetooth是否会这样做吗?我也一直在使用RPi,但应用程序没有看到RPi。我已经在Rpi上安装了Bluez,并在iPhone上下载了LightBlue。即使LightBlue在discover模式下也不会发现RPi discover不适用;你需要宣传一个GATT服务,上面的代码不起作用。在扫描外设之前,您需要确保状态为开机。