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(); }
UsbCardDevice(Context context, UsbDevice usbDevice) throws IOException { super(context); this.usbDevice = usbDevice; UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); if (usbManager == null) { throw new IOException(context.getString(R.string.failed_open_usb_connection)); } usbDeviceConnection = usbManager.openDevice(usbDevice); if (usbDeviceConnection == null) { throw new IOException(context.getString(R.string.failed_open_usb_connection)); } }
private void createAllPorts(UsbDeviceStatus usbDeviceStatus){ int interfaceCount = usbDeviceStatus.usbDevice.getInterfaceCount(); for(int i=0;i<=interfaceCount-1;i++) { if(usbDeviceStatus.usbDeviceConnection == null) { usbDeviceStatus.usbDeviceConnection = usbManager.openDevice(usbDeviceStatus.usbDevice); } UsbSerialDevice usbSerialDevice = UsbSerialDevice.createUsbSerialDevice( usbDeviceStatus.usbDevice, usbDeviceStatus.usbDeviceConnection, i); serialDevices.add(usbSerialDevice); } }
/** * Opens a USB connection to this device. * * @param context the current context, e.g., the activity invoking this method * @throws ConnectionFailedException */ public synchronized void open(Context context) throws ConnectionFailedException { close(); UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE); connection = manager.openDevice(device); if (connection == null) { throw new ConnectionFailedException(); } }
private UsbDeviceConnection openInterface(UsbManager manager, UsbDevice device, UsbInterface iface) throws UsbDeviceException { UsbDeviceConnection connection = manager.openDevice(device); if(connection == null) { throw new UsbDeviceException("Couldn't open a connection to " + "device -- user may not have given permission"); } mInterface = iface; connection.claimInterface(mInterface, true); return connection; }
@Override public void onReceive(Context arg0, Intent arg1) { if (arg1.getAction().equals(ACTION_USB_PERMISSION)) { boolean granted = arg1.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED); if (granted) // User accepted our USB connection. Try to open the device as a serial port { Intent intent = new Intent(ACTION_USB_PERMISSION_GRANTED); arg0.sendBroadcast(intent); connection = usbManager.openDevice(device); new ConnectionThread().start(); } else // User not accepted our USB connection. Send an Intent to the Main Activity { Intent intent = new Intent(ACTION_USB_PERMISSION_NOT_GRANTED); arg0.sendBroadcast(intent); } } else if (arg1.getAction().equals(ACTION_USB_ATTACHED)) { if (!serialPortConnected) findSerialPortDevice(); // A USB device has been attached. Try to open it as a Serial port } else if (arg1.getAction().equals(ACTION_USB_DETACHED)) { // Usb device was disconnected. send an intent to the Main Activity Intent intent = new Intent(ACTION_USB_DISCONNECTED); arg0.sendBroadcast(intent); if (serialPortConnected) { serialPort.syncClose(); } serialPortConnected = false; } } };
@Override public void onReceive(Context arg0, Intent arg1) { if (arg1.getAction().equals(ACTION_USB_PERMISSION)) { boolean granted = arg1.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED); if (granted) // User accepted our USB connection. Try to open the device as a serial port { Intent intent = new Intent(ACTION_USB_PERMISSION_GRANTED); arg0.sendBroadcast(intent); connection = usbManager.openDevice(device); new ConnectionThread().start(); } else // User not accepted our USB connection. Send an Intent to the Main Activity { Intent intent = new Intent(ACTION_USB_PERMISSION_NOT_GRANTED); arg0.sendBroadcast(intent); } } else if (arg1.getAction().equals(ACTION_USB_ATTACHED)) { if (!serialPortConnected) findSerialPortDevice(); // A USB device has been attached. Try to open it as a Serial port } else if (arg1.getAction().equals(ACTION_USB_DETACHED)) { // Usb device was disconnected. send an intent to the Main Activity Intent intent = new Intent(ACTION_USB_DISCONNECTED); arg0.sendBroadcast(intent); if (serialPortConnected) { serialPort.close(); } serialPortConnected = false; } } };
@Override public void onReceive(Context arg0, Intent arg1) { if (arg1.getAction().equals(ACTION_USB_PERMISSION)) { boolean granted = arg1.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED); if (granted) // User accepted our USB connection. Try to open the device as a serial port { Intent intent = new Intent(ACTION_USB_PERMISSION_GRANTED); arg0.sendBroadcast(intent); connection = usbManager.openDevice(device); serialPortConnected = true; new ConnectionThread().run(); } else // User not accepted our USB connection. Send an Intent to the Main Activity { Intent intent = new Intent(ACTION_USB_PERMISSION_NOT_GRANTED); arg0.sendBroadcast(intent); } } else if (arg1.getAction().equals(ACTION_USB_ATTACHED)) { if (!serialPortConnected) findSerialPortDevice(); // A USB device has been attached. Try to open it as a Serial port } else if (arg1.getAction().equals(ACTION_USB_DETACHED)) { // Usb device was disconnected. send an intent to the Main Activity Intent intent = new Intent(ACTION_USB_DISCONNECTED); arg0.sendBroadcast(intent); serialPortConnected = false; serialPort.close(); } } };
@Override public void onReceive(Context arg0, Intent arg1) { if (arg1.getAction().equals(ACTION_USB_PERMISSION)) { boolean granted = arg1.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED); if (granted) // User accepted our USB connection. Try to open the device as a serial port { Intent intent = new Intent(ACTION_USB_PERMISSION_GRANTED); arg0.sendBroadcast(intent); connection = usbManager.openDevice(device); new ConnectionThread().start(); } else // User not accepted our USB connection. Send an Intent to the Main Activity { Intent intent = new Intent(ACTION_USB_PERMISSION_NOT_GRANTED); arg0.sendBroadcast(intent); } } else if (arg1.getAction().equals(ACTION_USB_ATTACHED)) { if (!serialPortConnected) findSerialPortDevice(); // A USB device has been attached. Try to open it as a Serial port } else if (arg1.getAction().equals(ACTION_USB_DETACHED)) { // Usb device was disconnected. send an intent to the Main Activity Intent intent = new Intent(ACTION_USB_DISCONNECTED); arg0.sendBroadcast(intent); if (serialPortConnected) { serialPort.syncClose(); readThread.setKeep(false); } serialPortConnected = false; } } };
@Override public void onReceive(Context context, Intent intent) { if (usbDevice == null) { logger.log("USB device was not properly opened"); return; } if(intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false) && usbDevice.equals(intent.getParcelableExtra(UsbManager.EXTRA_DEVICE))){ usbConnection = usbManager.openDevice(usbDevice); BaseUsbConnection.this.context.registerReceiver(disconnectReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED)); onConnect(); broadcastManager.sendBroadcast(new Intent(getConnectIntent())); } else { logger.log("Could not get permission to open the USB device"); } BaseUsbConnection.this.context.unregisterReceiver(respondToUsbPermission); } };
mRequestPermissionDevices.add(deviceName); } else { UsbDeviceConnection connection = mUsbManager.openDevice(usbDevice); if (connection != null) { MtpDevice mtpDevice = new MtpDevice(usbDevice);
.openDevice(sPort.getDriver().getDevice()); if (connection == null)
/** * Initiate a control transfer to request the device information * from its descriptors. * * @param device USB device to query. */ private void printDeviceDetails(UsbDevice device) { UsbDeviceConnection connection = mUsbManager.openDevice(device); String deviceString = ""; try { //Parse the raw device descriptor deviceString = DeviceDescriptor.fromDeviceConnection(connection) .toString(); } catch (IllegalArgumentException e) { Log.w(TAG, "Invalid device descriptor", e); } String configString = ""; try { //Parse the raw configuration descriptor configString = ConfigurationDescriptor.fromDeviceConnection(connection) .toString(); } catch (IllegalArgumentException e) { Log.w(TAG, "Invalid config descriptor", e); } catch (ParseException e) { Log.w(TAG, "Unable to parse config descriptor", e); } printResult(deviceString + "\n\n" + configString); connection.close(); }
static synchronized MtpDevice openMTP(final UsbDevice device) { if (device == null) { return null; } final UsbManager usbManager = (UsbManager) xdrip.getAppContext().getSystemService(Context.USB_SERVICE); if (usbManager == null) { Log.d(TAG, "usbmanager is null in openMTP"); return null; } final MtpDevice mtpDevice = new MtpDevice(device); final UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(device); try { if (!mtpDevice.open(usbDeviceConnection)) { return null; } } catch (Exception e) { JoH.static_toast_long("Exception opening USB: " + e); return null; } return mtpDevice; }
static synchronized MtpDevice openMTP(final UsbDevice device) { if (device == null) { return null; } final UsbManager usbManager = (UsbManager) xdrip.getAppContext().getSystemService(Context.USB_SERVICE); if (usbManager == null) { Log.d(TAG, "usbmanager is null in openMTP"); return null; } final MtpDevice mtpDevice = new MtpDevice(device); final UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(device); try { if (!mtpDevice.open(usbDeviceConnection)) { return null; } } catch (Exception e) { JoH.static_toast_long("Exception opening USB: " + e); return null; } return mtpDevice; }
UsbDeviceConnection connection = usbManager.openDevice(driver.getDevice()); if (connection == null) { if (usbDevice == null) {
UsbDeviceConnection connection = mUsbManager.openDevice(driver.getDevice()); if (connection == null) {
UsbDeviceConnection connection = mUsbManager.openDevice(driver.getDevice()); if (connection != null) { mSerialDevice = driver;
UsbDeviceConnection connection = mUsbManager.openDevice(driver.getDevice()); if (connection != null) { mSerialDevice = driver;
usbConnection = usbManager.openDevice(usbDevice);