Unpredictable behavior when connecting to bluetooth mididevice

79 views
Skip to first unread message

Guido Vroombout

unread,
Mar 9, 2021, 7:55:04 PM3/9/21
to android-midi
I have a question about opening and closing bluetoothdevices and mididevices.

I've made an app with which you can practise playing notes. The app checks if the note played on the piano is the same note as displayed in the app. The app reuses the code from the MidiBtlePairing example.

Connecting to the piano (Roland FP30) with bluetooth however is troublesome:

- only the first time connecting succeeds (clean situation, after resetting the phone);
The app is connected to the piano (bluetooth light is lit up), the piano BTLE stops advertising because it's connected.
If I open an output ports data is sent from piano to app. The app works as expected.

- after closing this first connection, almost all attempts to reconnect thereafter fail;
opening the bluetoothdevice with MidiManager.openBluetoothDevice() is directly followed by a MidiManager.DeviceCallback.onDeviceRemoved (which is not initiated by calling Closeable.close() from the app).
The device ID in MidiDeviceInfo is increased, so it seems the device is opened (MidiService.addDeviceLocked, mNextDeviceId++) but it immediately is closed after opening.
Could it be possible that the close is initiated by the MidiService? If yes, why?

- sometimes, after closing the first connection, reconnecting with MidiManager.openBluetoothDevice() opens a mididevice without establishing a connection.
The bluetooth light on the piano is not lit up, the piano BTLE is still advertising.
I can open an output port on the mididevice but no data is exchanged.

If I close the app after 5 or 6 unsuccessfull connection attempts and reopen the app, the phone usually reboots after the first call to MidiManager.openBluetoothDevice().

I see this behaviour discussed in other posts on this forum (for instance BluetoothDevice and MidiDevice, 14-01-2019).
It looks like the mididevice (or bluetoothdevice) is not properly closed or properly administrated (in my app or in the MidiService?).
What could possibly cause this behaviour?

Any help here would be appreciated.
Thanks in advance.

Phil Burk

unread,
Mar 9, 2021, 8:41:06 PM3/9/21
to android-midi
Hello Guido,

Thanks for reporting this. I have not seen this behavior before.

What version of Android are you using?

Is the app in the Play Store? Can I try it?

> - after closing this first connection,

What steps do you take to close it?
Are you calling both openBluetoothDevice() and openDevice()? If so, are you closing both of those devices?

What do you get from:   adb shell dumpsys midi
when you are in the bad state?

If you take a bugreport we can help more. You can create a bug and attach the bugreport here:


Then please send me the bug ID.

Thanks,
Phil Burk
 

--
You received this message because you are subscribed to the Google Groups "android-midi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-midi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-midi/38d5f3e9-a7c8-4988-a587-33f5f68852c2n%40googlegroups.com.

Guido Vroombout

unread,
Mar 11, 2021, 5:51:55 PM3/11/21
to android-midi
Hi Phil,

I'm using Android 7.1.2.
The app is not in the Play Store. At this moment, the app is not finished and still in development.
I can send you the source if that helps.

The app uses the code of the MidiBtlePairing example but I modified it a little. I use one activity for both scanning the bluetoothdevices and managing the mididevices. I also use a RecyclerView.Adapter instead of a separate ListAdapter and Viewholder.
To make sure I eventually close the right MidiDevice (the one from the openBluetoothDevice() call) I store this MidiDevice in a global MidiDeviceList (application context). If I want to close the MidiDevice manually or if the MidiDevice is closed from the OnDestroy in the MainActivity (when leaving the app), the stored MidiDevice from this global list is used.
The function Closeable.close() is used to close the MidiDevice.

I use the MidiManager.openDevice call on 2 occasions:
1. In OnCreate of the MainActivity I call MidiManager.getDevices and MidiManager.openDevice to add every already existing midi device to the MidiDeviceList. All midi devices in the MidiDeviceList are manually or automatically (when closing the app) closed by a Closeable.close() call.
2. I use the miditools library and in the MidiOutputPortSelector file the MidiManager.openDevice is called from the spinner resource when trying to open an output port on the device. The onClose() function in the same file is used to close the device.

