Reputation: 6585
How can I get the MAC address, SSID, and signal strength of wifi routers nearby in Java? Preferably system independent, if not then Windows.
Upvotes: 3
Views: 6046
Reputation: 2255
I know this is a very old question but you can use netsh commands in Windows to extract wifi information using java. Here is a blog post I made regarding that. https://notebookbft.wordpress.com/2015/11/25/wifi-information-extraction-java/
Java doesn’t allows us to directly get that low level information. Therefore I had run command line and run netsh commands inside java an extract that information. Following is the class I implemented for this. Please note that this only works on windows.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Rajind
*/
public class Extractor {
public static String NOT_SET = "NOT_SET";
public static boolean isEnabled(){
try {
String state;
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "netsh interface show interface \"Wi-Fi\"");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = r.readLine())!=null) {
//line = r.readLine();
if (line.contains("Administrative state")){
state = line.split("\\s+")[3];
//System.out.println(state);
state = state.toLowerCase();
if(state.equals("enabled")){
return true;
}else{
return false;
}
}
}
} catch (IOException ex) {
Logger.getLogger(Extractor.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}
public static boolean isConnected(){
try {
String state;
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "netsh interface show interface \"Wi-Fi\"");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = r.readLine())!=null) {
//line = r.readLine();
if (line.contains("Connect state")){
state = line.split("\\s+")[3];
// System.out.println(state);
state = state.toLowerCase();
if(state.equals("connected")){
return true;
}else{
return false;
}
}
}
} catch (IOException ex) {
Logger.getLogger(Extractor.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}
public static String getConnectedSSID(){
String ssid = NOT_SET;
try {
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "netsh wlan show interfaces");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = r.readLine())!=null) {
//line = r.readLine();
if (line.contains("SSID")){
ssid = line.split("\\s+")[3];
// System.out.println(ssid);
return ssid;
}
}
} catch (IOException ex) {
Logger.getLogger(Extractor.class.getName()).log(Level.SEVERE, null, ex);
}
return ssid;
}
public static String[] getListOfSSIDs(){
String [] ssid_List;
String ssid;
ArrayList<String> arr = new ArrayList<>();
try {
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "netsh wlan show networks");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = r.readLine())!=null) {
//line = r.readLine();
if (line.contains("SSID")){
ssid = line.split("\\s+")[3];
//System.out.println(ssid);
arr.add(ssid);
}
}
} catch (IOException ex) {
Logger.getLogger(Extractor.class.getName()).log(Level.SEVERE, null, ex);
}
ssid_List = new String[arr.size()];
arr.toArray(ssid_List);
return ssid_List;
}
public static String getIP(){
String ip = NOT_SET;
try {
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "netsh interface ip show addresses \"Wi-Fi\"");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = r.readLine())!=null) {
//line = r.readLine();
if (line.contains("IP Address")){
ip = line.split("\\s+")[3];
//System.out.println(ip);
return ip;
}
}
} catch (IOException ex) {
Logger.getLogger(Extractor.class.getName()).log(Level.SEVERE, null, ex);
}
return ip;
}
public static String getSubnetMask(){
String sb = NOT_SET;
try {
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "netsh interface ip show addresses \"Wi-Fi\"");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = r.readLine())!=null) {
//line = r.readLine();
if (line.contains("Subnet Prefix")){
sb = line.split("\\s+")[5];
sb = sb.substring(0, sb.length() - 1);
//System.out.println(sb);
return sb;
}
}
} catch (IOException ex) {
Logger.getLogger(Extractor.class.getName()).log(Level.SEVERE, null, ex);
}
return sb;
}
public static String getBroadcast(){
String subnetMask = getSubnetMask();
String ip = getIP();
String []arrSubnetMask = subnetMask.split("\\.");
String []arrIP = ip.split("\\.");
int []networkAddress = new int[4];
int [] broadcastAddress = new int[4];
String broadcast = "";
for(int i=0; i< 4; i++){
networkAddress[i] = Integer.parseInt(arrIP[i]) & Integer.parseInt(arrSubnetMask[i]);
//System.out.println(networkAddress[i]);
}
for(int i=0; i< 4; i++){
//broadcastAddress[i] = networkAddress[i] | (~Integer.parseInt(arrSubnetMask[i]) & 0xff);
//System.out.println(broadcastAddress[i]);
broadcast = broadcast + "." + (networkAddress[i] | (~Integer.parseInt(arrSubnetMask[i]) & 0xff));
}
// System.out.println(broadcast.substring(1));
//mask AND ip you get network address
//Invert Mask OR Network Address you get broadcast
return broadcast.substring(1);
}
}
Upvotes: 0
Reputation: 3224
I don't really think there's any system independent way to know that from Java.
On Windows you should be able to do that using Wireless LAN APIs, but you most probably need some JNI to have access to them.
Upvotes: 3
Reputation: 2524
To get the Mac address you need to query the ARP cache. Which is not trivial and system dependant.
More here: Query ARP cache to get MAC ID
Upvotes: 2