0

I have a few questions regarding IOBluetooth framework which are listed below.

  1. I currently have one MacBook Pro (13-inch, 2018) running Big Sur v11.1 and when I call inquiries and attempt to find nearby devices it often fails and does not work consistently. On the other hand, I have a second MacBook Pro (13-inch, 2015) running Mojave v10.14.6 which does the exact same functions and always works each time. I have also been testing using blueutil command line tool: https://github.com/toy/blueutil as well as PyBluez: https://github.com/pybluez/pybluez and find that my second MacBook running Mojave always finds nearby devices while the MacBook running Big Sur has trouble doing so. Do you know if this is because of potential updates to the framework or is there something wrong with my laptop running Big Sur?

  2. I am trying to open L2CAPChannel from my first laptop to the second and vice versa but calling openL2CAPChannelSync on the IOBluetoothDevice object (which I have instantiated properly) seems to never return kIOReturnSuccess. Am I doing something wrong here as well? I attached a snippet of the code (in which I removed the addressString of my other device) I am using below.

import IOBluetooth
import PlaygroundSupport

class ChannelDelegate : IOBluetoothL2CAPChannelDelegate {

    func l2capChannelOpenComplete(_ l2capChannel: IOBluetoothL2CAPChannel!, status error: IOReturn) {
        print("Channel Opened!")
    }

}

var remoteDevice = IOBluetoothDevice(addressString: ***deviceString***)
print((remoteDevice?.name ?? "nil") as String)

remoteDevice?.openConnection()
var connection = remoteDevice?.isConnected()
print(connection!)

var channelPtr: AutoreleasingUnsafeMutablePointer<IOBluetoothL2CAPChannel?>?
var success = remoteDevice?.openL2CAPChannelSync(channelPtr, withPSM: 0x0000, delegate: ChannelDelegate())
print(success == kIOReturnSuccess)

PlaygroundPage.current.needsIndefiniteExecution = true
johnnyboy
  • 33
  • 6

1 Answers1

1

In regards to the second point, I fixed the code and is pasted below. The issue was that Apple's documentation stated that the object is instantiated using the function call openL2CAPChannelSync but that is not the case. You need to instantiate the object first then pass a reference to the object you instantiated. Hope this saves people some time given how little examples there are on IOBluetooth API.

import IOBluetooth
import PlaygroundSupport

class ChannelDelegate : IOBluetoothL2CAPChannelDelegate {

    func l2capChannelOpenComplete(_ l2capChannel: IOBluetoothL2CAPChannel!, status error: IOReturn) {
        print("Channel Opened!")
    }

}

var remoteDevice = IOBluetoothDevice(addressString: ***deviceString***)
print((remoteDevice?.name ?? "nil") as String)

remoteDevice?.openConnection()
var connection = remoteDevice?.isConnected()
print(connection!)

var channel: IOBluetoothL2CAPChannel? = IOBluetoothL2CAPChannel()
var success = remoteDevice?.openL2CAPChannelSync(&channel, withPSM: 0x0000, delegate: ChannelDelegate())
print(success == kIOReturnSuccess)

PlaygroundPage.current.needsIndefiniteExecution = true
johnnyboy
  • 33
  • 6