Android:如何获取连接热点的设备数量?

//热点连接设备数查询
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"));

            // Skip over the line bearing colum titles
            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) {
                // Do nothing
            }
        }
        return deviceList;

    }

    /**
     * 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
     */
    public static int getClientList(int clientNum, final boolean onlyReachables, final int reachableTimeout) {
//        Runnable runnable = new Runnable() {
//            public void run() {

        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;
            // 延时1000毫秒,防止onNumClientsChanged回调过快br = new BufferedReader(new FileReader("/proc/net/arp"));获取不到wlan0的值
            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) {
//            Log.e(this.getClass().toString(), e.toString());
        } finally {
            try {
                br.close();
            } catch (IOException e) {
//                Log.e(this.getClass().toString(), e.getMessage());
            }
        }
//                ArrayList<HotspotBean> finalResult = result;
//                // Get a handler that can be used to post to the main thread
//                Handler mainHandler = new Handler(context.getMainLooper());
//                Runnable myRunnable = new Runnable() {
//                    @Override
//                    public void run() {
//                        Log.d(LOG_TAG, "result = " + finalResult.size());
//                        finishListener.onFinishScan(finalResult);
//                    }
//                };
//                mainHandler.post(myRunnable);
//            }
//        };

//        Thread mythread = new Thread(runnable);
//        mythread.start();
        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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值