title: Is there a simple example of the new ANDROID M MIDI
body: <p>I am trying to read MIDI data in with the new Android Mushroom Midi.
  I am attempting to use the new MIDI method.</p>
  
  <p><a href="https://developer.android.com/reference/android/media/midi/package-summary.html" rel="nofollow noreferrer">https://developer.android.com/reference/android/media/midi/package-summary.html</a></p>
  
  <p>Android Midi has a example.  It contains spinners, context, wrappers and is way to complex to get a first understanding from.  It has many java programs and about 800 lines of code.  I only want to open device 0 and MIDI output port 2.</p>
  
  <p>Is there a simple example out there only using the classes m.openDevice and public void onDeviceOpened(MidiDevice device)?  I am searching for a first example the runs only in the main and produces a textview output.</p>
  
  <p>This seems simple as MIDI only sends three bits of data, key up down, key number, and impact.  After weeks of trying, can't seem to get it to work.  Here is oneIDI example of my code below.  It contains only about 30 lines of code.  That is all that I want.</p>
  
  <pre><code>// get the MIDI parameters out of the MIDI transmitter
  
  
      public void midi_info() {
  
          MidiManager m = (MidiManager) getSystemService(MIDI_SERVICE);
          //get the system device information
          final MidiDeviceInfo[] deviceInfo = m.getDevices();
  
          //Your devices only have info in array poiston 0
          MidiDeviceInfo info = deviceInfo[0];
  
  
          // get the midi device port information
          int numInputs = info.getInputPortCount();
          int numOutputs = info.getOutputPortCount();
  
          // test for a connection
          if (numOutputs &lt; 1) {
              //out put no prot found
              TextView textout = null;
              textout = (TextView) findViewById(R.id.log);
              textout.setTextColor(Color.RED);
              textout.setText("     Midi Port Disconnected");
              //send the device info to a test box
          } else {
              TextView textout = null;
              textout = (TextView) findViewById(R.id.log);
              textout.setTextColor(Color.BLUE);
              textout.setText(String.format(" Midi Input Port Number   =  %d  of %d \n" +
                      " Midi Output Port Number = %d  of %d\n" +
                      " \n" +
                      " %s", MidiDeviceInfo.PortInfo.TYPE_INPUT, numInputs, MidiDeviceInfo.PortInfo.TYPE_OUTPUT, numOutputs, deviceInfo[0].toString()));
  
  
          }
  
      }
  
      //method open_device opens midi device
      public void open_device() {
  
  
          //Call Midimanager within the onCreate method
          MidiManager m = (MidiManager) getSystemService(MIDI_SERVICE);
  
  
          MidiDeviceInfo[] info = m.getDevices();
          final MidiDeviceInfo info0 = info[0];
  
          //Open an instance of the class openDevice
  
          Bundle properties = info0.getProperties();
          final String name = properties.getString(MidiDeviceInfo.PROPERTY_NAME);
          final String deviceusb = properties.getString(MidiDeviceInfo.PROPERTY_USB_DEVICE);
  
          m.openDevice(info0, new OnDeviceOpenedListener() {
  
  
              @Override
  
  
              public void onDeviceOpened(MidiDevice device) {
  
  
  
                  if (name.equals("AndroidMidiScope0")) {
                      TextView textout = null;
                      textout = (TextView) findViewById(R.id.log);
                      textout.setTextColor(Color.RED);
                      textout.setText("Device failed to open");
                  } else {
  
  
                      TextView textout = null;
                      textout = (TextView) findViewById(R.id.log);
                      textout.setTextColor(Color.BLUE);
                      textout.setText(String.format("Device opened  = %s", device));
                      int portNumber = 2;
                      MidiOutputPort OutputPort = device.openOutputPort(portNumber);
  
  
  
  
                  }
  
  
              }
  
  
          }, new Handler(Looper.getMainLooper()));
      }
  
      }
  
  
  public class MyReceiver extends MidiReceiver {
  
  
      MidiDevice device =null;
      public void onSend(byte[] data, int offset, int count, long timestamp) throws IOException {
          TextView textout = null;
          textout = (TextView) textout.findViewById(R.id.log);
          textout.setTextColor(Color.RED);
          textout.setText(String.format("Midi Keydown detected  data =%s", data[0]));
  
      }
  
  
  
  
  }
  </code></pre>