USB Midi Device Hot Unplugged

Is there a way to recover a usb midi device if it has been unplugged and plugged back in without restarting the juce application?

More specifically, I would like to be able to send a message to a device, have that device restart, at which time it re-enumerates as a midi device (USB Audio Device), and be able to send more messages without the need of restarting my application.

I have tried something like this:

//Init Midi Device
while(MidiOutput::getDevices().contains(midiDevice)); //wait for device to unload
while(!MidiOutput::getDevices().contains(midiDevice));//wait for device to come back
audioDeviceManager.setDefaultMidiOutput (outputName);

if I try to send midi data now it will not work.

Any ideas??

That code, although logical, looks a bit more like scripting than C++. At a guess, if you’re doing this on the main thread, nothing is really happening while you’re trapped your app waiting for something to happen. A better bet would be to look at something like timers, and to ‘check back’ with your USb device. You could also move your code to a thread, but you would want to put waits (yields) in between every loop.

In case it’s a Midi or USB oddity, try making some manual buttons to do what you need and try the sequence manually, that will establish whether your steps will give the right result, exact method aside.

There’s some ideas.