如何使用Core Bluetooth框架在iOS 13中发现经典蓝牙设备

6
我正在尝试使用Core Bluetooth框架在我的运行于iOS-13的app中发现Bluetooth LE和Classic(BR/EDR)设备。但是我只能发现BLE设备。
如何扫描Bluetooth Classic设备?
在初始化CBCentralManager并获取CentralManager状态为已开启后,我通过传递Services和Options中的nil来扫描外围设备。
下面是代码:
private var cbManager: CBCentralManager!

override func viewDidLoad() {
     super.viewDidLoad()
     cbManager = CBCentralManager(delegate: self, queue: nil)
}

func centralManagerDidUpdateState(_ central: CBCentralManager) {
     switch central.state {
        case .poweredOn:
             cbManager.scanForPeripherals(withServices: nil, options:nil)

        default:
             os_log("Cleaning up cbManager")
     }
}


func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

     if let name = peripheral.name {
         print(name)
     }
}

我只能获取BLE设备,但期望的结果是:获取BLE和经典(BR/EDR)蓝牙设备列表。

1个回答

2
在你的.powerOn中添加以下内容,以便能够匹配特定的服务UUID。"最初的回答"。
let matchingOptions = [CBConnectionEventMatchingOption.serviceUUIDs: [uuid-to-classic-bt-device]]
            cbManager.registerForConnectionEvents(options: matchingOptions)

在委托方法centralManager:connectionEventDidOccur:forPeripheral:中,您可以检查事件.peerDisconnected.peerConnected,以对找到的外围设备进行操作。Apple提供了一个示例Using Core Bluetooth Classic。"最初的回答"

我该如何获取与特定设备配对使用的正确的“uuid-to-classic-bt-device”? - Andrea Gorrieri
@AndreaGorrieri 你能找出如何获取经典蓝牙设备的正确UUID吗? - sublimepremise
3
我发现新的BR/EDR支持仅适用于实现GATT配置文件的蓝牙经典设备。在"registerForConnectionEvents"方法中提供给matchingOptions的UUID是GATT UUID,而不是SDP UUID。这意味着使用CoreBluetooth时,您无法为实现A2DP或HFP配置文件的设备注册连接事件。请参见此处 https://stackoverflow.com/questions/58592698/registering-bluetooth-br-edr-classic-connection-in-ios13 - Andrea Gorrieri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接