Java Android应用程序Wi-Fi设备-AP连接

Java Android应用程序Wi-Fi设备-AP连接,java,android,network-programming,wifi,jvm-hotspot,Java,Android,Network Programming,Wifi,Jvm Hotspot,我正在构建一个应用程序,可以在移动设备和Wi-Fi设备之间传输数据。。。手机已启用(通过代码),另一个设备连接到此特定网络。。。如何通过代码进行检测以查看连接到网络(AP)的设备的详细信息?**是否有解决方案 我在中看到一个名为Wifi热点的应用程序,它可以显示连接到网络的设备的IP地址。如何做到这一点 退房 它显示了一个可以实际显示连接用户的应用程序。我们如何通过编程实现这一点?有一个API吗 要创建接入点,请执行以下操作: private void createWifiAccessPoint

我正在构建一个应用程序,可以在移动设备和Wi-Fi设备之间传输数据。。。手机已启用(通过代码),另一个设备连接到此特定网络。。。如何通过代码进行检测以查看连接到网络(AP)的设备的详细信息?**是否有解决方案

我在中看到一个名为Wifi热点的应用程序,它可以显示连接到网络的设备的IP地址。如何做到这一点

退房

它显示了一个可以实际显示连接用户的应用程序。我们如何通过编程实现这一点?有一个API吗

要创建接入点,请执行以下操作:

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");
    }
}

如果知道设备的主机名或IP地址,可以ping该设备

    Runtime runtime = Runtime.getRuntime();
    Process proc = runtime.exec("ping -c 1   " + hostname);
    proc.waitFor();
您可以执行IP地址扫描,使用上面的ping命令尝试网络上的每个IP地址以获取响应,或者尝试使用或进行连接

如果你知道MAC地址,你可以使用这个表


如果您在设备上运行自己的软件,您可以在每个设备上发送UDP数据包,并在Android设备上侦听它们。请参阅如何执行此操作。

您可以阅读
/proc/net/arp
文件来读取所有条目。请参阅博客文章中的示例。在ARP表中,根据IP地址搜索属于您的Wi-Fi网络的所有主机

    Runtime runtime = Runtime.getRuntime();
    Process proc = runtime.exec("ping -c 1   " + hostname);
    proc.waitFor();
下面是示例代码,它统计连接到AP的主机数。此代码假定一个ARP条目用于连接到网络的电话,其余的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.
}
您可以使用:

WifiApControl apControl=WifiApControl.getInstance(上下文);
//它们已缓存,可能不再连接,请参阅
//WifiApControl.getReachableClients(int,ReachableClientListener)
List clients=apControl.getClients()

是否有任何其他值可用于检查连接到网络的设备?因此,我可以反复浏览并找到所需的设备…在我的需求中,我有一个连接到设备的wifi控制器(例如,我在其中安装了应用程序的一部手机,我启用了AP,并且从我连接到该网络的另一部设备)。。。现在,我如何在设备(已安装的应用程序)中查找连接到网络的设备。。发送UDP数据包需要我在另一台设备上安装另一个应用程序..无论如何..我如何侦听UDP数据包?请记住,如果防火墙/路由器丢弃ICMP数据包,则ping将无法工作。您还可以尝试通过发送广播ping来简化上述解决方案。这是不对的,HTC Desire上的WiFi热点不会显示哪些设备连接到Desire上的同一WiFi网络,但它会打开一个自己的WiFi热点(以及WiFi网络),并充当自己的接入点。这就是为什么它可以显示连接的设备,因为它是它自己的网络。好吧…我可以通过编程建立一个Wifi接入点。。。现在,您将能够检测连接到此网络的设备吗?您将在哪里发送访问点消息?在非Android设备上?那么是的。在Android上,如果没有root访问权限,在普通应用程序中获取数据将非常困难。我正在Android设备上建立访问点…您的代码工作正常,但在我的htc愿望下,当我以编程方式打开热点时-DHCP禁用。。。。。为什么?????你怎么知道你必须访问这个文件???/proc/net/arp。。。这个???/proc的任何源代码都是一个标准的linux文件系统,它提供了很多关于内核的信息。有关/proc/net的详细信息,请访问。谷歌搜索/proc会给你大量的信息。这是不正确的,因为当设备断开连接时,ARP表不一定会刷新。比如说,您可以简单地ping表中的所有设备,以查看哪些IP处于活动状态,但很可能某个设备被防火墙连接,无法响应ICMP请求,ping将失败。这个解决方案虽然有效,但并不可靠和准确,。。。是否有其他替代解决方案@MridangAgarwalla用于检查客户端是否仍然连接?如果我不知道MAC或IP,如何获取连接到我的Wi-Fi Teethring网络的设备的详细信息?请帮忙