1

I have a rooted Android device with a USB serial device connected. The device shows up as "/dev/ttyUSB5"

I would like to open the serial port and read/write from it. First and foremost... within Android, will it even allow me to read/open /dev/ttyUSB5? Or within the application will I be prohibited from touching /dev ? If I cannot touch anything in /dev, then this is all irrelevant.

However, if I can, then I am wondering if it is possible to import javax.comm so that I can communicate with the serial device.

As of right now, I am able to get around this by spawning a native C application which opens the serial port, then I use a socket to read/write to the serial port in Java. However, it would be much better to do it all in Java.

MPelletier
  • 16,256
  • 15
  • 86
  • 137
gnychis
  • 7,289
  • 18
  • 75
  • 113

2 Answers2

3

within Android, will it even allow me to read/open /dev/ttyUSB5? Or within the application will I be prohibited from touching /dev ?

I am unsure if an ordinary SDK application will have rights to this. An application running as root would, if you have a rooted device.

However, if I can, then I am wondering if it is possible to import javax.comm so that I can communicate with the serial device.

Not directly. The build tools don't want you importing java.* and javax.* classes. And, I don't know how quickly javax.comm drops down into native code, even on standard Java.

What you can try is see if Apache Harmony has an implementation of javax.comm, and take a look at it. If it seems like it will fit your needs, you can copy the relevant classes, then refactor them into a new package (e.g., org.gynchis.comm).

However, the best overall answer, if possible, is to try to stick with the ADK and the new USB classes added in API Level 10. I'm not a hardware guy, so I have no idea if what's in there will meet your needs, or if you have a device that is on a new enough Android version to have that support.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Yes you can use the /dev/tty* to access your serial port. Check this out : serialport-api. I tested it but of course, you need to have your /dev/tty* with the proper rights set. This is in the init.rc file (chmod 0666 /dev/ttyUSB5). If you are working in an embedded system, it's easy to fix. If you want to work on an "off the shelf any tablet on the market", it's more complicated. I believe the only way is to root it. The only drawback of that package is it seems to use a thread that constantly poll the inputstream which makes me believe it is not very CPU/battery efficient. I'm looking for a way to just get something that would wakeup my thread when data is available on the reception side.

Sylvain Huard
  • 1,378
  • 6
  • 18
  • 29