Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 464
Max Kraev Avatar asked Jun 08 '18 17:06

Max Kraev


1 Answers

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

like image 110
Max Kraev Avatar answered Oct 25 '22 19:10

Max Kraev