代码如下:
public int SignatureWithExPinpad() {
String ACTION_USB_PERMISSION = "cn.wch.wchusbdriver.USB_PERMISSION";
// Find all available drivers from attached devices.
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
if (availableDrivers.isEmpty()) {
return -1;
}
// Open a connection to the right available driver.
int i;
UsbSerialDriver driver = availableDrivers.get(0);
for (i = 0; i < availableDrivers.size(); i++) {
driver = availableDrivers.get(i);
UsbDevice udevice = driver.getDevice();
if ((udevice.getVendorId() == UsbId.VID) && (udevice.getProductId() == UsbId.PID)) {
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);//PendingIntent.FLAG_UPDATE_CURRENT
manager.requestPermission(driver.getDevice(), mPermissionIntent);
SystemApi.Delay_Api(5000);
break;
}
}
final UsbDeviceConnection connection = manager.openDevice(driver.getDevice());
if (connection == null) {
return -1;
}
// Read some data! Most have just one port (port 0).
port = driver.getPorts().get(0);
try {
port.open(connection);
port.setParameters(38400, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
} catch (IOException e) {
Log.d("aabb", "get port");
connection.close();
e.printStackTrace();
return -1;
}
return 0;
}
CdcAcmSerialDriver 用的官方的源码,其内的mControlInterface.getEndpoint(0) 调用看起来也没有错,android.hardware.usb.UsbInterface.getEndpoint 是android sdk内的 看不到源码了,请指教。