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

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

问题描述

我建立它可以在移动和Wi-Fi设备之间传输数据的应用......移动已经拿到了的 AP 启用(通过code)和其它设备连接到这个特定的网络... ...我怎样才能通过code看到连接到网络的设备的详细信息检测(AP )?**是否有解决方案吗?

我已经看到了所谓的无线热点的在的HTC Desire ,做示出连接到网络的设备的IP地址的这种功能。如何才能实现这一目标?

查阅的 <一个href="http://www.goodandevo.net/2010/05/review-sprint-mobile-hotspot-on-htc-evo-4g.html?cid=6a00d83451c9ec69e20133ef0db4e3970b"相对=nofollow>点评:Sprint的移动热点​​的HTC EVO 4G

这表明,实际上可以显示已连接用户的应用程序。我们怎样才能做到这一点编程?是否有一个API是什么?

有关创建接入点:

 私人无效createWifiAccessPoint(){
    如果(wifiManager.isWifiEnabled())
    {
        wifiManager.setWifiEnabled(假);
    }
    方法[] wmMethods = wifiManager.getClass()getDeclaredMethods()。 //获取WifiManager类中的所有声明的方法
    布尔methodFound = FALSE;

    对于(方法方法:wmMethods){
        如果(method.getName()。等于(setWifiApEnabled)){
            methodFound = TRUE;
            WifiConfiguration用netconfig =新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.$p$pSharedKey =密码;
            //netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            //netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            //netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            //netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);

            尝试 {
                布尔apstatus =(布尔)method.invoke(wifiManager,用netconfig,真正的);
                //statusView.setText("Creating一个Wi-Fi网络\+ netConfig.SSID +\);
                对于(方法isWifiApEnabledmethod:wmMethods)
                {
                    如果(isWifiApEnabledmethod.getName()。等于(isWifiApEnabled)){
                        而(!(布尔)isWifiApEnabledmethod.invoke(wifiManager)){
                        };
                        对于(方法方法一:wmMethods){
                            如果(method1.getName()。等于(getWifiApState)){
                                INT apstate;
                                apstate =(整数)method1.invoke(wifiManager);
                                //用netconfig =(WifiConfiguration)method1.invoke(WIFI);
                                //statusView.append("\nSSID:"+netConfig.SSID+"\nPassword:"+netConfig.$p$pSharedKey+"\n);
                            }
                        }
                    }
                }

                如果(apstatus)
                {
                    的System.out.println(SUCCESSdddd);
                    //statusView.append("\nAccess点创建)!;
                    //完();
                    //意图searchSensorsI​​ntent =新的意图(这一点,SearchSensors.class);
                    // startActivity(searchSensorsI​​ntent);
                }
                其他
                {
                    的System.out.println(失败);

                    //statusView.append("\nAccess点创建失败)!;
                }
            }
            赶上(抛出:IllegalArgumentException E){
                e.printStackTrace();
            }
            赶上(IllegalAccessException E){
                e.printStackTrace();
            }
            赶上(的InvocationTargetException E){
                e.printStackTrace();
            }
        }
    }
    如果(!methodFound){
        //statusView.setText("Your手机的API不包含setWifiApEnabled方法来配置接入点);
    }
}
 

解决方案

您可以阅读的/ proc /净/ ARP 文件读取所有的 ARP 项。见的例子在博客中的 <一个href="http://www.flattermann.net/2011/02/android-howto-find-the-hardware-mac-address-of-a-remote-host/"相对=nofollow>安卓HOWTO找到一个远程主机的硬件MAC地址 的。在ARP表中,搜索所有属于你的Wi-Fi网络基础上的IP地址的主机。

下面是示例code,它计数连接到AP主机的数量。此code假定1表项为连接到网络的电话,而其余的是从连接到该AP主机

 私人诠释countNumMac()
{
    INT macCount = 0;
    的BufferedReader BR = NULL;
    尝试 {
        BR =新的BufferedReader(新的FileReader(的/ proc /净/ ARP));
        串线;
        而((行= br.readLine())!= NULL){
            串[]分裂= line.split(+);
            如果(分裂= NULL和放大器;!&安培; splitted.length&GT; = 4){
                //基本的完整性检查
                串MAC =分裂[3];
                如果(mac.matches(..:..:..:..:..:..)){
                    macCount ++;
                }
            }
        }
    }
    赶上(例外五){
        e.printStackTrace();
    }
    最后 {
        尝试 {
            br.close();
        }
        赶上(IOException异常E){
            e.printStackTrace();
        }
    }

    如果(macCount == 0)
        返回0;
    其他
        返回macCount-1; //一个MAC地址表项将是主机。
}
 

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

解决方案

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

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

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