我如何使用Swift扫描ios中的Bluetooth经典设备?

问题描述

大家好,我尝试使用CoreBluetooth在ios上开始扫描蓝牙设备,但是我没有看到所有的蓝牙设备。我只看到了Macbook,手表Mi Band 4和电视等ble设备,但是我需要扫描所有设备,例如android和iphone。当我在互联网上寻找解决方案时,我没有找到答案,而是意识到最有可能的iPhone不支持Bluetooth Classic。我在使用蓝牙经典版的android java上执行了此逻辑,但是我也需要在ios上执行相同的操作。

这是我的代码


import UIKit
import CoreBluetooth


class ViewController: UIViewController,CBPeripheralDelegate,CBCentralManagerDelegate {
    
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        switch central.state {
          case .unkNown:
            print("central.state is .unkNown")
          case .resetting:
            print("central.state is .resetting")
          case .unsupported:
            print("central.state is .unsupported")
          case .unauthorized:
            print("central.state is .unauthorized")
          case .poweredOff:
            print("central.state is .poweredOff")
          case .poweredOn:
            print("central.state is .poweredOn")
        @unkNown default:
            print("default")
        }
    }
    func centralManager(_ central: CBCentralManager,diddiscover peripheral:
                            CBPeripheral,advertisementData: [String : Any],RSSi RSSI: NSNumber){
        print("Device name: ",peripheral.name ?? "none")
    }
    
    
    var centralManager: CBCentralManager!


    @IBAction func StartInspect(_ sender: Any) {
        centralManager.scanForperipherals(withServices: nil,options: nil)
        print("was clicked")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        centralManager = CBCentralManager(delegate: self,queue: nil)
        
    }


}

因此,我希望您能对我有所帮助,并说明如何在iOS上扫描所有蓝牙设备。

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)