I'm creating a plugin for the REAPER DAW to support a BLE MIDI input. Code is as follows:
fire_and_forget MidiDeviceService::connectDevice(hstring deviceId, bool updateConfig = true)
{
if (!deviceId.empty())
{
currentDevice = co_await MidiInPort::FromIdAsync(deviceId);
if (!currentDevice) {}
{
ShowMessageBox("Couldn't connect to device.", "Connection Error", 0);
co_return;
}
messageReceivedEventToken = currentDevice.MessageReceived({this, &MidiDeviceService::midiInPort_messageReceived});
}
if (updateConfig)
{
configuredDeviceId = deviceId;
setCurrentDeviceInConfig(configuredDeviceId);
}
}
This works like a charm when trying to connect to a MIDI device that's connected to the system. However, when trying to connect to a BLE MIDI device that is out of range, as soon as I co_await co_await MidiInPort::FromIdAsync, the calling thread freezes for five seconds and then the rest of the fire_and_forget is simply not executed.
I tried several things, including changing the fire_and_forget to an IAsyncAction and get the result from that in the synchronous code that executes this, but all with the same result. If I step through the coroutine with the Visual Studio debugger, after the co_await freezes the caller for five seconds, the cursor instantly jumps to the bottom of the coroutine.
I might be doing something horribly wrong, but there's no information in the Docs as of what the behavior should be when the connection to a device fails (e.g. should it just return nullptr, should it throw an exception)?