How to get a list of BLE devices through IOBluetooth framework (macOS)
Asked Answered
L

1

7

I have a problem listing all of the devices through IOBluetooth framework. Is there a way to get not only classic, but also BLE devices?

My code is

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

I know I can do that with CoreBluetooth, but I also need to filter devices to match a certain criteria.

From this answer I know that I can do that, but that answer lacks details. For now I'm just getting the list of classic bluetooth devices.

Could anyone please help?

UPD:

Now I found .searchType method with kIOBluetoothDeviceSearchClassic and kIOBluetoothDeviceSearchLE constants. In viewDidLoad I did the following:

ioBlutoothmanager.searchType = IOBluetoothDeviceSearchLE.rawValue but it still only finds the classic devices.

UPD:

All this time it was working so properly I didn't expect. It filtered all the devices I don't need and I didn't have AirPods to check that.

Logy answered 8/6, 2018 at 17:13 Comment(0)
L
9

Ok, so at last it's working. Just need to set the searchType property to IOBluetoothDeviceSearchLE.rawValue, and it excludes all the unnecessary devices from search – just exactly what I was looking for. Now my search query is exactly as the default Bluetooth Explorer.

ioBlutoothmanager.searchType = kIOBluetoothDeviceSearchLE.rawValue

Maybe it will help someone some day

Logy answered 11/6, 2018 at 13:53 Comment(2)
Documentation of the IOBluetooth framework is so sparse... would you care to post the complete program that you used for this?Lashawn
Hi, @corwin.amber, it's available on github github.com/havebeenfitz/touchbarconnectorLogy

© 2022 - 2024 — McMap. All rights reserved.