List Connected Devices To Android Phone Working As Wifi Access Point
Is there a way in Android API to list connected devices, when Android phone is acting as WiFi router? And also is there a way to interefere with routed request to serve a welcome/l
Solution 1:
This is an answer from another stackoverflow question, I read it before two month and don´t know from which user the answer is from, but this should work. WifiManager gives You what You need:
Main.java
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.whitebyte.hotspotclients.R;
import com.whitebyte.wifihotspotutils.ClientScanResult;
import com.whitebyte.wifihotspotutils.WifiApManager;
publicclassMainextendsActivity {
TextView textView1;
WifiApManager wifiApManager;
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView1 = (TextView) findViewById(R.id.textView1);
wifiApManager = newWifiApManager(this);
scan();
}
privatevoidscan() {
ArrayList<ClientScanResult> clients = wifiApManager.getClientList(false);
textView1.append("Clients: \n");
for (ClientScanResult clientScanResult : clients) {
textView1.append("####################\n");
textView1.append("IpAddr: " + clientScanResult.getIpAddr() + "\n");
textView1.append("Device: " + clientScanResult.getDevice() + "\n");
textView1.append("HWAddr: " + clientScanResult.getHWAddr() + "\n");
textView1.append("isReachable: " + clientScanResult.isReachable() + "\n");
}
}
ClientScanResult.java
publicclassClientScanResult {
privateStringIpAddr;
privateStringHWAddr;
privateStringDevice;
privateboolean isReachable;
publicClientScanResult(String ipAddr, String hWAddr, String device, boolean isReachable) {
super();
IpAddr = ipAddr;
HWAddr = hWAddr;
Device = device;
this.setReachable(isReachable);
}
publicStringgetIpAddr() {
returnIpAddr;
}
publicvoidsetIpAddr(String ipAddr) {
IpAddr = ipAddr;
}
publicStringgetHWAddr() {
returnHWAddr;
}
publicvoidsetHWAddr(String hWAddr) {
HWAddr = hWAddr;
}
publicStringgetDevice() {
returnDevice;
}
publicvoidsetDevice(String device) {
Device = device;
}
publicvoidsetReachable(boolean isReachable) {
this.isReachable = isReachable;
}
publicbooleanisReachable() {
return isReachable;
}
WIFI_AP_STATE.java
publicenumWIFI_AP_STATE
{
WIFI_AP_STATE_DISABLING, WIFI_AP_STATE_DISABLED, WIFI_AP_STATE_ENABLING, WIFI_AP_STATE_ENABLED, WIFI_AP_STATE_FAILED
}
WifiApManager.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.util.ArrayList;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;
publicclassWifiApManager {
privatefinal WifiManager mWifiManager;
publicWifiApManager(Context context) {
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
}
/**
* Gets a list of the clients connected to the Hotspot, reachable timeout is 300
* @param onlyReachables {@code false} if the list should contain unreachable (probably disconnected) clients, {@code true} otherwise
* @return ArrayList of {@link ClientScanResult}
*/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) {
BufferedReaderbr=null;
ArrayList<ClientScanResult> result = null;
try {
result = newArrayList<ClientScanResult>();
br = newBufferedReader(newFileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
String[] splitted = line.split(" +");
if ((splitted != null) && (splitted.length >= 4)) {
// Basic sanity checkStringmac= splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
booleanisReachable= InetAddress.getByName(splitted[0]).isReachable(reachableTimeout);
if (!onlyReachables || isReachable) {
result.add(newClientScanResult(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;
}
}
Post a Comment for "List Connected Devices To Android Phone Working As Wifi Access Point"