Skip to content

Using the library without extending Activity

Kaoru Shoji edited this page Jul 23, 2014 · 2 revisions

If the application's Activity already extended from the another Activity class, the UsbMidiDriver can be used to add USB MIDI functions to the application Activity.

Work in progress! This class is currently available at develop branch or the 0.0.2-SNAPSHOT version.

UsbMidiDriver class

At the onCreate of the Activity or Fragment, create an instance of UsbMidiDriver class with Context object. And then, implement the unimplemented methods.

    // stores connected UsbDevice information
    private Set<UsbDevice> usbDevices = new HashSet<UsbDevice>();

    // the driver
    private UsbMidiDriver usbMidiDriver;

    public void onCreate(Bundle savedInstanceState) {
        super(savedInstanceState);
        usbMidiDriver = new UsbMidiDriver(this) {
            @Override
            public void onDeviceAttached(UsbDevice usbDevice) {
                // store usbDevice instance
                synchronized (usbDevices) {
                    usbDevices.add(usbDevice);
                }

                // TODO do something, like showing a toast message
            }


            @Override
            public void onDeviceDetached(UsbDevice usbDevice) {
                // remove usbDevice instance
                synchronized (usbDevices) {
                    usbDevices.remove(usbDevice);
                }

                // TODO do something, like showing a toast message
            }

            @Override
            public void onMidiNoteOn(final MidiInputDevice sender, int cable, int channel, int note, int velocity) {
                // TODO do something, like playing a sound
            }

            // 
            // a lot of method implementations(onMidi...) comes here.
            // 
        };
    }

    // For example, send a Note On message
    public void onNoteOnButtonPressed() {

        // obtain the first device from set
        Iterator usbDeviceIterator = usbDevices.iterator();
        if (usbDeviceIterator.hasNext()) {

            // find a MidiOutputDevice instance, and send message.
            List<MidiOutputDevice> midiOutputDevices = usbMidiDriver.getMidiOutputDevices(usbDeviceIterator.next());
            if (midiOutputDevices.size() > 0) {
            	midiOutputDevices.get(0).sendMidiNoteOn(0/*cable*/, 0/*channel*/, 60/*note*/, 127/*velocity*/);
            }
        }
    }

Initializing

At the beginning of using the UsbMidiDriver, the open method must be called. After calling the method open, USB device connection watching thread will start. If USB MIDI device will be connected to the android, the MIDI message processing thread will start.

    // This method will be called at clicking toggle button or something event
    // Move these into the inside of `onCreate`, is also OK.
    public void startUsingMidi() {
    	if (usbMidiDriver != null) {
            usbMidiDriver.open();
        }
    }

Finalizing

At the end of using the UsbMidiDriver, the close method must be called. After calling the method close, all the processing threads will be destoyed.

    // This method will be called at clicking toggle button or something event
    // Move these into the inside of `onDestroy`, is also OK.
    public void finishUsingMidi() {
    	if (usbMidiDriver != null) {
            usbMidiDriver.close();
        }
    }