USB MIDI connect/disconnect bug

Using iOS 8.4.1 on iPad mini.

It seems that the MidiInput::getDevices() returns "<error>" (i.e. the device name string is blank) when unplugging a USB Midi device from the iPad.

I created a Hello World project (based on GUI IntroJucer template) with the following code:

#include "MainComponent.h"

String mainMessage;

int count = 0;


//==============================================================================

MainContentComponent::MainContentComponent()

{

    setSize (600, 400);

    mainMessage = "Nothing yet";

    startTimer(1000);

}


MainContentComponent::~MainContentComponent()

{

    stopTimer();

}


void MainContentComponent::timerCallback() {

    StringArray midiInputs = MidiInput::getDevices();

    mainMessage = String(count++)+": "+midiInputs[0];

    repaint();

}

void MainContentComponent::paint (Graphics& g)

{

    g.fillAll (Colour (0xff001F36));


    g.setFont (Font (16.0f));

    g.setColour (Colours::white);

    g.drawText (mainMessage, getLocalBounds(), Justification::centred, true);

}

Before the MIDI Device is unplugged mainMessage is "Network Session 1". 

When the device is unplugged, after a few seconds, mainMessage contains "<error>".

Note that it also shows the same problem as described here: http://www.juce.com/forum/topic/issues-midioutputgetdevices. I also see the abort app syndrom described there, but the workaround (as Jules suggested) is to wait for a few seconds before re-launching the app. However in the case above, this is really annoying and I can't even find a workaround.

Is that a bug or am I doing something wrong ?

If that can help fix the problem: 

If I open the port corresponding to the newly connected device, the problem seems to disappear:

    if (midiInputs.size() > 1) {

        MidiInput* midiIn = MidiInput::openDevice(1, dummyMidiInputCallback);

    }

Could it be that the system needs to assign a callback even if the device is not used ?

 

Hmm. I don't think it has anything to do with the callback, it'll be dependent on whether you've got the device open or not. 

If you look inside the implementation of MidiInput::getDevices() you'll see it's actually a very thin wrapper around what iOS provides. We just ask it for the list of devices, and the string "<error>" is used if a device has no name.. I guess what's happening is that the OS is just dropping device connections after a timeout - I'm not sure what we could do to change that behaviour.

bummer... The iOS should just remove the device instead of returning a blank name, as I said in my first post (yes, I did have a look at the getDevices code). But I wanted to know if that was due to the OS itself or something you did in the deep layers of your code (where I didn't dare to go...).

I guess you answered that. Thanks.