I use usb-serial-for-android project to communicate with a device pluged in USB I can detect the device, open the port and send data BUT data received is always the data I just sent before
I Also update the manifest to detect when the usb device is pluged or unpluged and run the application to manage it.
There is the code I use. Am I missing something?
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);
}
To read the data I have another method below
@Override
public void onNewData(byte[] data) {
runOnUiThread(() -> { infos.append(byteArrayToHexString(data)); });
}
Finaly I succed to read data with my usb device I was pretty dumb with my textview too small, I didn't realize in the answer I had the echo AND the response I needed
I Just use somme String functions to delete echo and it's all OK now. Sorry for the inconvenience