嗨,我想找到从Android连接到我的WIFI路由器的所有设备,我需要设备Mac地址和每个设备的本地IP地址(包括iOT设备),
现在,我试图从ARP缓存表中找到.但是在扫描的某个时候某些设备丢失了,它并不那么准确.
我的代码:
List<LocalDeviceInfo> devicesInfos = 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("..:..:..:..:..:..")) { LocalDeviceInfo thisNode = new LocalDeviceInfo(ip, mac); devicesInfos.add(thisNode); } } } } catch (fileNotFoundException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } finally{ try { bufferedReader.close(); } catch (IOException e) { e.printstacktrace(); } } // Print Description for (LocalDeviceInfo devicesInfo :devicesInfos) { System.out.print("✅"); System.out.println("IP : "+devicesInfo.getIp()); System.out.println("Mac : "+devicesInfo.getMacAddress()); }
How can i scan all devices (IP address and Mac address) in
androID accurately .
解决方法:
我找到了我的问题的解决方案,大多数设备都不在系统arp表中,所以你需要在第一次Ping每个设备,一旦你Ping该设备它将存储在系统ARP表中存储在(/ PROC /净/ ARP)
使用ip Ping所有设备:(首先需要找到设备的IP地址,而不是确定子网掩码,你可以从(0-255)开始学习
码:
public voID startPingService(Context context){ List<LocalDeviceInfo> deviceInfoList = new ArrayList<LocalDeviceInfo>(); 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<LocalDeviceInfo> 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";}
您也需要这些权限:
<uses-permission androID:name="androID.permission.INTERNET" /><uses-permission androID:name="androID.permission.ACCESS_WIFI_STATE" /><uses-permission androID:name="androID.permission.ACCESS_NETWORK_STATE" />
总结 以上是内存溢出为你收集整理的如何在android中准确扫描所有连接到wifi的设备的IP和Mac地址?全部内容,希望文章能够帮你解决如何在android中准确扫描所有连接到wifi的设备的IP和Mac地址?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)