wifi 熱點是android 常用的一種功能,那么如何獲取連接熱點的設備數量?
網上查閱了一下資料,基本都是通過/proc/net/arp 設備文件去獲取相關信息,包括ip mac 以及連接狀態。
文件內容如下:
IP address HW type Flags HW address Mask Device
192.168.43.73 0x1 0x2 64:a6:51:74:23:f7 * wlan0
顯而易見,內容包含IP 和mac, 另外Flags 表示連接狀態。當然有一些設備,沒有更新改flags,
那就很抱歉,該方法失效。
解析該文件代碼如下:
String MacAddr = null;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("/proc/net/arp"));
String line = reader.readLine();
//讀取第一行信息,就是IP address HW type Flags HW address Mask Device
while ((line = reader.readLine()) != null) {
String[] tokens = line.split("[ ]+");
if (tokens.length < 6) {
continue;
}
String ip = tokens[0]; //ip
String mac = tokens[3]; //mac 地址
String flag = tokens[2];//表示連接狀態
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
} finally {
try {
if (reader != null) {
reader.close();
}
}
catch (IOException e) {
}
}