如何通过IOBluetooth框架(macOS)获取BLE设备列表

Max*_*aev 7 macos bluetooth iobluetooth core-bluetooth

我有一个问题,通过IOBluetooth框架列出所有设备.有没有办法不仅可以获得经典设备,还可以获得BLE设备?

我的代码是

let ioBluetoothManager = IOBluetoothDeviceInquiry(delegate: self)
var ioDevices = [IOBluetoothDevice]()
...
ioBluetoothManager?.start()
...

func deviceInquiryStarted(_ sender: IOBluetoothDeviceInquiry!) {
    print("started")
}
///
//IOBluetoothDeviceInquiryDelegate
///

func deviceInquiryDeviceFound(_ sender: IOBluetoothDeviceInquiry!, device: IOBluetoothDevice!) {
    print("found device")
    ioDevices.append(device)
    scrubber.reloadData()
    tableView.reloadData()
}

func deviceInquiryComplete(_ sender: IOBluetoothDeviceInquiry!, error: IOReturn, aborted: Bool) {
    print("completed")
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以用CoreBluetooth做到这一点,但我还需要过滤设备以符合某个标准.

这个答案我知道我可以做到这一点,但答案缺乏细节.现在我只是获得经典蓝牙设备列表.

有人可以帮忙吗?

UPD:

现在我找到了.searchType方法kIOBluetoothDeviceSearchClassickIOBluetoothDeviceSearchLE常量.在viewDidLoad中,我执行了以下操作:

ioBlutoothmanager.searchType = IOBluetoothDeviceSearchLE.rawValue 但它仍然只能找到经典的设备.

UPD:

这段时间它的工作正常我没想到.它过滤了我不需要的所有设备,我没有AirPods来检查.

Max*_*aev 6

好的,终于可以了。只需将searchType属性设置为IOBluetoothDeviceSearchLE.rawValue,即可从搜索中排除所有不必要的设备-正是我想要的。现在,我的搜索查询与默认的蓝牙浏览器完全相同。

ioBlutoothmanager.searchType = kIOBluetoothDeviceSearchLE.rawValue
Run Code Online (Sandbox Code Playgroud)

也许有一天会有帮助

  • 你好,@corwin.amber,它可以在 github 上找到 https://github.com/havebeenfitz/touchbarconnector (2认同)