ClientScanResult.java
public class ClientScanResult {
private String IpAddr;
private String HWAddr;
private String Device;
private boolean isReachable;
public ClientScanResult(String ipAddr, String hWAddr, String device, boolean isReachable) {
super();
IpAddr = ipAddr;
HWAddr = hWAddr;
Device = device;
this.setReachable(isReachable);
}
public String getIpAddr() {
return IpAddr;
}
public void setIpAddr(String ipAddr) {
IpAddr = ipAddr;
}
public String getHWAddr() {
return HWAddr;
}
public void setHWAddr(String hWAddr) {
HWAddr = hWAddr;
}
public String getDevice() {
return Device;
}
public void setDevice(String device) {
Device = device;
}
public void setReachable(boolean isReachable) {
this.isReachable = isReachable;
}
public boolean isReachable() {
return isReachable;
}
@Override
public String toString() {
return "ClientScanResult{" +
"IpAddr='" + IpAddr + '\'' +
", HWAddr='" + HWAddr + '\'' +
", Device='" + Device + '\'' +
", isReachable=" + isReachable +
'}';
}
}
WifiApManager.java
public class WifiApManager {
private final WifiManager mWifiManager;
public WifiApManager(Context context) {
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
}
/**
* Gets a list of the clients connected to the Hotspot, reachable timeout is 300
*
* @param onlyReachables {@code false} if the list should contain unreachable (probably disconnected) clients, {@code true} otherwise
* @return ArrayList of {@link ClientScanResult}
*/
public ArrayList<ClientScanResult> getClientList(boolean onlyReachables) {
return getClientList(onlyReachables, 300);
}
/**
* Gets a list of the clients connected to the Hotspot
*
* @param onlyReachables {@code false} if the list should contain unreachable (probably disconnected) clients, {@code true} otherwise
* @param reachableTimeout Reachable Timout in miliseconds
* @return ArrayList of {@link ClientScanResult}
*/
public ArrayList<ClientScanResult> getClientList(boolean onlyReachables, int reachableTimeout) {
BufferedReader br = null;
ArrayList<ClientScanResult> result = null;
try {
result = new ArrayList<ClientScanResult>();
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)) {
// Basic sanity check
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
boolean isReachable = InetAddress.getByName(splitted[0]).isReachable(reachableTimeout);
Log.e("WifiApManager", "getClientList: " + isReachable);
if (!onlyReachables || isReachable) {
result.add(new ClientScanResult(splitted[0], splitted[3], splitted[5], isReachable));
}
}
}
}
} catch (Exception e) {
Log.e(this.getClass().toString(), e.getMessage());
} finally {
try {
br.close();
} catch (IOException e) {
Log.e(this.getClass().toString(), e.getMessage());
}
}
return result;
}
}
网友评论