Hello,
I am writing an app that communicates with a classic bluetooth device. I am attempting to integrate it into a swift ui front end.
I have created an async method that looks like :
func doIndex() async -> String {
obd.openConnection()
}
It this method is called from a task
.task{
indexOut = await appState.doIndex()
}
When I call this method, the bluetooth connection times out connecting every time.
** Obd> Status changed - Connecting to OBD Device
AddressString: 00-04-3e-5b-54-6f
Paired?: true
Connected?: false
channel 1 identified
2022-09-02 17:14:42.751753-0400 odbpoc[12934:355248] [IOBluetooth] -[IOBluetoothRFCOMMChannel waitforChanneOpen] CID:1 - timed out waiting to open
2022-09-02 17:14:42.752114-0400 odbpoc[12934:355248] [IOBluetooth] -[IOBluetoothDevice openRFCOMMChannelSync:withChannelID:delegate:] CID:1 error -536870212
If I run this code in the main loop, the open happens fine.
** Obd> Status changed - Connecting to OBD Device
AddressString: 00-04-3e-5b-54-6f
Paired?: true
Connected?: false
channel 1 identified
channel open !
** Obd> Status changed - Connected
I did some digging and see recommendations to add a runloop to the async method. When I do that, Xcode tells me Runloop.current shouldn't be used in async code. Is there a recommended method for doing background IOBluetooth work ?
Thank You,
David