iam just simply trying to know what are the input devices and output devices connected to the system. Do there any api to get information about input/output devices using java?
EDIT
independent of any operating system.
Please suggest.
To find what usb devices are connected to the system you can use jUSB. This article has more in-depth information on how to use the api. In particular, to find all usb devices (slightly modified from the article):
Host host = HostFactory.getHost();
// Obtain a list of the USB buses available on the Host.
Bus[] bus = host.getBusses();
// Traverse through all the USB buses.
for (int i = 0; i < bus.length; i++) {
// Access the root hub on the USB bus and obtain the number of USB ports available on the root hub.
Device root = bus[i].getRootHub();
int totalPorts = root.getNumPorts();
// Traverse through all the USB ports available on the
// root hub. It should be mentioned that the numbering
// starts from 1, not 0.
for (int j=1; j<=total_port; j++) {
// Obtain the Device connected to the port.
Device device = root.getChild(j);
if (device != null) {
// USB device available, do something here.
}
}
}
Similarly, you can use the api for MIDI systems to find what MIDI devices are connected, see the java tutorials for more information.
I think you should try commandline via java Runtime.getRuntime().exec(command);
According to this site http://michaelminn.com/linux/command_line
Following command should return this:
lspci: Lists information about devices connected to the internal PCI busses.
lspci -vv: Full dump of information.
so
String command="lspci";
And from windows you may need to download DevCon command! http://support.microsoft.com/kb/311272/EN-US
so
String command="devcon find";
Related
I am trying to program an android app dashboard(latest Java android 5.1 lollipop) that would be able to control my govee lamp which has a LAN control api which connects to a multicast address on my LAN(239.255.255.250) but I need a way to find the IP of the specific lamp so that I can send it commands after user interaction on my app.
Pinged the multicast IP and got no response(operation timed out). Looked around google for a way but couldn't find anything so wondering if someone here could help me out. TY for your time :)
Assuming you know the mac address you can send a reverse ARP packet (Reverse Address Resolution Protocol) to obtain the IP address. ARP itself is normally used to map an IP address to the mac address so the local router can properly forward the packet. RARP does the opposite.
Another way is to interrogate the local ARP cache from within the program on any attached device and see if that has it. So on a windows OS within the program you can initiate an arp -a in the console window to see the current arp cache. The only caveat is that the particular value may have time out and been removed.
Here is an example of the latter.
String mac = "1c-7e-81-9e-48-e8";
try {
Runtime rt = Runtime.getRuntime();
// send broadcast address
rt.exec(new String[]{
"ping","-n 1 -w 200 192.255.255.255"
});
// interrogate arp cache
Process p = rt.exec(new String[]{
"cmd.exe","/c","arp","-a"
});
BufferedReader in = new BufferedReader(
new java.io.InputStreamReader(p.getInputStream()));
String line1;
while ((line1 = in.readLine()) != null) {
if (line1.contains(mac)) {
System.out.println(line1);
}
}
} catch (Exception e) {
e.printStackTrace();
}
prints something like
192.168.1.107 1c-7e-81-9e-48-e8 dynamic
The above may need to be adjusted to fulfill your exact requirements.
Of course the easiest way might be to force the issue and assign a static address of your choosing.
I've created a Java GUI which lists all serial ports in a drop down menu from which the user selects the correct port and clicks connect. Connection to an Arduino is then established and the user is able to perform some actions. I get the available ports using Fazecast JSerialComm:
SerialPort[] ports = SerialPort.getCommPorts();
I grab the ports and put the results into the drop down. This works flawlessly BUT only when the Arduino is plugged into the Mac BEFORE launching my Java GUI. Is there a way to detect a hotplugged device in Java? I already thought of getting the com ports periodically (every second or so) but to me that does seem to be a very elegant solution.
update:
I found this answered by another user:
Serial communication manager have APIs to find serial port names like COMxx dynamically. Just connect your USB-USRT IC and SCM library will tell you the device node for it. Just google for Serial communication manager. It is hosted on GitHub.
src
You might do this by checking if the port is opened or not after trying to open it using:
SerialPort[] serialPorts = SerialPort.getCommPorts();
SerialPort liveSerialPort = null;
for (SerialPort p: serialPorts) {
p.openPort();
if (p.isOpen()) {
liveSerialPort = p;
System.out.println("HERE opened port = " + liveSerialPort.getSystemPortName());
break;
}
}
I am trying to develop an open-source network monitoring application
The application scan the network to find all connected devices. It shows all IPs in the given range but the connected devices will appear with checked box (JCheckBox). I list the connected devices in a JTable with information about each device such as name or IP address using address.getHostName() and address.getHostAddress() -Methods in the class InetAddress-
Now I am trying to get the physical address (MAC). I tried this code but the application shows the local device only; the devices (connected or disconnected) in the given IPs range did not appear.
byte[] mac = NetworkInterface.getByInetAddress(address).getHardwareAddress();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
String MAC = sb.toString();
To request MAC (Hardware) address you need to make ARP lookup.
Answer to appropriate question may help you.
There are several ways defined there:
arp -a lookup (UNIX-specific way)
SNMP request
Send your own ARP request packet
Anyway you can get MAC address that belongs only to devices within your local network.
I'm trying to write a program that will show a DHCP client list in Java. I want to get the IP addresses, MAC addresses and the host names of all the devices connected to my wifi network.
I have a Belkin router. Its homepage has a 'DHCP client list' option which when clicked shows me this table :
That's exactly what I'm looking for. But I want to show all this data in the form of a list in a Java Swing program. I also want to be able to update this list by pressing a refresh button. Is there any way to achieve this?
It should look something like this :
I've written a basic java program that shows all the IP addresses that are online. Here's the code :
public static void main(String[] args) throws IOException {
InetAddress localhost = InetAddress.getLocalHost();
// this code assumes IPv4 is used
byte[] ip = localhost.getAddress();
for (int i = 1; i <= 254; i++)
{
ip[3] = (byte)i;
InetAddress address = InetAddress.getByAddress(ip);
if (address.isReachable(1000))
{
// machine is turned on and can be pinged
System.out.println(address + "is online");
}
else if (!address.getHostAddress().equals(address.getHostName()))
{
// machine is known in a DNS lookup
System.out.println(address + "is in a DNS lookup");
}
else
{
// the host address and host name are equal, meaning the host name could not be resolved
System.out.println(address + " is not online");
}
}
}
But this doesn't serve the purpose and it's really slow. I want to write a Swing program that'll show me the DHCP client list as seen in the image above.
Any help is appreciated.
I would think about three possible alternatives:
1-The one you implemented that is slow but it can work. You need to find a JAVA API to get the MAC addresses of received messages (I don't know if it exists or not). You can also send ARP messages asking "who has this IP address) and obtain the MAC address from the response. Use some Java interface for pcap library: jNetPcap vs Jpcap , http://jnetpcap.com/
2-Create an app that accesses your router web interface using HTTP and sending the appropriate messages with data as if you were using the UI. In this way you can programatically follow the steps a human would go and get the list that you browser shows, parse it and obtain the data.
3-If the router/access point provides a web API, which I doubt, you can use it.
Is there any way I can get any unique identifier for a particular wifi router?
I'm trying to write an Android app that needs to know which router it is connected to. I know that android provides a way to get the BSSID of the currently connected network, but to my surprise, this is not unique.
I found out that on dual band routers, two different devices could be getting two different BSSID, even if they are on the same SSID.
Is there any other parameter I can fetch that can uniquely identify the wifi I am connected to? I would love to try some third party library, if that would allow me, since I am quite certain Android does not come packaged with a better method than giving me the BSSID.
Edit: I'm trying to find out who all have set their home wifi (via the app) as the same Wi-Fi as me. Each user sets their 'home Wi-Fi' which gets saved on the server ( the mac address is what I'm saving). Then each user can query who all are on their Wi-Fi, and if they are currently connected on that Wi-Fi or not. The query of 'who is on my wifi' is done by searching for the same MAC address as the one I'm connected to. This fails if my home has a dual band, since they could be connected to the second frequency (and thus second MAC).
You are correct in assuming that getBSSID() will return two different addresses for the two different bands, as they are essentially two different access points, one 2.4GHz and one 5 GHz, simply wrapped up in the same package. I wrote a quick app that gets and displays all of the available fields that may be obtained using the WifiInfo class. When I connected to the 2.4 GHz band of my access point the details were as follows:
Note that you can tell you are connected to the 2.4 GHz band ("Frequency: 2452"). Repeating the process with the 5 GHz band shows the following:
As you can see, the MAC addresses for the two different bands differ by only one number; I do not know if this is the case for all routers. If this is indeed the case, then you can conclusively determine which access point you are connected to by analyzing a combination of the MAC address (BSSID) and the frequency.
Sadly, there is no guaranteed method of uniquely identifying a specific access point. You can come close by using the SSID, MAC, etc. but it wouldn't necessarily work every time and it would still be possible for someone to spoof this if they desired.
If you wish to consider different WiFi interfaces(1) (as opposed to the physical device supporting it) then the MAC address is, by definition, what you need (barring broken devices that wrongly re-use a MAC).
(1) I use the word "interfaces" because it's possible to support multiple networks (i.e. SSIDs) on the same interface. An interface will effectively be a single channel at a single frequency.
Just use MAC address of the AP. (MAC Address is unique)
Here is how:
Can I find the MAC address of my Access point in Android?
For unique identifier, you can use
BSSI+Frequency
Example:-
AA:AA:AA:AA:AA:AA+2432 and AA:AA:AA:AA:AA:AA+5230 are unique in dual band case
You can find the Mac address of your router and use that as your basis for uniquely identifying it.
You can do so by checking your Android devices ARP table and compare the router IP that you are connected to. An example can be shown here:
http://www.flattermann.net/2011/02/android-howto-find-the-hardware-mac-address-of-a-remote-host/
Relevant code extracted:
/**
* Try to extract a hardware MAC address from a given IP address using the
* ARP cache (/proc/net/arp).<br>
* <br>
* We assume that the file has this structure:<br>
* <br>
* IP address HW type Flags HW address Mask Device
* 192.168.18.11 0x1 0x2 00:04:20:06:55:1a * eth0
* 192.168.18.36 0x1 0x2 00:22:43:ab:2a:5b * eth0
*
* #param ip
* #return the MAC from the ARP cache
*/
public static String getMacFromArpCache(String ip) {
if (ip == null)
return null;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4 && ip.equals(splitted[0])) {
// Basic sanity check
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
return mac;
} else {
return null;
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}