Device Discovery in Android

712 views Asked by At

I am trying to create an application which can find all devices in local area network. I have been able to do it by pinging the IP range. Are there any other ways to do the same?

Is it possible to do detect devices in same network by listening to incoming packets in android ?

2

There are 2 answers

1
Ravneet Singh On

Following code will search the device and put their information in a list:-

     //an array list of DeviceList type, which will take the info from getter setters
    ArrayList<DeviceList> Dlist = new ArrayList<DeviceInfo>();
    wifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    if(wifi.isWifiEnabled()){

    List<ScanResult>wifilist =wifi.getScanResults();  

    if(wifilist != null){
    for(int m = 0; m < wifilist.size(); m++)
    {
        int signallevel = WifiManager.calculateSignalLevel(wifilist.get(m).level,20);
        int signalStrength=(signallevel*100)/20;
        DeviceList dInfo = new DeviceList();
        dInfo.setDeviceName((wifilist.get(m).SSID));
        dInfo.setSignalStrength(String.valueOf((signalStrength+"%")));
        dInfo.setDeviceType("WiFi");

        Dlist.add(dInfo);
    }
}     

and you'll need some getter setters for this. You can add them in a different class as follows:-

 public class DeviceList {

  String DeviceName="";
  String SignalStrength="";
  String deviceType="";

  public String getDeviceName() {
   return DeviceName;
  }
  public void setDeviceName(String deviceName) {
   DeviceName = deviceName;
  }
  public String getSignalStrength() {
   return SignalStrength;
  }
  public void setSignalStrength(String signalStrength) {
   SignalStrength = signalStrength;
  }
  public String getDeviceType() {
   return deviceType;
  }
  public void setDeviceType(String deviceType) {
   this.deviceType = deviceType;
  }
 }
0
d4n3 On

This depends on what you want to do with the devices.

To discover UPNP compatible devices, you could use the Cling library.

This will return a list of devices, descriptions and services available.

Pinging the IP range will only show devices that respond to ping, and could depend on the network setup.