private void setupMidi() {
    // Setup MIDI
    mMidiManager = (MidiManager) getSystemService(MIDI_SERVICE);

    mMidiManager.registerDeviceCallback(
        new DeviceCallback() {
          @Override
          public void onDeviceRemoved(final MidiDeviceInfo info) {
            mOpenDeviceListAdapter.remove(info);
          }

          // Update port open counts so user knows if the device is in use.
          @Override
          public void onDeviceStatusChanged(final MidiDeviceStatus status) {
            MidiDeviceInfo info = status.getDeviceInfo();
            BluetoothMidiDeviceTracker tracker = mOpenDeviceListAdapter.getDevice(info);
            if (tracker != null) {
              tracker.outputOpenCount = 0;
              for (int i = 0; i < info.getOutputPortCount(); i++) {
                tracker.outputOpenCount += status.getOutputPortOpenCount(i);
              }
              tracker.inputOpenCount = 0;
              for (int i = 0; i < info.getInputPortCount(); i++) {
                tracker.inputOpenCount += status.isInputPortOpen(i) ? 1 : 0;
              }
              mOpenDeviceListAdapter.notifyDataSetChanged();
            }
          }
        },
        new android.os.Handler(Looper.getMainLooper()));
  }
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == REQUEST_BLUETOOTH_SCAN && resultCode == RESULT_OK) {
     final BluetoothDevice fBluetoothDevice = (BluetoothDevice) data.getParcelableExtra("device");
     if (fBluetoothDevice != null) {
       Log.i(
           TAG,
           "Bluetooth device name = "
               + fBluetoothDevice.getName()
               + ", address = "
               + fBluetoothDevice.getAddress());
       mMidiManager.openBluetoothDevice(
           fBluetoothDevice,
           new MidiManager.OnDeviceOpenedListener() {
             @Override
             public void onDeviceOpened(MidiDevice device) {
               onBluetoothDeviceOpen(fBluetoothDevice, device);
             }
           },
           null);
     }
   }
 }