列出作为WiFi接入点连接到Android手机的设备

列出作为WiFi接入点连接到Android手机的设备,android,wifi,router,device,access-point,Android,Wifi,Router,Device,Access Point,当安卓手机充当WiFi路由器时,安卓API中是否有列出已连接设备的方法 还有一种方法可以与路由请求交互以提供欢迎/登录页面吗?这是另一个stackoverflow问题的答案,我在两个月前阅读了它,不知道答案来自哪个用户,但这应该有效。WifiManager为您提供您所需的: Main.java import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.widg

当安卓手机充当WiFi路由器时,安卓API中是否有列出已连接设备的方法


还有一种方法可以与路由请求交互以提供欢迎/登录页面吗?

这是另一个stackoverflow问题的答案,我在两个月前阅读了它,不知道答案来自哪个用户,但这应该有效。WifiManager为您提供您所需的:

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;

public class Main extends Activity {
      TextView textView1;
      WifiApManager wifiApManager;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    textView1 = (TextView) findViewById(R.id.textView1);
    wifiApManager = new WifiApManager(this);

    scan();
}

private void scan() {
    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");
    }
}
WIFI\u AP\u STATE.java

 public enum WIFI_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;

public class WifiApManager {
private final WifiManager mWifiManager;

public WifiApManager(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) {
    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;
}
}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.lang.reflect.Method;
导入java.net.InetAddress;
导入java.util.ArrayList;
导入android.content.Context;
导入android.net.wifi.WifiConfiguration;
导入android.net.wifi.WifiManager;
导入android.util.Log;
公共类WifiApManager{
私人最终WifiManager mWifiManager;
公共WifiApManager(上下文){
mWifiManager=(WifiManager)context.getSystemService(context.WIFI_服务);
}
/**
*获取连接到热点的客户端的列表,可访问超时为300
*@param onlyReachables{@code false}如果列表应包含无法访问(可能已断开连接)的客户端,则为{@code true}
*{@link ClientScanResult}的@return数组列表
*/
public ArrayList getClientList(仅布尔值可选){
返回getClientList(仅可访问,300);
}
/**
*获取连接到热点的客户端的列表
*@param onlyReachables{@code false}如果列表应包含无法访问(可能已断开连接)的客户端,则为{@code true}
*@param Reachable timeout Reachable timeout(毫秒)
*{@link ClientScanResult}的@return数组列表
*/
公共ArrayList getClientList(仅布尔值可访问,int-reachableTimeout){
BufferedReader br=null;
ArrayList结果=null;
试一试{
结果=新的ArrayList();
br=新的BufferedReader(新的文件读取器(“/proc/net/arp”);
弦线;
而((line=br.readLine())!=null){
String[]splitted=line.split(“+”);
如果((拆分!=null)和&(拆分.length>=4)){
//基本健康检查
字符串mac=splitted[3];
如果(mac.matches(“…:”)){
布尔值isReachable=InetAddress.getByName(拆分为[0]).isReachable(可达超时);
如果(!onlyreachable | | isReachable){
结果.add(新客户端扫描结果(已拆分[0]、已拆分[3]、已拆分[5]、可删除));
}
}
}
}
}捕获(例外e){
Log.e(this.getClass().toString(),e.getMessage());
}最后{
试一试{
br.close();
}捕获(IOE异常){
Log.e(this.getClass().toString(),e.getMessage());
}
}
返回结果;
}
}

我认为这不是关于Android手机充当WiFi路由器,而是只是扫描Android手机范围内的接入点。我的意思是,当您可以在手机内启动wifi路由器,而其他设备(如计算机)将手机视为路由器时,请检查此链接@killer\u PL您不应“编辑”您接受的答案。如果不是你想要的答案,不要接受。如果你找到了问题的答案,请将其作为其他答案或评论分享。但是1。塞吉找到了。2.目前被接受的答案是完全离题的,任何进入该页面的人都会感到困惑。我编辑它是因为真正的答案只存在于Segi注释中,而答案内容现在(在我的编辑还原后)已脱离主题且无效。@killer\u PL在这种情况下,您实际上可以“不接受”答案,但由于Segi编辑了他的答案,所以现在与此无关。
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;

public class WifiApManager {
private final WifiManager mWifiManager;

public WifiApManager(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) {
    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;
}
}