Get Devices Connected to Same Network Programmatically
Published December 08, 2020In this post we will learn how to get list of devices connected to wifi network in Android.
For this example we are using the library Android NetworkTool
This library provides different features like
- Port Scanning
- Subnet Device Finder (discovers devices on local network)
- Ping
- Wake-On-Lan
Let's get started
Step 1: Create Android studio Project
Step 2: Add dependencies to your build.gradle files
Project Level Gradle file
repositories {
maven {
url "https://jitpack.io"
}
}
|
App Level gradle file
dependencies {
compile 'com.github.stealthcopter:AndroidNetworkTools:0.4.5.3'
}
|
When you add dependency please check for latest release here
Add permissions in Manifest file
<uses-permission android:name="android.permission.INTERNET" />
|
Step 3: Update main activity file
public class MainActivity extends AppCompatActivity {
TextView tvWifiState;
TextView tvScanning, tvResult;
ArrayList<InetAddress> inetAddresses;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvWifiState = (TextView)findViewById(R.id.WifiState);
tvScanning = (TextView)findViewById(R.id.Scanning);
tvResult = (TextView)findViewById(R.id.Result);
findSubnetDevices();
}
private void findSubnetDevices() {
final long startTimeMillis = System.currentTimeMillis();
tvScanning.setText("Scanning...");
SubnetDevices subnetDevices = SubnetDevices.fromLocalAddress().findDevices(new SubnetDevices.OnSubnetDeviceFound() {
@Override
public void onDeviceFound(Device device) {
}
public void onFinished(ArrayList<Device> devicesFound) {
float timeTaken = (System.currentTimeMillis() - startTimeMillis)/1000.0f;
runOnUiThread(new Runnable() {
@Override
public void run() {
tvScanning.setText("Scan Finished");
for(Device device:devicesFound) {
tvResult.append("Device " + device.hostname);
tvResult.append("IP : " + device.ip + "\n");
tvResult.append("Mack : " + device.mac + "\n");
tvResult.append("\n");
}
}
});
/* appendResultsText("Finished "+timeTaken+" s");
setEnabled(subnetDevicesButton, true);*/
}
});
// Below is example of how to cancel a running scan
// subnetDevices.cancel();
}
} |
here is the xml file for the example
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:textSize="25sp"
android:layout_margin="10dp"
android:textColor="#A213BA"
android:text="https://www.rrtutors.com/"
android:textStyle="bold" />
<TextView
android:id="@+id/WifiState"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:textSize="25sp"
android:textColor="#088E81"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="Scanning: "/>
<TextView
android:id="@+id/Scanning"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#0AEC13"
android:textStyle="italic"/>
</LinearLayout>
<TextView
android:id="@+id/Result"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:textSize="25sp"
android:textColor="#000"
android:layout_height="wrap_content"
android:textStyle="bold"/>
</LinearLayout>
|
In this sample we can also get the Wifi State of connected network by
WifiManager wifiManager =
(WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
tvWifiState.setText(readtvWifiState(wifiManager));
private String readtvWifiState(WifiManager wm){
String result = "";
switch (wm.getWifiState()){
case WifiManager.WIFI_STATE_DISABLED:
result = "WIFI_STATE_DISABLED";
break;
case WifiManager.WIFI_STATE_DISABLING:
result = "WIFI_STATE_DISABLING";
break;
case WifiManager.WIFI_STATE_ENABLED:
result = "WIFI_STATE_ENABLED";
break;
case WifiManager.WIFI_STATE_ENABLING:
result = "WIFI_STATE_ENABLING";
break;
case WifiManager.WIFI_STATE_UNKNOWN:
result = "WIFI_STATE_UNKNOWN";
break;
default:
}
return result;
}
|
Step 5: Now run the application
![]() |
Other Libraries to check Connected Devices in same Network in Android Programatically
Article Contributed By :
|
|
|
|
8651 Views |