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

发布于 2024-10-21 00:06:36 字数 4032 浏览 3 评论 0原文

我正在构建一个可以在移动设备和 Wi-Fi 设备之间传输数据的应用程序...移动设备具有 AP 启用(通过代码)并且另一台设备连接到该特定网络...我如何通过代码检测以查看连接到网络(AP)的设备的详细信息?** 有解决方案吗为了这?

我在 HTC Desire 中看到了一个名为 Wifi 热点 的应用程序执行此功能,显示连接到网络的设备的 IP 地址。如何才能实现这一目标?

查看评测:HTC EVO 4G 上的 Sprint 移动热点

它显示了一个可以实际显示连接用户的应用程序。我们如何以编程方式做到这一点?有相关的 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");
    }
}

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?

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?

Check out Review: Sprint Mobile Hotspot on 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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

高冷爸爸 2024-10-28 00:06:36

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

下面是示例代码,它计算连接到 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.
}

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.

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.

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.
}
孤寂小茶 2024-10-28 00:06:36

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

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

您可以进行 IP 地址扫描,使用上面的 ping 尝试网络上的每个 IP 地址进行响应,或者尝试使用 TCPUDP

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

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

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

    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.

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

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.

黑寡妇 2024-10-28 00:06:36

您可以使用接入点

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()

You can use 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文