Java中的Wifi信息

如何获取Java附近的wifi路由器的MAC地址SSID信号强度 ? 最好是系统独立的,如果不是那么Windows。

我真的不认为有任何系统独立的方式来从Java知道。

在Windows上,您应该能够使用无线LAN API执行此操作,但您很可能需要一些JNI才能访问它们。

要获取Mac地址,您需要查询ARP缓存。 这不是微不足道的,也不依赖于系统。

更多信息: 查询ARP缓存以获取MAC ID

我知道这是一个非常古老的问题,但你可以在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 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); } }