我使用 usb-serial-for-android 项目 与插入 USB 的设备进行通信 我可以检测到设备,打开端口并发送数据,但收到的数据始终是我之前发送的数据
我还更新了清单以检测 USB 设备何时插入或拔出,并运行应用程序来管理它。
这是我使用的代码。我是不是错过了什么?
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
UsbSerialProber usbDefaultProber = UsbSerialProber.getDefaultProber();
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
if (deviceList.isEmpty()) {
Toast.makeText(this, "Aucun phériphérique USB detecté", Toast.LENGTH_LONG).show();
infos.setText("Aucun phériphérique USB detecté");
}
for (UsbDevice device : deviceList.values()) {
Toast.makeText(this, "Device detecté =" + device.getProductName(), Toast.LENGTH_LONG).show();
Toast.makeText(this, "Device Vendor =" + device.getVendorId() + " Product Id = " + device.getProductId(), Toast.LENGTH_LONG).show();
UsbSerialDriver driver = usbDefaultProber.probeDevice(device);
if (driver == null) {
ProbeTable customTable = new ProbeTable();
customTable.addProduct(device.getVendorId(), device.getProductId(), Cp21xxSerialDriver.class);
UsbSerialProber customProber = new UsbSerialProber(customTable);
driver = customProber.probeDevice(device);
}
if (driver != null) {
Context c = getApplicationContext();
PendingIntent pendingIntent;
Intent notificationIntent = new Intent();
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(c,
0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(c,
0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
while (!manager.hasPermission(device)) {
manager.requestPermission(device, pendingIntent);
}
UsbDeviceConnection connection = manager.openDevice(device);
if (connection == null) {
Toast.makeText(this, "Connexion impossible", Toast.LENGTH_LONG).show();
return;
}
infos.setText("Envoi en cours...");
Thread.sleep(1000);
Toast.makeText(this, "Envoi en cours...", Toast.LENGTH_LONG).show();
UsbSerialPort port = driver.getPorts().get(0); // Most devices have just one port (port 0)
port.open(connection);
port.setParameters(1200, 8, UsbSerialPort.STOPBITS_2, UsbSerialPort.PARITY_NONE);
request = new byte[port.getReadEndpoint().getMaxPacketSize()];
request[0] = hexIntToByte(0x80);
request[1] = hexIntToByte(0x3F);
request[2] = hexIntToByte(0x01);
byte crcMSByte = hexIntToByte(0x05);
byte crcLSByte = hexIntToByte(0x8A);
request[3] = crcMSByte;//CRC MSByte
request[4] = crcLSByte;//CRC LSByte
request[5] = hexIntToByte(0x0D);//Stop Byte
SerialInputOutputManager usbIoManager = new SerialInputOutputManager(port, this);
usbIoManager.start();
port.write(request, 0);
String infosText;
infosText = "Informations \n";
infosText += "Data envoyée = " + byteArrayToHexString(request) + "\n";
infos.setText(infosText);
} else {
Toast.makeText(this, "AUCUN Driver disponible", Toast.LENGTH_LONG).show();
}
}
spinner.setVisibility(View.GONE);
} catch (Exception e) {
String erreur = e.toString();
Toast.makeText(this, "Erreur = " + erreur, Toast.LENGTH_LONG).show();
String erreurText;
erreurText = "ERREUR \n";
erreurText += "Data envoyée = " + byteArrayToHexString(request) + "\n";
erreurText += "Erreur Message = " + erreur + "\n";
infos.setText(erreurText);
spinner.setVisibility(View.GONE);
}
要读取数据,我有下面另一种方法
@Override
public void onNewData(byte[] data) {
runOnUiThread(() -> { infos.append(byteArrayToHexString(data)); });
}
终于成功用我的USB设备读取数据了 我很愚蠢,我的文本视图太小,我没有意识到在答案中我有回声和我需要的响应
我只是使用一些字符串函数来删除回声,现在一切都OK了。给您带来不便敬请谅解