When opening a bluetooth device I use the same routine as in MidiBtlePairing: scan bluetooth device,  add bluetooth device to BluetoothRecyclerView.Adapter, when clicking on bluetooth device in BluetoothRecyclerView call openBluetoothDevice(), if onDeviceOpened() add MidiDevice to MidiRecyclerView.Adapter.
In the MidiRecyclerView, every entry has a close button which closes the MidiDevice (Closeable.close()).

See dumpsys midi sessions below.
Precondition: Phone is reset, app is started, DeviceScan activity started, bluetooth device found.

Action: select bluetooth device, openbluetoothdevice(), mididevice opened, added to mididevicelist
Result: midi device (piano) is in MidiDeviceList and connected

serranoltexx:/ # dumpsys midi
MIDI Manager State:
Devices:
Device Info: MidiDeviceInfo[mType=3,mInputPortCount=1,mOutputPortCount=1,mProperties=Bundle[{name=FP-30, bluetooth_device=EB:9A:35:EA:86:6E}],mIsPrivate=false Status: mInputPortOpen=[false] mOutputPortOpenCount=[0] UID: 10045 DeviceConnection count: 1 mServiceConnection: com.android.server.midi.MidiService$Device$1@b7bd85c
Clients:
Client: UID: 10062 PID: 3638 listener count: 0 Device Connections: <device 1>

Action: use close button, midi device is closed and removed from mididevicelist
Result: midi device (piano) is
not in MidiDeviceList and not connected

serranoltexx:/ # dumpsys midi

MIDI Manager State:
Devices:
Clients:

Action: rescan bluetooth, select bluetooth device, openbluetoothdevice(), mididevice opened, added to mididevicelist
Result: midi device (piano) is in MidiDeviceList but NOT connected (bluetooth light is not lit up, I can choose output port but no data is sent from piano to app)

serranoltexx:/ # dumpsys midi

MIDI Manager State:
Devices:
Device Info: MidiDeviceInfo[mType=3,mInputPortCount=1,mOutputPortCount=1,mProperties=Bundle[{name=FP-30, bluetooth_device=EB:9A:35:EA:86:6E}],mIsPrivate=false Status: null UID: 10045 DeviceConnection count: 1 mServiceConnection: com.android.server.midi.MidiService$Device$1@dc88f15
Clients:
Client: UID: 10062 PID: 3638 listener count: 0 Device Connections: <device 2>

Action: use close button, midi device is closed and removed from mididevicelist
Action: rescan bluetooth, select bluetooth device, openbluetoothdevice(), mididevice opened, added to mididevicelist and immediately OnDeviceRemoved is called
Result: midi device (piano) is
not in MidiDeviceList and not connected

serranoltexx:/ # dumpsys midi

MIDI Manager State:
Devices:
Clients:

Thanks for looking into it.
Guido

Op woensdag 10 maart 2021 om 02:41:06 UTC+1 schreef phil...@google.com:

Phil Burk

unread,
Mar 11, 2021, 6:37:44 PM3/11/21
to android-midi
Hello Guido,

Sorry. Your message got stuck in a pending queue. You are approved now so future message should go through.

Thanks for the additional info. This is puzzling.  I am trying to reproduce this on QT and S with my own apps but not seeing any unexpected behavior. (THe MIDI BLE app is missing the close icon but that is another issue.)

Once a device is opened by both openBluetoothDevice() and openDevice() then the Bluetooth connection should be preserved by retaining either object.

Try logging your calls to open and logging the result of the callbacks.
There may be some interaction with the garbage collector.

> , added to mididevicelist and immediately OnDeviceRemoved is called

I assume that is the unexpected behavior.  It is a little hard to sort out the actual bug.
Can you describe it in terms of...

REPRO STEPS:
EXPECTED BEHAVIOR:
ACTUAL BEHAVIOR:

Thanks,
Phil Burk

Guido Vroombout

unread,
Mar 15, 2021, 7:25:06 PM3/15/21
to 'Phil Burk' via android-midi
Hi Phil,

I created a bug report; bug ID = 182811259.
I am working on adding logging to every open/close/callback and will post it in the android midi group.

Let me know if you need additional information.

Thanks
Guido Vroombout

Phil Burk

unread,
Mar 15, 2021, 7:30:29 PM3/15/21
to android-midi
Hello Guido,
Thanks for opening the bug tracker at b/182811259
Rather than post lots of logs here, let's continue the discussion on the bug tracker.
Thanks,
Phil Burk



Reply all
Reply to author
Forward
0 new messages