12

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

我在HTC Desire中看到了一个名为Wifi Hotpot的应用程序,它具有显示连接到网络的设备的 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");
    }
}
4

3 回答 3

8

您可以读取该/proc/net/arp文件以读取所有ARP条目。请参阅博文Android: Howto find the hardware MAC address of a remote host中的示例。在 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.
}
于 2011-03-16T00:02:01.607 回答
5

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

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

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

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

如果您在设备上运行了一些自己的软件,您可以在每台设备上发送 UDP 数据包并在您的 Android 设备上监听它们。有关如何执行此操作,请参阅在 Android 中发送和接收 UDP 广播数据包。

于 2011-03-08T15:08:35.310 回答
3

您可以使用接入点

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()
于 2015-09-10T21:37:26.203 回答