12

モバイルと Wi-Fi デバイス間でデータを転送できるアプリケーションを構築しています...モバイルは(コードを介して) APを有効にしており、別のデバイスがこの特定のネットワークに接続しています...コードを介してどのように検出できますか?ネットワーク (AP) に接続されているデバイスの詳細を表示しますか?** これに対する解決策はありますか?

ネットワークに接続されたデバイスの IP アドレスを表示するこの機能を実行するHTC DesireのWifi ホット スポットと呼ばれるアプリケーションを見たことがあります。これはどのように達成できますか?

レビューをご覧ください: HTC EVO 4G の Sprint Mobile Hotspot .

実際に接続ユーザーを表示できるアプリケーションを示しています。どうすればプログラムでそれを行うことができますか? そのための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:リモートホストのハードウェアMACアドレスを見つける方法の例を参照してください。ARPテーブルで、IPアドレスに基づいてWi-Fiネットワークに属するすべてのホストを検索します。

APに接続されているホストの数をカウントするサンプルコードを次に示します。このコードは、1つの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();

上記のような ping を使用して応答をネットワーク上のすべての IP アドレスで試すか、TCPまたはUDPを使用して接続を試みて、IP アドレス スキャンを実行できます。

MAC アドレスがわかっている場合は、ARPテーブルを使用できます。

デバイスで独自のソフトウェアを実行している場合は、すべてのデバイスで UDP パケットを送信し、Android デバイスでリッスンできます。これを行う方法については、Android での UDP ブロードキャスト パケットの送受信を参照してください。

于 2011-03-08T15:08:35.310 に答える