如何在Java中获取附近 wifi 路由器的MAC 地址、SSID和信号强度?最好是系统独立的,如果不是那么 Windows。
问问题
5348 次
3 回答
3
我真的不认为有任何独立于系统的方式可以从 Java 中了解这一点。
在 Windows 上,您应该能够使用无线 LAN API来做到这一点,但您很可能需要一些 JNI 才能访问它们。
于 2009-09-14T09:03:34.807 回答
2
要获取 Mac 地址,您需要查询 ARP 缓存。这不是微不足道的和系统相关的。
更多信息:查询 ARP 缓存以获取 MAC ID
于 2009-09-14T08:41:47.213 回答
0
我知道这是一个非常古老的问题,但您可以在 Windows 中使用 netsh 命令使用 java 提取 wifi 信息。这是我为此发表的一篇博文。https://notebookbft.wordpress.com/2015/11/25/wifi-information-extraction-java/
Java 不允许我们直接获取低级信息。因此,我在 java 中运行命令行并运行 netsh 命令以提取该信息。以下是我为此实现的类。请注意,这仅适用于 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);
}
}
于 2016-05-06T18:33:51.557 回答