private void connect2Usb() {
Logger.t(TAG).d("connect2Usb : connect");
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> usbDevices = mUsbManager.getDeviceList();
Set<Map.Entry<String, UsbDevice>> entrySet = usbDevices.entrySet();
UsbDevice usbDevice= null;
for (Map.Entry<String, UsbDevice> entry : entrySet) {
Logger.t(TAG).d("connect2Usb : " + entry.getKey() + "," + entry.getValue());
if (entry.getValue().getVendorId() == USB_VENDORID) {
usbDevice= entry.getValue();
break;
}
}
if (usbDevice== null) {
Logger.t(TAG).e("no usb devices");
return;
}
if (usbDevice.getInterfaceCount() <= 0) {
Logger.t(TAG).e("no usb devices interface ");
return;
}
mUsbInterface = usbDevice.getInterface(0);
Logger.t(TAG).d("usb interface : " + mUsbInterface);
for (int i = 0; i < mUsbInterface.getEndpointCount(); i++) {
UsbEndpoint endpoint = mUsbInterface.getEndpoint(i);
if (endpoint.getDirection() == UsbConstants.USB_DIR_IN) {
mUsbEndpointIn = endpoint;
if (mUsbEndpointIn.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
Logger.t(TAG).d("usb bulk transfer");
}
}
if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) {
mUsbEndpointOut = endpoint;
}
int direction = endpoint.getDirection();
Logger.d("direction " + direction);
}
boolean hasPermission = mUsbManager.hasPermission(usbDevice);
if (!hasPermission) {
Logger.t(TAG).e("no usb permission");
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 9898, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_ONE_SHOT);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
mUsbManager.requestPermission(usbDevice, mPermissionIntent);
return;
}
if (mUsbDeviceConnection == null) {
mUsbDeviceConnection = mUsbManager.openDevice(usbDevice);
}
if (mUsbDeviceConnection != null) {
boolean claimUsb = mUsbDeviceConnection.claimInterface(mUsbInterface, true);
if (claimUsb) {
Logger.t(TAG).d("claimed usbinterface");
}
}
}