Android应用Wi-Fi设备- AP连接

时间:2021-03-20 17:21:21

I am building an application which can transfer data between a mobile and a Wi-Fi device... The mobile has got the AP enabled (through code) and another device connects to this specific network... How can I detect through code to see the details of the devices connected to the network(AP)?** Is there a solution for this?

我正在开发一个应用程序,可以在移动设备和Wi-Fi设备之间传输数据……移动设备已经启用了AP(通过代码),另一个设备连接到这个特定的网络……如何通过代码检测到连接到网络的设备的详细信息(AP)?**有解决办法吗?

I have seen an application called Wifi Hot spot in HTC Desire that does this functionality of showing the IP addresses of the devices connected to the network. How can this be achieved?

我在HTC Desire中看到了一个名为Wifi热点的应用,它的功能是显示连接到网络的设备的IP地址。如何实现这一点?

Check out Review: Sprint Mobile Hotspot on HTC EVO 4G.

查看:在HTC EVO 4G上的斯普林特移动热点。

It shows an application that can actually display the connected users. How can we do that programmatically? Is there an API for that?

它显示了一个可以实际显示连接用户的应用程序。我们如何用编程的方式去做呢?有API吗?

For creating an access point:

创建访问点:

private void createWifiAccessPoint() {
    if (wifiManager.isWifiEnabled())
    {
        wifiManager.setWifiEnabled(false);
    }
    Method[] wmMethods = wifiManager.getClass().getDeclaredMethods(); //Get all declared methods in WifiManager class
    boolean methodFound = false;

    for (Method method: wmMethods){
        if (method.getName().equals("setWifiApEnabled")){
            methodFound = true;
            WifiConfiguration netConfig = new WifiConfiguration();
            netConfig.SSID = "\""+ssid+"\"";
            netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
            //netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            //netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            //netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            //netConfig.preSharedKey = password;
            //netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            //netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            //netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            //netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);

            try {
                boolean apstatus = (Boolean) method.invoke(wifiManager, netConfig,true);
                //statusView.setText("Creating a Wi-Fi Network \""+netConfig.SSID+"\"");
                for (Method isWifiApEnabledmethod: wmMethods)
                {
                    if (isWifiApEnabledmethod.getName().equals("isWifiApEnabled")){
                        while (!(Boolean)isWifiApEnabledmethod.invoke(wifiManager)){
                        };
                        for (Method method1: wmMethods){
                            if(method1.getName().equals("getWifiApState")){
                                int apstate;
                                apstate = (Integer)method1.invoke(wifiManager);
                                //                      netConfig = (WifiConfiguration)method1.invoke(wifi);
                                //statusView.append("\nSSID:"+netConfig.SSID+"\nPassword:"+netConfig.preSharedKey+"\n");
                            }
                        }
                    }
                }

                if(apstatus)
                {
                    System.out.println("SUCCESSdddd");
                    //statusView.append("\nAccess Point Created!");
                    //finish();
                    //Intent searchSensorsIntent = new Intent(this,SearchSensors.class);
                    //startActivity(searchSensorsIntent);
                }
                else
                {
                    System.out.println("FAILED");

                    //statusView.append("\nAccess Point Creation failed!");
                }
            }
            catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
    if (!methodFound){
        //statusView.setText("Your phone's API does not contain setWifiApEnabled method to configure an access point");
    }
}

3 个解决方案

#1


7  

You could read the /proc/net/arp file to read all the ARP entries. See the example in the blog post Android: Howto find the hardware MAC address of a remote host. In the ARP table, search for all the hosts that belong to your Wi-Fi network based on the IP address.

您可以读取/proc/net/arp文件以读取所有ARP条目。参见Android博客文章中的示例:如何查找远程主机的硬件MAC地址。在ARP表中,根据IP地址搜索属于Wi-Fi网络的所有主机。

Here is example code, which counts the number of hosts connected to the AP. This code assumes that one ARP entry is for the phone connected to the network and the remaining ones are from hosts connected to the AP.

下面是示例代码,它计算连接到AP的主机数量,该代码假设一个ARP条目用于连接到网络的电话,其余的来自连接到AP的主机。

private int countNumMac()
{
    int macCount = 0;
    BufferedReader br = null;
    try {
        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("..:..:..:..:..:..")) {
                    macCount++;
                }
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        try {
            br.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    if (macCount == 0)
        return 0;
    else
        return macCount-1; //One MAC address entry will be for the host.
}

#2


5  

You could ping the device if you know its host-name or its IP address.

如果您知道设备的主机名或IP地址,可以对设备进行ping。

    Runtime runtime = Runtime.getRuntime();
    Process proc = runtime.exec("ping -c 1   " + hostname);
    proc.waitFor();

You could do an IP address scan, trying every IP address on the network for a response using a ping like above or trying to connect using TCP or UDP.

您可以进行IP地址扫描,使用上面的ping测试网络上的每个IP地址,或者尝试使用TCP或UDP连接。

If you know the MAC address, you could use the ARP table.

如果您知道MAC地址,可以使用ARP表。

If you got some own software running on the devices, you could send out UDP packets on every device and listen for them on your Android device. See Sending and receiving UDP broadcast packets in Android on how to do this.

如果你有一些自己的软件在设备上运行,你可以在每个设备上发送UDP数据包并在你的Android设备上监听它们。关于如何做到这一点,请参阅在Android中发送和接收UDP广播包。

#3


2  

You can use accesspoint:

您可以使用accesspoint:

WifiApControl apControl = WifiApControl.getInstance(context);

// These are cached and may no longer be connected, see
// WifiApControl.getReachableClients(int, ReachableClientListener)
List<WifiApControl.Client> clients = apControl.getClients()

#1


7  

You could read the /proc/net/arp file to read all the ARP entries. See the example in the blog post Android: Howto find the hardware MAC address of a remote host. In the ARP table, search for all the hosts that belong to your Wi-Fi network based on the IP address.

您可以读取/proc/net/arp文件以读取所有ARP条目。参见Android博客文章中的示例:如何查找远程主机的硬件MAC地址。在ARP表中,根据IP地址搜索属于Wi-Fi网络的所有主机。

Here is example code, which counts the number of hosts connected to the AP. This code assumes that one ARP entry is for the phone connected to the network and the remaining ones are from hosts connected to the AP.

下面是示例代码,它计算连接到AP的主机数量,该代码假设一个ARP条目用于连接到网络的电话,其余的来自连接到AP的主机。

private int countNumMac()
{
    int macCount = 0;
    BufferedReader br = null;
    try {
        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("..:..:..:..:..:..")) {
                    macCount++;
                }
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        try {
            br.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    if (macCount == 0)
        return 0;
    else
        return macCount-1; //One MAC address entry will be for the host.
}

#2


5  

You could ping the device if you know its host-name or its IP address.

如果您知道设备的主机名或IP地址,可以对设备进行ping。

    Runtime runtime = Runtime.getRuntime();
    Process proc = runtime.exec("ping -c 1   " + hostname);
    proc.waitFor();

You could do an IP address scan, trying every IP address on the network for a response using a ping like above or trying to connect using TCP or UDP.

您可以进行IP地址扫描,使用上面的ping测试网络上的每个IP地址,或者尝试使用TCP或UDP连接。

If you know the MAC address, you could use the ARP table.

如果您知道MAC地址,可以使用ARP表。

If you got some own software running on the devices, you could send out UDP packets on every device and listen for them on your Android device. See Sending and receiving UDP broadcast packets in Android on how to do this.

如果你有一些自己的软件在设备上运行,你可以在每个设备上发送UDP数据包并在你的Android设备上监听它们。关于如何做到这一点,请参阅在Android中发送和接收UDP广播包。

#3


2  

You can use accesspoint:

您可以使用accesspoint:

WifiApControl apControl = WifiApControl.getInstance(context);

// These are cached and may no longer be connected, see
// WifiApControl.getReachableClients(int, ReachableClientListener)
List<WifiApControl.Client> clients = apControl.getClients()