0

I'm scanning for Bluetooth devices on macOS 13.1 using IOBluetoothDeviceInquiry. The scanning itself works, but only the deviceInquiryDeviceFound delegate method gets called. For the remaining methods:

  • deviceInquiryStarted never fires, even though the inquiry obviously starts;
  • deviceInquiryComplete never fires, and indeed the inquiry never seems to end (the program keeps outputting Found peer... console logs);
  • When updateNewDeviceNames is set to true, the delegate methods related to updating device names are nonetheless never called.

Setting inquiryLength has no effect. The documentation states:

if you have the inquiry object updating device names for you, the whole inquiry process could be much longer than the specified length... If you -must- have a strict inquiry length, disable name updates.

But scanning continues indefinitely even when I set updateNewDeviceNames to false.

Here's my code:

import IOBluetooth

class Inquiry: NSObject {
    private lazy var inquiry: IOBluetoothDeviceInquiry = {
        let inquiry: IOBluetoothDeviceInquiry = IOBluetoothDeviceInquiry()
        inquiry.delegate = self
        inquiry.updateNewDeviceNames = false
        return inquiry
    }()
    lazy var foundDevices: [Any]! = self.inquiry.foundDevices()

    private var continuation: CheckedContinuation<Void, Never>?
    func start() async -> () {
        await withCheckedContinuation { continuation in
            self.continuation = continuation
            self.inquiry.start()
        }
    }
}

extension Inquiry: IOBluetoothDeviceInquiryDelegate {
    func deviceInquiryStarted(_ sender: IOBluetoothDeviceInquiry!) {
        print("inquiry started")
    }
    func deviceInquiryComplete(_ sender: IOBluetoothDeviceInquiry!, error: IOReturn, aborted: Bool) {
        print("inquiry complete")
        continuation?.resume()
    }
    func deviceInquiryDeviceFound(_ sender: IOBluetoothDeviceInquiry!, device: IOBluetoothDevice!) {
        print("device found: \(device.addressString!)")
    }
}

let inquiry: Inquiry = Inquiry()
await inquiry.start()
print(inquiry.foundDevices!)

As I'm writing a command line tool, I wrap the start method in a continuation. Other than that it's pretty much identical to other examples of IOBluetoothDeviceInquiry usage I've found (e.g. this one, which has the same problems when I try running it).

I'm really at a loss as to why this isn't working, any help would be greatly appreciated!

mingwei
  • 93
  • 6

0 Answers0