int num = WifiApDeviceUtil.getClientList(0,true,500);
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import com.adayo.app.autotest.bean.DnsmasqBean;
import com.adayo.app.autotest.bean.HotspotBean;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import static com.adayo.app.autotest.Constant.LOG_TAG;
public class WifiApDeviceUtil {
private static final String TAG = "dxfWifiApDeviceUtil";
private final Context context;
public WifiApDeviceUtil(Context context) {
this.context = context;
}
private static List<DnsmasqBean> getDeviceList(){
BufferedReader reader = null;
List<DnsmasqBean> deviceList = new ArrayList<>();
try {
reader = new BufferedReader(new FileReader("/data/misc/dhcp/dnsmasq.leases"));
String line;
while ((line = reader.readLine()) != null) {
String[] tokens = line.split("[ ]+");
if (tokens.length < 5) {
continue;
}
String mac = tokens[1];
String deviceName = tokens[3];
Log.d(LOG_TAG, "getDeviceList: mac = "+mac+" deviceName = "+deviceName);
if (!TextUtils.isEmpty(deviceName)){
DnsmasqBean dnsmasqBean = new DnsmasqBean();
dnsmasqBean.setMac(mac);
dnsmasqBean.setDeviceName(deviceName);
deviceList.add(dnsmasqBean);
}
}
} catch (FileNotFoundException e) {
Log.e(LOG_TAG, "Could not open /data/misc/dhcp/dnsmasq.leases to lookup mac address");
Log.e(LOG_TAG, "FileNotFoundException----" + e );
} catch (IOException e) {
Log.e(LOG_TAG, "Could not read /data/misc/dhcp/dnsmasq.leases to lookup mac address");
Log.e(LOG_TAG, "IOException----" + e );
} finally {
try {
if (reader != null) {
reader.close();
}
}
catch (IOException e) {
}
}
return deviceList;
}
public static int getClientList(int clientNum, final boolean onlyReachables, final int reachableTimeout) {
BufferedReader br = null;
ArrayList<HotspotBean> result = new ArrayList<HotspotBean>();
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
Log.d(LOG_TAG, "br = " + br);
String line;
Thread.sleep(1000);
while ((line = br.readLine()) != null) {
Log.d(LOG_TAG, "line = " + line);
String[] tokens = line.split("[ ]+");
if (tokens.length < 6) {
continue;
}
String mac = tokens[3];
String device = tokens[5];
String ip = tokens[0];
if (mac.matches("..:..:..:..:..:..")) {
boolean isReachable = true;
Log.d(LOG_TAG, "isReachable = " + isReachable);
Log.d(LOG_TAG, "HWAddr = " + tokens[3]);
if (!onlyReachables || (isReachable && ("wlan0".equals(device)
|| "wlan1".equals(device))
&& ip.contains("192.168.43")
&& !mac.contains("00:00:00"))) {
Log.d(LOG_TAG, " add List---HWAddr" + mac);
String deviceName = mac;
List<DnsmasqBean> deviceNameList = getDeviceList();
for (DnsmasqBean dnsmasqBean : deviceNameList) {
if (dnsmasqBean.getMac().equals(mac)){
deviceName = dnsmasqBean.getDeviceName();
break;
}
}
result.add(new HotspotBean(ip, mac,device, deviceName,isReachable));
}
}
}
if(clientNum != result.size()){
Log.d("WifiApManager", "clientNum != result.size()");
final ArrayList<HotspotBean> newResult = new ArrayList<HotspotBean>();
for(HotspotBean hotspotBean : result){
boolean isReachable = InetAddress.getByName(hotspotBean.getmIpAddress()).isReachable(reachableTimeout);
Log.d("WifiApManager", "isReachable = " + isReachable);
Log.d("WifiApManager", "HWAddr = " + hotspotBean.getmMacAddress());
if(isReachable){
newResult.add(hotspotBean);
}
}
result = newResult;
}
} catch (Exception e) {
} finally {
try {
br.close();
} catch (IOException e) {
}
}
return result.size();
}
}
public class DnsmasqBean {
private String mac;
private String deviceName;
public String getMac() {
return mac;
}
public void setMac(String mac) {
this.mac = mac;
}
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
}
package com.adayo.app.autotest.bean;
public class HotspotBean {
private String mIpAddress;
private String mMacAddress;
private String mDevice;
private String mHostName;
private boolean mIsReachable;
public HotspotBean(String ipAddr, String hWAddr, String device,String deviceName ,boolean isReachable) {
super();
this.mIpAddress = ipAddr;
this.mMacAddress = hWAddr;
this.mDevice = device;
this.mHostName = deviceName;
this.mIsReachable = isReachable;
}
public String getmIpAddress() {
return mIpAddress;
}
public void setmIpAddress(String mIpAddress) {
this.mIpAddress = mIpAddress;
}
public String getmMacAddress() {
return mMacAddress;
}
public void setmMacAddress(String mMacAddress) {
this.mMacAddress = mMacAddress;
}
public String getmDevice() {
return mDevice;
}
public void setmDevice(String mDevice) {
this.mDevice = mDevice;
}
public String getmHostName() {
return mHostName;
}
public void setmHostName(String mHostName) {
this.mHostName = mHostName;
}
public boolean ismIsReachable() {
return mIsReachable;
}
public void setmIsReachable(boolean mIsReachable) {
this.mIsReachable = mIsReachable;
}
}