android ip地址扫描,如何准确扫描Android中连接到wifi的所有设备的IP和Mac地址?

小编典典

我找到了解决问题的方法,大多数设备不在系统arp表中,因此您需要在第一次ping通每个设备,一旦ping该设备将存储在系统ARP表中,该表存储在

(/proc/net/arp)

使用ip ping所有设备:(首先,您需要找到设备的IP地址,然后才能确定子网掩码,然后可以从(0-255)开始固定

码:

public void startPingService(Context context)

{

List deviceInfoList = new ArrayList();

try {

WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

WifiInfo mWifiInfo = mWifiManager.getConnectionInfo();

String subnet = getSubnetAddress(mWifiManager.getDhcpInfo().gateway);

for (int i=1;i<255;i++){

String host = subnet + "." + i;

if (InetAddress.getByName(host).isReachable(timeout)){

String strMacAddress = getMacAddressFromIP(host);

Log.w("DeviceDiscovery", "Reachable Host: " + String.valueOf(host) +" and Mac : "+strMacAddress+" is reachable!");

LocalDeviceInfo localDeviceInfo = new LocalDeviceInfo(host,strMacAddress);

deviceInfoList.add(localDeviceInfo);

}

else

{

Log.e("DeviceDiscovery", "❌ Not Reachable Host: " + String.valueOf(host));

}

}

}

catch(Exception e){

//System.out.println(e);

}

}

private String getSubnetAddress(int address)

{

String ipString = String.format(

"%d.%d.%d",

(address & 0xff),

(address >> 8 & 0xff),

(address >> 16 & 0xff));

return ipString;

}

从ARP缓存表中获取Mac地址

public String getMacAddressFromIP(@NonNull String ipFinding)

{

Log.i("IPScanning","Scan was started!");

List antarDevicesInfos = new ArrayList<>();

BufferedReader bufferedReader = null;

try {

bufferedReader = new BufferedReader(new FileReader("/proc/net/arp"));

String line;

while ((line = bufferedReader.readLine()) != null) {

String[] splitted = line.split(" +");

if (splitted != null && splitted.length >= 4) {

String ip = splitted[0];

String mac = splitted[3];

if (mac.matches("..:..:..:..:..:..")) {

if (ip.equalsIgnoreCase(ipFinding))

{

return mac;

}

}

}

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally{

try {

bufferedReader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return "00:00:00:00";

}

您还需要以下权限:

2020-11-16

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值