- Common ways to obtain UsbDevice
private void myMethod () {UsbDevice u =
Intent intent;String name;intent.getParcelableExtra(name)
Intent intent;String name;(UsbDevice) intent.getParcelableExtra(name)
- Smart code suggestions by Codota
}
public CommunicationHandler(UsbManager usbManager) { this.mUsbManager = usbManager; mUsbDevice = null; for (final UsbDevice device : mUsbManager.getDeviceList().values()) { Log.d(TAG, "VID : " + device.getVendorId() + "PID : " + device.getProductId()); if (device.getVendorId() == PSLAB_VENDOR_ID && device.getProductId() == PSLAB_PRODUCT_ID) { Log.d(TAG, "Found PSLAB Device"); mUsbDevice = device; device_found = true; break; } } mReadBuffer = new byte[DEFAULT_READ_BUFFER_SIZE]; mWriteBuffer = new byte[DEFAULT_WRITE_BUFFER_SIZE]; }
/** * Adds a USB device into available USB devices map with permission value. If the USB device * already exists, updates the USB device with new permission value. */ public void addOrUpdateUsbDevice(UsbDevice usbDevice, boolean hasPermission) { Preconditions.checkNotNull(usbDevice); Preconditions.checkNotNull(usbDevice.getDeviceName()); usbDevices.put(usbDevice.getDeviceName(), usbDevice); if (hasPermission) { grantPermission(usbDevice); } else { revokePermission(usbDevice, RuntimeEnvironment.application.getPackageName()); } }
public UsbDeviceAssert hasInterfaceCount(int count) { isNotNull(); int actualCount = actual.getInterfaceCount(); assertThat(actualCount) // .overridingErrorMessage("Expected interface count <%s> but was <%s>.", count, actualCount) // .isEqualTo(count); return this; }
if (usbVendorId != -1 && device.getVendorId() != usbVendorId) { return false; if (usbProductId != -1 && device.getProductId() != usbProductId) { return false; if (matches(device.getDeviceClass(), device.getDeviceSubclass(), device.getDeviceProtocol())) { return true; int count = device.getInterfaceCount(); for (int i = 0; i < count; i++) { UsbInterface intf = device.getInterface(i); if (matches(intf.getInterfaceClass(), intf.getInterfaceSubclass(), intf.getInterfaceProtocol())) { return true;
device = entry.getValue(); Log.d(TAG, String.format("USBDevice.HashMap (vid:pid) (%X:%X)-%b class:%X:%X name:%s", device.getVendorId(), device.getProductId(), UsbSerialDevice.isSupported(device), device.getDeviceClass(), device.getDeviceSubclass(), device.getDeviceName())); int deviceVID = device.getVendorId(); int devicePID = device.getProductId();
static public boolean isG4Connected(Context c){ UsbManager manager = (UsbManager) c.getSystemService(Context.USB_SERVICE); HashMap<String, UsbDevice> deviceList = manager.getDeviceList(); Log.i("USB DEVICES = ", deviceList.toString()); Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); Log.i("USB DEVICES = ", String.valueOf(deviceList.size())); while(deviceIterator.hasNext()){ UsbDevice device = deviceIterator.next(); if (device.getVendorId() == 8867 && device.getProductId() == 71 && device.getDeviceClass() == 2 && device.getDeviceSubclass() ==0 && device.getDeviceProtocol() == 0){ Log.i("CALIBRATION-CHECK-IN: ", "Dexcom Found!"); return true; } } return false; }
private void populateDataTable(LayoutInflater inflater) { final String vid = padLeft(Integer.toHexString(device.getVendorId()), "0", 4); final String pid = padLeft(Integer.toHexString(device.getProductId()), "0", 4); final String deviceClass = UsbConstantResolver.resolveUsbClass(device.getDeviceClass()); viewHolder.getReportedVendor().setText(device.getManufacturerName()); viewHolder.getReportedProduct().setText(device.getProductName()); } else { viewHolder.getReportedVendor().setText(R.string.not_provided); for (int i = 0; i < device.getInterfaceCount(); i++) { iFace = device.getInterface(i); if (iFace != null) { final TableLayout bottomTable = viewHolder.getBottomTable(); loadAsyncData(viewHolder, vid, pid, device.getManufacturerName()); } else { loadAsyncData(viewHolder, vid, pid, null);
public void open() throws IOException { if (!device_found) { throw new IOException("Device not Connected"); } mConnection = mUsbManager.openDevice(mUsbDevice); Log.d(TAG, "Claiming interfaces, count=" + mUsbDevice.getInterfaceCount()); mConnection.controlTransfer(0x21, 0x22, 0x1, 0, null, 0, 0); mControlInterface = mUsbDevice.getInterface(0); Log.d(TAG, "Control interface=" + mControlInterface); if (!mConnection.claimInterface(mControlInterface, true)) { throw new IOException("Could not claim control interface."); } mControlEndpoint = mControlInterface.getEndpoint(0); Log.d(TAG, "Control endpoint direction: " + mControlEndpoint.getDirection()); Log.d(TAG, "Claiming data interface."); mDataInterface = mUsbDevice.getInterface(1); Log.d(TAG, "data interface=" + mDataInterface); if (!mConnection.claimInterface(mDataInterface, true)) { throw new IOException("Could not claim data interface."); } mReadEndpoint = mDataInterface.getEndpoint(1); Log.d(TAG, "Read endpoint direction: " + mReadEndpoint.getDirection()); mWriteEndpoint = mDataInterface.getEndpoint(0); Log.d(TAG, "Write endpoint direction: " + mWriteEndpoint.getDirection()); connected = true; setBaudRate(1000000); //Thread.sleep(1000); clear(); }
/** * Enumerate the endpoints and interfaces on the connected device. * * @param device Device to query. * @return String description of the device configuration. */ public static String readDevice(UsbDevice device) { StringBuilder sb = new StringBuilder(); sb.append("Device Name: " + device.getDeviceName() + "\n"); sb.append(String.format( "Device Class: %s -> Subclass: 0x%02x -> Protocol: 0x%02x\n", nameForClass(device.getDeviceClass()), device.getDeviceSubclass(), device.getDeviceProtocol())); for (int i = 0; i < device.getInterfaceCount(); i++) { UsbInterface intf = device.getInterface(i); sb.append(String.format(Locale.US, "-- Interface %d Class: %s -> Subclass: 0x%02x -> Protocol: 0x%02x\n", intf.getId(), nameForClass(intf.getInterfaceClass()), intf.getInterfaceSubclass(), intf.getInterfaceProtocol())); sb.append(String.format(Locale.US, " -- Endpoint Count: %d\n", intf.getEndpointCount())); } return sb.toString(); }
@NonNull @Override public Info getDeviceInfo() { if (cachedInfo != null) { return cachedInfo; } UsbDevice usbDevice = null; for (final MidiInputDevice midiInputDevice : transmitters.keySet()) { usbDevice = midiInputDevice.getUsbDevice(); break; } if (usbDevice == null) { for (final MidiOutputDevice midiOutputDevice : receivers.keySet()) { usbDevice = midiOutputDevice.getUsbDevice(); break; } } if (usbDevice == null) { // XXX returns `null` information return cachedInfo = new Info("(null)", "(null)", "(null)", "(null)"); } return cachedInfo = new Info(usbDevice.getDeviceName(), // String.format("vendorId: %x, productId: %x", usbDevice.getVendorId(), usbDevice.getProductId()), // "deviceId:" + usbDevice.getDeviceId(), // usbDevice.getDeviceName()); }
static boolean deviceIsTrezor(UsbDevice usbDevice) { // no usable interfaces if (usbDevice.getInterfaceCount() <= 0) { return false; } // TREZOR v1 if (usbDevice.getVendorId() == 0x534c) { return usbDevice.getProductId() == 0x0001; } // TREZOR v2 if (usbDevice.getVendorId() == 0x1209) { return usbDevice.getProductId() == 0x53c0 || usbDevice.getProductId() == 0x53c1; } return false; }
/** * Delete HVC-P Device. * @param device device */ public void removeUSBDevice(final UsbDevice device) { HVCCameraInfo camera = mServices.remove("" + device.getDeviceId() + "_" + device.getProductId() + "_" + device.getVendorId()); if (camera != null) { // デバイスとの接続切断を通知. notifyOnDisconnected(camera); } }
public static UsbDevice getUsbDevice(final int vendorId, final int productId, final String search) { final UsbManager manager = (UsbManager) xdrip.getAppContext().getSystemService(Context.USB_SERVICE); if (manager == null) return null; final HashMap<String, UsbDevice> deviceList = manager.getDeviceList(); for (Map.Entry<String, UsbDevice> entry : deviceList.entrySet()) { final UsbDevice device = entry.getValue(); if (device.getVendorId() == vendorId && device.getProductId() == productId && device.toString().contains(search)) { Log.d(TAG, "Found device: " + entry.getKey() + " " + device.toString()); return device; } } return null; }
UsbManager usbManager = (UsbManager) getSystemService(USB_SERVICE); HashMap<String, UsbDevice> devicelist = usbManager.getDeviceList(); Iterator<UsbDevice> deviceIterator = devicelist.values().iterator(); while(deviceIterator.hasNext()) { UsbDevice usbDevice = deviceIterator.next(); Log.i(Log_Tag, "Model : " +usbDevice.getDeviceName()); Log.i(Log_Tag, "Id : " +usbDevice.getDeviceId()); }
UsbInterface usbInterface = mDevice.getInterface(0); if (mDevice.getDeviceClass() == 0x02) { mDeviceType = DEVICE_TYPE_0; } else { if (maxPacketSize0 == 64) { mDeviceType = DEVICE_TYPE_HX; } else if ((mDevice.getDeviceClass() == 0x00) || (mDevice.getDeviceClass() == 0xff)) { mDeviceType = DEVICE_TYPE_1; } else {
public CH34xSerialDevice(UsbDevice device, UsbDeviceConnection connection, int iface) { super(device, connection); rtsCtsEnabled = false; dtrDsrEnabled = false; mInterface = device.getInterface(iface >= 0 ? iface : 0); }
public UsbDeviceAssert hasVendorId(int id) { isNotNull(); int actualId = actual.getVendorId(); assertThat(actualId) // .overridingErrorMessage("Expected vendor ID <%s> but was <%s>.", id, actualId) // .isEqualTo(id); return this; } }
public UsbDeviceAssert hasProductId(int id) { isNotNull(); int actualId = actual.getProductId(); assertThat(actualId) // .overridingErrorMessage("Expected product ID <%s> but was <%s>.", id, actualId) // .isEqualTo(id); return this; }
public UsbDeviceAssert hasDeviceId(int id) { isNotNull(); int actualId = actual.getDeviceId(); assertThat(actualId) // .overridingErrorMessage("Expected device id <%s> but was <%s>.", id, actualId) // .isEqualTo(id); return this; }
public UsbDeviceAssert hasDeviceClass(int value) { isNotNull(); int actualValue = actual.getDeviceClass(); assertThat(actualValue) // .overridingErrorMessage("Expected device class <%s> but was <%s>.", value, actualValue) // .isEqualTo(value); return this; }