Plugin for serial communication over USB

We have a project that supports USB. I was wanting to know if there is a plugin that would allow a user to plug in their phone/device (via USB) and allow serial communications with our device.

I did find this forked repository but did not know if there was anything else:

Hi, Did you find the solution?

I have not found a solution yet.

ok,
I wonder why there is no discussion or solution is there for it.

I agree. I would think it would be something commonly used.

1 Like

I’ve forked too that plugin, I’ve partially integrated into my app but I get couple of error from the plug-in.

  • One is related to the connection, which unfortunately make the whole app crash (at com.viewtrak.plugins.usbserial.UsbSerial.openSerial(UsbSerial.java:155)).
    This should be relatively simple to fix.

UsbSerial.java lines [150-157]

            for (UsbDevice v : usbManager.getDeviceList().values()) {
                
                if (v.getDeviceId() == settings.deviceId)
                    device = v; <---If no device has been found with device id.
            }

            if (device == null) {
                throw new Error("connection failed: device not found", new Throwable("connectionFailed:DeviceNotFound")); <--- Not handled properly cause crash.
            }
  • I’ve yet to find a solution to recover connect(able) device, but I think it’s related to the fact that the plugin only asks for formal permission just before the connect (UsbSerial.java lines [170-184]).
    But you can’t connect if you can’t select a proper deviceId so you always circle back to the start without an inch of progress…

**Breaking unhandled exception: **

Serious error executing plugin
    java.lang.reflect.InvocationTargetException
    	at java.lang.reflect.Method.invoke(Native Method)
    	at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138)
    	at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:774)
    	at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0)
    	at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8)
    	at android.os.Handler.handleCallback(Handler.java:883)
    	at android.os.Handler.dispatchMessage(Handler.java:100)
    	at android.os.Looper.loop(Looper.java:224)
    	at android.os.HandlerThread.run(HandlerThread.java:67)
    Caused by: java.lang.Error: connection failed: device not found
    	at com.viewtrak.plugins.usbserial.UsbSerial.openSerial(UsbSerial.java:155)
    	at com.viewtrak.plugins.usbserial.UsbSerialPlugin.openSerial(UsbSerialPlugin.java:64)
    	at java.lang.reflect.Method.invoke(Native Method) 
    	at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138) 
    	at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:774) 
    	at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0) 
    	at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8) 
    	at android.os.Handler.handleCallback(Handler.java:883) 
    	at android.os.Handler.dispatchMessage(Handler.java:100) 
    	at android.os.Looper.loop(Looper.java:224) 
    	at android.os.HandlerThread.run(HandlerThread.java:67) 
    Caused by: java.lang.Throwable: connectionFailed:DeviceNotFound
    	at com.viewtrak.plugins.usbserial.UsbSerial.openSerial(UsbSerial.java:155) 
    	at com.viewtrak.plugins.usbserial.UsbSerialPlugin.openSerial(UsbSerialPlugin.java:64) 
    	at java.lang.reflect.Method.invoke(Native Method) 
    	at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138) 
    	at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:774) 
    	at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0) 
    	at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8) 
    	at android.os.Handler.handleCallback(Handler.java:883) 
    	at android.os.Handler.dispatchMessage(Handler.java:100) 
    	at android.os.Looper.loop(Looper.java:224) 
    	at android.os.HandlerThread.run(HandlerThread.java:67) 
2023-09-14 16:00:16.046  3945-4024  AndroidRuntime          io.ionic E  FATAL EXCEPTION: 
	CapacitorPlugins
    Process: io.ionic.ismes, PID: 3945
    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    	at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:783)
    	at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0)
    	at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8)
    	at android.os.Handler.handleCallback(Handler.java:883)
    	at android.os.Handler.dispatchMessage(Handler.java:100)
    	at android.os.Looper.loop(Looper.java:224)
    	at android.os.HandlerThread.run(HandlerThread.java:67)
    Caused by: java.lang.reflect.InvocationTargetException
    	at java.lang.reflect.Method.invoke(Native Method)
    	at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138)
    	at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:774)
    	at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0) 
    	at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8) 
    	at android.os.Handler.handleCallback(Handler.java:883) 
    	at android.os.Handler.dispatchMessage(Handler.java:100) 
    	at android.os.Looper.loop(Looper.java:224) 
    	at android.os.HandlerThread.run(HandlerThread.java:67) 
    Caused by: java.lang.Error: connection failed: device not found
    	at com.viewtrak.plugins.usbserial.UsbSerial.openSerial(UsbSerial.java:155)
    	at com.viewtrak.plugins.usbserial.UsbSerialPlugin.openSerial(UsbSerialPlugin.java:64)
    	at java.lang.reflect.Method.invoke(Native Method) 
    	at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138) 
    	at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:774) 
    	at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0) 
    	at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8) 
    	at android.os.Handler.handleCallback(Handler.java:883) 
    	at android.os.Handler.dispatchMessage(Handler.java:100) 
    	at android.os.Looper.loop(Looper.java:224) 
    	at android.os.HandlerThread.run(HandlerThread.java:67) 
    Caused by: java.lang.Throwable: connectionFailed:DeviceNotFound
    	at com.viewtrak.plugins.usbserial.UsbSerial.openSerial(UsbSerial.java:155) 
    	at com.viewtrak.plugins.usbserial.UsbSerialPlugin.openSerial(UsbSerialPlugin.java:64) 
    	at java.lang.reflect.Method.invoke(Native Method) 
    	at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138) 
    	at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:774) 
    	at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0) 
    	at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8) 
    	at android.os.Handler.handleCallback(Handler.java:883) 
    	at android.os.Handler.dispatchMessage(Handler.java:100) 
    	at android.os.Looper.loop(Looper.java:224) 
    	at android.os.HandlerThread.run(HandlerThread.java:67) 

Also, the other alternative would be Cordovarduido… Which is even older and unmaintained that this plugin is!


It won’t work on Android 10+ due to the changes to the IntentFilter which now require flags as paramets.

What a mess of a framework is Ionic…
Do you guys manage to make it at least display the device list?
If so, would you mind sharing a snippet so I can adjust my code too.