Identify a new device in java - java

I want to know when a new device is connected.
I learned that it is possible to get all the current devices in C# using:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select VolumeName, DeviceID from Win32_LogicalDisk Where Not (VolumeName is null)");
and then searcher.get() returns a collection of the devices.
Is there something parallel to ManagementObjectSearcher that i can use in java?
If not, is there another way to get the list of connected devices? or another way to find out when a new device is being connected?
Thanks.

I know of no real cross platform way to do this in Java. If you're happy with a Windows only solution, the simplest solution would be to generate a C# / VBScript executable you can call from your Java program with Runtime.exec().
If you want a "nicer" solution that takes a bit more work (but still only Windows) you could use iKVM to interface between your C# and Java programs.
The only thing I can think of from a cross platform perspective (and this still uses native libraries is to use RXTX to poll an enumerate on the COM ports, and check when something changes. This won't get you a list of all the devices, and it will only work for devices that use a COM port, but depending on your needs it may be enough. The plus side with this approach is that RXTX binaries freely exist for every common platform out there.

Related

Trying to communicate to a USB port/Device in Java on Mac

I have a basic understanding of network programming but i have never had anything to do with USB ports.
This is what i am trying to achieve
I need to write a program in Java, to communicate to a device which is connected to USB port and then later send commands.
I tried terminal as follows earlier but it didn't quite work
ls -l /dev/tty.*
screen /dev/tty.usbserrial-FTYRDSX7 9600
The above didn't work out.
So if someone can just guide with what Java classes i should be looking into and if there are any APIs that should help me get started
Unless you're required to build it from the ground up, use a library such as usb4java (also here). Trying to manage it as a block device and interpreting whatever protocols etc., etc is not worth the effort unless there something very unique to your problem.
So i ended up using jssc which is much easier to use with strait forward syntax and methods

How to access information in Windows Device Manager from Java?

I have a serial to USB device and more than one of those can be connected to the computer. I need to query and retrieve a list of COM ports that the devices are connected to. In Windows Device Manager you can get the COM port + friendly name of devices that are connected at the present time. This list is dynamic.
Reading from the registry did not work because the information stored is stale and static, not dynamic.
Devcon (from Microsoft) does list the ports that devices are connected to, but it cannot be used in my app because it is not re-distributable.
Any ideas or preferably, a solution?
I had to solve a similar issue just some weeks ago. I came accross the Jawin-Project that provided everything you need to use WMI-Queries. It is already quite old but works like a charm if you follow the documentation. A nice German walkthrough can be found codegods blog.
For me, it did not solve all problems (I had some very specific things to find out about the target device), I finally created an own JNI (Java Native Interface) Class and DLL in C incorporating the windows API.
I hope this is what you searched for.
Did you try this jUSB API or This example ?
I hope this will help you.
Use JNA (https://github.com/twall/jna) and take a look at the following page. It might be a useful point to start from.
http://www.digitalinferno.com/wiki/Wiki.jsp?page=JNAPrintDevices
I am looking at Java Native Access as it provides access to the windows API from pure Java code - no JNI to deal with. I was concerned about the LGPL license; whether it can be included in a commercial product but from what a few posts in the newsgroup say, it can be.
After I try it out in the next few weeks, I shall post back here.

possible to import javax.comm on Android? want to open serial device

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.
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.
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.

How do you interface with a USB to Parallel adapter?

I'm currently doing a project where I have to interact with a circuit I made through the parallel port of a computer. However, my computer doesn't have a parallel port so I borrowed a Parallel to USB adapter cable. The cable didn't come with any drivers, but it's recognized by the device manager as a "USB Printing Support" controller, under the USB section.
It seems that old parallel printers can be plugged in and work properly without any problems. So my question is, if I write a program in Java that tries to interact with a parallel port directly, will it work? And if not, can anyone give me some pointers as to what I need to do to interact with it?
Thanks.
I think you should head toward javax.comm library here.. there is also a different version that is supposed to work better, called librxtx.. take a look here (it's a pluggable replacement for javax.comm)..
I used both of them for an embedded device and they worked great, they manage serial and parallel port.. maybe also usb in your case.
I can't speak for parallel or Java but I've done something similar with serial-via-USB and C#. In that case it was exactly the same as a native controller. YMMV.
As for testing things: get an old dot-matrix printer (and put it in hex dump mode if you really want the nitty-gritty).
If you really want drivers for the thing, find a utility (I think the windows device manager can do it) that gives you the vendor ID and product ID numbers and from those you can look up all kinds of fun stuff (many Linux distributions have a plain text file that maps the numbers to the name of the manufacturer and what not) that plus Google should give you a driver installer.
You need java parallel port drivers which I haven't found for free. You'll have to pay for the driver for Windows.
I think there might be some free drivers if you use Linux.
USB "parallel port" adapters and cables generally aren't. They contain chips that emulate USB printers and send the print data out the parallel port like it might be sent to a similar printer using a parallel (printer) port.
Unless the device you have is actually a printer, there are probably very few (if any) adapters that will work.
There are ways of attaching GPIO "parallel"/bus pins into USB including certain FTDI chips, UARTs and various microcontrollers. If you can write software to use one of these, it could let you drive arbitrary circuits the way olde PC parallel ports were (not through the same MMIO, though).

Access USB with Java, in order to find thumbdrive manufacturer's serial#/unique-ID

Looking for a way to read the unique ID / serial# of a USB thumb drive;
please note that
- I am looking for the value of the manufacturer, not the one Windows allocates for it.
- I need to support multiple OS (Windows, Unix, Mac), thus needs to be a Java solution
The idea is to be able to distinguish between different USB thumb drives.
RXTX is the way to go. In the world of model trains, JMRI (Java Model Railroad Interface) has become very popular. JMRI runs on all platforms (Windows, Linux and Mac) and communicates with a variety of USB based devices (command stations). RXTX is in fact used by JMRI.
You might give a look at the following projects:
javax-usb and jusb. They seem to support Linux and Windows.
Anyway, since USB access in Java requires the use of native libraries, you might not achieve the required portability.
I've never tried using it (it's been on my todo list for a good few months now), but there is the "marge" project on java.net:
http://marge.java.net/
This should let you connect to bluetooth devices (although I don't think it is 100% feature complete, there is demo code on there), and then the ClientDevice class has a "getBluetoothAddress" method which I believe should be unique to that device
http://marge.java.net/javadoc/v06/marge-core/net/java/dev/marge/entity/ClientDevice.html
As I say though, I've never tried it...
I have never investigated this thoroughly, but from memory the RXTX library implementation of the javax.comm packages are supposedly very good and now have USB support.

Categories