I’m making an app to scan the IP and MAC of all the devices connected to the network, but I can only get their IP, and when I get the IPs it doesn’t do it well, it only gets from cell phones but not from laptops or PCs that are connected by cable to the router.
I tried to do it with arp -a but this no longer works on Android 13.
I see that some applications such as
Network Scanner and Wi-FI Monitor can get the IP and MAC
This is my code for now, but it only gets the IPs and doesn’t scan all the devices. Could someone help me to correctly get all the devices with their IP and MAC?
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final int REQUEST_CODE_PERMISSIONS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_WIFI_STATE
}, REQUEST_CODE_PERMISSIONS);
}
Button scanButton = findViewById(R.id.scanButton);
scanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ScanNetworkTask().execute();
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_PERMISSIONS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
findViewById(R.id.scanButton).setEnabled(true);
} else {
Log.e(TAG, "Permissions denied by user.");
findViewById(R.id.scanButton).setEnabled(false);
}
}
}
private class ScanNetworkTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
try {
scanNetwork();
} catch (IOException e) {
Log.e(TAG, "IOException in scanNetwork: ", e);
}
return null;
}
private void scanNetwork() throws IOException {
try {
NetworkInterface wlan0 = NetworkInterface.getByName("wlan0");
if (wlan0 != null && wlan0.isUp()) {
Enumeration<InetAddress> addresses = wlan0.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
if (address.isLoopbackAddress() || !address.isSiteLocalAddress()) {
continue;
}
String subnet = getSubnet(address.getHostAddress());
Log.d(TAG, "Scanning subnet: " + subnet);
for (int i = 1; i < 255; i++) {
String host = subnet + i;
try {
InetAddress inetAddress = InetAddress.getByName(host);
if (inetAddress.isReachable(1000)) {
Log.d(TAG, "Host: " + inetAddress.getHostAddress() + " is reachable");
Log.d(TAG, "Hostname: " + inetAddress.getHostName());
}
} catch (IOException e) {
Log.e(TAG, "IOException in scanNetwork: ", e);
}
}
}
}
} catch (SocketException e) {
Log.e(TAG, "SocketException in scanNetwork: ", e);
}
}
private String getSubnet(String ipAddress) {
return ipAddress.substring(0, ipAddress.lastIndexOf('.') + 1);
}
}
}
Luis Carlos Bautista Machuca is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.