I have the following IpScanActivity when I populate the listView adapter with fake data, it works, but when I run it with the try/catch doInbackground() method it is not showing anything. I can see the host increasing in the logs. Is it the time out? I have more hosts on my network, so it is not that. any ideas.. thanks
IpScanActivity
IpScanActivity
Code:
public class IpScanActivity extends AppCompatActivity {
String ip;
String mac;
ArrayList<Node> hostList;
ListView networkScan;
[USER=1021285]@override[/USER]
protected void onCreate([USER=1996173]@nullable[/USER] Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scan_list);
hostList = new ArrayList<>();
//inflating adapter
networkScan = (ListView) findViewById(R.id.scan_list);
NodeAdapter networkAdapter = new NodeAdapter(this, R.layout.list_item, hostList);
networkScan.setAdapter(networkAdapter);
//scanning network
TaskScanNetwork scanNetwork = new TaskScanNetwork();
scanNetwork.execute();
}
Code:
/*
* AscynTask to scan the network
* you should try different timeout for your network/devices
* it will try to detect localhost ip addres and subnet. then
* it will use subnet to scan network
*
*/
private class TaskScanNetwork extends AsyncTask<Void, Node, Void> {
String localIp;
String mac;
String subnet;
static final int lower = 1;
static final int upper = 254;
static final int timeout = 2000;
[USER=1021285]@override[/USER]
protected void onPreExecute() {
WifiManager wifiMan1 = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo1 = wifiMan1.getConnectionInfo();
int ipAddress = wifiInfo1.getIpAddress();
localIp = String.format("%d.%d.%d.%d", (ipAddress & 0xff),(ipAddress >> 8 & 0xff),(ipAddress >> 16 & 0xff),(ipAddress >> 24 & 0xff));
Log.v("Ip Address: ", localIp);
subnet = localIp.substring(0, localIp.lastIndexOf("."));
hostList.clear();
}
[USER=1021285]@override[/USER]
protected Void doInBackground(Void... params) {
for (int i = lower; i <= upper; i++) {
ip = subnet + "." + i;
Log.v("Host: ", ip);
mac = "00:11:22:33:44:" + i;
/*Node newNode = new Node(ip, "00:11:22:33:44:55");
publishProgress(newNode);*/
try {
InetAddress inetAddress = InetAddress.getByName(ip);
if (inetAddress.isReachable(timeout)) {
Node newNode = new Node(ip, mac);
publishProgress(newNode);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
[USER=1021285]@override[/USER]
protected void onProgressUpdate(Node... values) {
hostList.add(values[0]);
networkScan.deferNotifyDataSetChanged();
}
[USER=1021285]@override[/USER]
protected void onPostExecute(Void aVoid) {
Toast.makeText(getApplicationContext(), "Scan Completed!", Toast.LENGTH_LONG);
}
}
}
Last edited by a moderator: