public static String formatDeviceInfo(MidiDeviceInfo info) { StringBuilder sb = new StringBuilder(); if (info != null) { Bundle properties = info.getProperties(); for (String key : properties.keySet()) { Object value = properties.get(key); sb.append(key).append(" = ").append(value).append('\n'); } for (PortInfo port : info.getPorts()) { sb.append((port.getType() == PortInfo.TYPE_INPUT) ? "input" : "output"); sb.append("[").append(port.getPortNumber()).append("] = \"").append(port.getName() + "\"\n"); } } return sb.toString(); }
@Override public void onPortSelected(final MidiPortWrapper wrapper) { close(); final MidiDeviceInfo info = wrapper.getDeviceInfo(); if (info != null) { mMidiManager.openDevice(info, new MidiManager.OnDeviceOpenedListener() { @Override public void onDeviceOpened(MidiDevice device) { if (device == null) { Log.e(MidiConstants.TAG, "could not open " + info); } else { mOpenDevice = device; mInputPort = mOpenDevice.openInputPort( wrapper.getPortIndex()); if (mInputPort == null) { Log.e(MidiConstants.TAG, "could not open input port on " + info); } } } }, null); // Don't run the callback on the UI thread because openInputPort might take a while. } }
/** * @return a device that matches the manufacturer and product or null */ public static MidiDeviceInfo findDevice(MidiManager midiManager, String manufacturer, String product) { for (MidiDeviceInfo info : midiManager.getDevices()) { String deviceManufacturer = info.getProperties() .getString(MidiDeviceInfo.PROPERTY_MANUFACTURER); if ((manufacturer != null) && manufacturer.equals(deviceManufacturer)) { String deviceProduct = info.getProperties() .getString(MidiDeviceInfo.PROPERTY_PRODUCT); if ((product != null) && product.equals(deviceProduct)) { return info; } } } return null; }
private void updateString() { if (mInfo == null) { mString = "- - - - - -"; } else { StringBuilder sb = new StringBuilder(); String name = mInfo.getProperties() .getString(MidiDeviceInfo.PROPERTY_NAME); if (name == null) { name = mInfo.getProperties() .getString(MidiDeviceInfo.PROPERTY_MANUFACTURER) + ", " + mInfo.getProperties() .getString(MidiDeviceInfo.PROPERTY_PRODUCT); } sb.append("#" + mInfo.getId()); sb.append(", ").append(name); PortInfo portInfo = findPortInfo(); sb.append("[" + mPortIndex + "]"); if (portInfo != null) { sb.append(", ").append(portInfo.getName()); } else { sb.append(", null"); } mString = sb.toString(); } }
private void findMidiDevice() { MidiDeviceInfo[] infos = midiManager.getDevices(); for(MidiDeviceInfo info : infos) { String name = info.getProperties().getString(MidiDeviceInfo.PROPERTY_NAME); logger.log("Found MIDI device named " + name); if(TEENSY_MIDI_NAME.equals(name)) { logger.log("^^^ using this device ^^^"); isConnecting = true; midiManager.openDevice(info, new MidiManager.OnDeviceOpenedListener() { @Override public void onDeviceOpened(MidiDevice device) { if (device == null) { logger.log("Error, unable to open MIDI device"); } else { logger.log("Opened MIDI device successfully!"); midiDevice = device; } isConnecting = false; } }, null); break; } } }
@Override public void onDeviceAdded(final MidiDeviceInfo device) { // Call all of the locally registered callbacks. for(Map.Entry<DeviceCallback, Handler> item : mCallbacks.entrySet()) { final DeviceCallback callback = item.getKey(); Handler handler = item.getValue(); if(handler == null) { callback.onDeviceAdded(device); } else { handler.post(new Runnable() { @Override public void run() { callback.onDeviceAdded(device); } }); } } }
@Override public void onDeviceRemoved(final MidiDeviceInfo device) { for(Map.Entry<DeviceCallback, Handler> item : mCallbacks.entrySet()) { final DeviceCallback callback = item.getKey(); Handler handler = item.getValue(); if(handler == null) { callback.onDeviceRemoved(device); } else { handler.post(new Runnable() { @Override public void run() { callback.onDeviceRemoved(device); } }); } } }
private void registerDeviceAddedCallback(final MidiManager midiManager) { midiManager.registerDeviceCallback(new MidiManager.DeviceCallback() { @Override public void onDeviceAdded(MidiDeviceInfo device) { if (device.getOutputPortCount() > 0) { midiManager.unregisterDeviceCallback(this); openDevice(midiManager, device); } } }, null); }
@Override public void onDeviceOpened(MidiDevice device) { this.device = device; stream(device.getInfo().getPorts()) .filter(port -> port.getType() == MidiDeviceInfo.PortInfo.TYPE_OUTPUT).findFirst() .ifPresent(port -> device.openOutputPort(port.getPortNumber()).connect(this)); }
@Override public void onPortSelected(final MidiPortWrapper wrapper) { Log.i(MidiConstants.TAG, "onPortSelected: " + wrapper); close(); final MidiDeviceInfo info = wrapper.getDeviceInfo(); if (info != null) { mMidiManager.openDevice(info, new MidiManager.OnDeviceOpenedListener() { @Override public void onDeviceOpened(MidiDevice device) { if (device == null) { Log.e(MidiConstants.TAG, "could not open " + info); } else { mOpenDevice = device; mOutputPort = device.openOutputPort(wrapper.getPortIndex()); if (mOutputPort == null) { Log.e(MidiConstants.TAG, "could not open output port for " + info); return; } mOutputPort.connect(mDispatcher); } } }, null); // Don't run the callback on the UI thread because openOutputPort might take a while. } }
@Override public void onDeviceAdded(final MidiDeviceInfo info) { int portCount = getInfoPortCount(info); for (int i = 0; i < portCount; ++i) { MidiPortWrapper wrapper = new MidiPortWrapper(info, mType, i); mAdapter.add(wrapper); Log.i(MidiConstants.TAG, wrapper + " was added"); mAdapter.notifyDataSetChanged(); } }
@Override public void onDeviceRemoved(final MidiDeviceInfo info) { int portCount = getInfoPortCount(info); for (int i = 0; i < portCount; ++i) { MidiPortWrapper wrapper = new MidiPortWrapper(info, mType, i); MidiPortWrapper currentWrapper = mCurrentWrapper; mAdapter.remove(wrapper); // If the currently selected port was removed then select no port. if (wrapper.equals(currentWrapper)) { clearSelection(); } mAdapter.notifyDataSetChanged(); Log.i(MidiConstants.TAG, wrapper + " was removed"); } }
@Override public void onDeviceStatusChanged(final MidiDeviceStatus status) { // If an input port becomes busy then remove it from the menu. // If it becomes free then add it back to the menu. if (mType == MidiDeviceInfo.PortInfo.TYPE_INPUT) { MidiDeviceInfo info = status.getDeviceInfo(); Log.i(MidiConstants.TAG, "MidiPortSelector.onDeviceStatusChanged status = " + status + ", mType = " + mType + ", activity = " + mActivity.getPackageName() + ", info = " + info); // Look for transitions from free to busy. int portCount = info.getInputPortCount(); for (int i = 0; i < portCount; ++i) { MidiPortWrapper wrapper = new MidiPortWrapper(info, mType, i); if (!wrapper.equals(mCurrentWrapper)) { if (status.isInputPortOpen(i)) { // busy? if (!mBusyPorts.contains(wrapper)) { // was free, now busy mBusyPorts.add(wrapper); mAdapter.remove(wrapper); mAdapter.notifyDataSetChanged(); } } else { if (mBusyPorts.remove(wrapper)) { // was busy, now free mAdapter.add(wrapper); mAdapter.notifyDataSetChanged(); } } } } } }
/** * @param midiManager * @param activity * @param spinnerId * @param type */ public MidiOutputPortConnectionSelector(MidiManager midiManager, Activity activity, int spinnerId, MidiDeviceInfo destinationDeviceInfo, int destinationPortIndex) { super(midiManager, activity, spinnerId, MidiDeviceInfo.PortInfo.TYPE_OUTPUT); mDestinationDeviceInfo = destinationDeviceInfo; mDestinationPortIndex = destinationPortIndex; }
/** * Open a source device and connect its output port to the * destinationInputPort. * * @param sourceDeviceInfo * @param sourcePortIndex * @param destinationInputPort */ private void connectToDevicePort(final MidiDeviceInfo sourceDeviceInfo, final int sourcePortIndex, final MidiInputPort destinationInputPort, final OnPortsConnectedListener listener, final Handler handler) { mMidiManager.openDevice(sourceDeviceInfo, new MidiManager.OnDeviceOpenedListener() { @Override public void onDeviceOpened(MidiDevice device) { if (device == null) { Log.e(MidiConstants.TAG, "could not open " + sourceDeviceInfo); safeClose(); if (listener != null) { listener.onPortsConnected(null); } } else { Log.i(MidiConstants.TAG, "connectToDevicePort opened " + sourceDeviceInfo); // Device was opened so connect the ports. mSourceDevice = device; mConnection = device.connectPorts( destinationInputPort, sourcePortIndex); if (mConnection == null) { Log.e(MidiConstants.TAG, "could not connect to " + sourceDeviceInfo); safeClose(); } if (listener != null) { listener.onPortsConnected(mConnection); } } } }, handler); }
private void setupMidi() { // Setup MIDI mMidiManager = (MidiManager) getSystemService(MIDI_SERVICE); MidiDeviceInfo synthInfo = MidiTools.findDevice(mMidiManager, "AndroidTest", "SynthExample"); int portIndex = 0; mPortSelector = new MidiOutputPortConnectionSelector(mMidiManager, this, R.id.spinner_synth_sender, synthInfo, portIndex); mPortSelector.setConnectedListener(new MyPortsConnectedListener()); }
public static boolean checkUsbMidi(Context context) { MidiManager midiManager = (MidiManager) context.getSystemService(Context.MIDI_SERVICE); if (midiManager != null) { final MidiDeviceInfo[] infos = midiManager.getDevices(); if (infos.length > 0) { return true; } else { return false; } } else { return false; } }
/** * Returns the product name. */ @CalledByNative String getProduct() { String product = getProperty(MidiDeviceInfo.PROPERTY_PRODUCT); // TODO(crbug.com/636455): Following code to use PROPERTY_NAME is a // workaround for a BLE MIDI device issue that Android does not provide // information for PROPERTY_MANUFACTURER, PROPERTY_PRODUCT, and // PROPERTY_VERSION. Confirmed on Android M and N. // See discussion at http://crbug.com/636455 and http://b/32259464. if (product == null || product.isEmpty()) { return getProperty(MidiDeviceInfo.PROPERTY_NAME); } return product; }
private void openDevice(final MidiDeviceInfo info) { mManager.openDevice(info, new MidiManager.OnDeviceOpenedListener() { @Override public void onDeviceOpened(MidiDevice device) { MidiManagerAndroid.this.onDeviceOpened(device, info); } }, mHandler); }
/** * Called when a midi device is attached. * @param info the attached device information. */ private void onDeviceAdded(final MidiDeviceInfo info) { if (!mIsInitialized) { mPendingDevices.add(info); } openDevice(info); }
/** * Called when a midi device is detached. * @param info the detached device information. */ private void onDeviceRemoved(MidiDeviceInfo info) { for (MidiDeviceAndroid device : mDevices) { if (device.isOpen() && device.getInfo().getId() == info.getId()) { device.close(); nativeOnDetached(mNativeManagerPointer, device); } } }
private void onDeviceOpened(MidiDevice device, MidiDeviceInfo info) { mPendingDevices.remove(info); if (device != null) { MidiDeviceAndroid xdevice = new MidiDeviceAndroid(device); mDevices.add(xdevice); if (mIsInitialized) { nativeOnAttached(mNativeManagerPointer, xdevice); } } if (!mIsInitialized && mPendingDevices.isEmpty()) { nativeOnInitialized(mNativeManagerPointer, mDevices.toArray(new MidiDeviceAndroid[0])); mIsInitialized = true; } }
private void setupMidi() { // Setup MIDI mMidiManager = (MidiManager) getSystemService(MIDI_SERVICE); MidiDeviceInfo synthInfo = MidiTools.findDevice(mMidiManager, "Mobileer", "SynthExample"); int portIndex = 0; mPortSelector = new MidiOutputPortConnectionSelector(mMidiManager, this, R.id.spinner_synth_sender, synthInfo, portIndex); mPortSelector.setConnectedListener(new MyPortsConnectedListener()); }
@Override public void onPortSelected(final MidiPortWrapper wrapper) { close(); final MidiDeviceInfo info = wrapper.getDeviceInfo(); if (info != null) { mMidiManager.openDevice(info, new MidiManager.OnDeviceOpenedListener() { @Override public void onDeviceOpened(MidiDevice device) { if (device == null) { Log.e(MidiConstants.TAG, "could not open " + info); } else { mOpenDevice = device; mOutputPort = device.openOutputPort(wrapper.getPortIndex()); if (mOutputPort == null) { Log.e(MidiConstants.TAG, "could not open output port for " + info); return; } mOutputPort.connect(mDispatcher); } } }, null); // Don't run the callback on the UI thread because openOutputPort might take a while. } }
@Override public void onDeviceAdded(final MidiDeviceInfo info) { int portCount = getInfoPortCount(info); for (int i = 0; i < portCount; ++i) { MidiPortWrapper wrapper = new MidiPortWrapper(info, mType, i); mAdapter.add(wrapper); Log.i(MidiConstants.TAG, wrapper + " was added to " + this); mAdapter.notifyDataSetChanged(); } }