Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Austin Aigbe
Front End Web Development Techdegree Student 1,065 PointsHow to get the Connected Wifi Devices
Hello, I am using the wifi hotspot and tethering function on my Android phone to connect to the WAMP server running on my PC. Now, I need to get the connected device(s) to my phone's Wifi AP. How can I do this?
Currently, I am using reflection to check the state of the Wifi AP:
public int getWifiAPState() {
WifiManager wifi = (WifiManager) _context.getSystemService(Context.WIFI_SERVICE);
int state = -1;
try {
Method method1 = wifi.getClass().getMethod("getWifiApState");
state = (Integer) method1.invoke(wifi);
} catch (Exception e) {}
Log.d("WifiAP", "getWifiAPState.state " + (state == -1 ? "UNKNOWN":WIFI_STATE_TEXTSTATE[state-10]));
return state;
}
From the LogCat, I can see the WIfiStateMachine getConnectedWifiDevices result. How do I get this information programmatically?
Thanks.
Austin
2 Answers
Austin Aigbe
Front End Web Development Techdegree Student 1,065 PointsThanks Ben.
Currently, I am using the following piece of code to get the connected WiFi Device (in this case, my PC running a WAMP server):
public boolean isConnectedToServer() {
ArrayList<ClientScanResult> clients = getClientList(false);
for (ClientScanResult clientScanResult : clients) {
Log.d("IP Address", clientScanResult.getIpAddr());
// IP address of my PC (192.148.43.20), running a WAMP server
if (clientScanResult.getIpAddr().matches("192.148.43.20")){
return true;
}
}
return false;
}
public ArrayList<ClientScanResult> getClientList(boolean onlyReachables) {
return getClientList(onlyReachables, 300);
}
/**
* Gets a list of the clients connected to the Hotspot
* @param onlyReachables {@code false} if the list should contain unreachable (probably disconnected) clients, {@code true} otherwise
* @param reachableTimeout Reachable Timout in miliseconds
* @return ArrayList of {@link ClientScanResult}
*/
public ArrayList<ClientScanResult> getClientList(boolean onlyReachables, int reachableTimeout) {
BufferedReader br = null;
ArrayList<ClientScanResult> result = null;
try {
result = new ArrayList<ClientScanResult>();
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
String[] splitted = line.split(" +");
if ((splitted != null) && (splitted.length >= 4)) {
// Basic sanity check
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
boolean isReachable = InetAddress.getByName(splitted[0]).isReachable(reachableTimeout);
if (!onlyReachables || isReachable) {
result.add(new ClientScanResult(splitted[0], splitted[3], splitted[5], isReachable));
}
}
}
}
} catch (Exception e) {
Log.e(this.getClass().toString(), e.getMessage());
} finally {
try {
br.close();
} catch (IOException e) {
Log.e(this.getClass().toString(), e.getMessage());
}
}
return result;
}
Ben Jakuben
Treehouse TeacherI've never tried this, but perhaps you can try this library from GitHub: https://github.com/rorist/android-network-discovery
Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherCool - thanks for sharing!