我与UsbDeviceConnection的连接有问题。我不能使用serialPort.open吗?当我删除UsbEndpoint和UsbInterface,connection.claimInterface和bulkTransfer时,我进入我的应用程序时,如果没有崩溃应用程序,则无法打开端口
但是如果我在这里使用代码,则会得到:E / UsbManager:UsbManager.openDevice中的异常 java.lang.SecurityException:用户未授予设备UsbDevice的权限...
BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
// UsbDeviceConnection connection = usbManager.openDevice(device);
// handleIntent(getIntent());
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
UsbDeviceConnection connection = usbManager.openDevice(device);
UsbInterface intf = device.getInterface(0);
UsbEndpoint endpoint = intf.getEndpoint(0);
connection.claimInterface(intf, true);
connection.bulkTransfer(endpoint, "test".getBytes(), "test".length(), 0);
if(device != null) {
// printSomething();
UsbSerialDevice serialPort = UsbSerialDevice.createUsbSerialDevice(device, connection);
boolean granted = intent.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED);
if(granted) {
serialPort.open();
showCommand.setText("serial port " + serialPort.syncOpen());
}
else{
showCommand.setText("Can't open port" + endpoint.getType());
}
}
else{
// showCommand.setText("Test");
printSomething();
}
}
}
};