Android 应用 Wi-Fi 设备 - AP 连接 [英] Android application Wi-Fi device - AP connectivity

查看:30
本文介绍了Android 应用 Wi-Fi 设备 - AP 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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 EVO 4G 上的 Sprint 移动热点.

它显示了一个可以实际显示连接用户的应用程序.我们如何以编程方式做到这一点?有相应的 API 吗?

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

用于创建接入点:

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("
SSID:"+netConfig.SSID+"
Password:"+netConfig.preSharedKey+"
");
                            }
                        }
                    }
                }

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

                    //statusView.append("
Access 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");
    }
}

推荐答案

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

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.

这里是示例代码,它计算连接到 AP 的主机数量.此代码假定一个 ARP 条目用于连接到网络的电话,其余的来自连接到 AP 的主机.

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.
}

这篇关于Android 应用 Wi-Fi 设备 - AP 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