2

次の問題を解決したい。私のデバイスは AP モード (ポータブル WiFi ホットスポット) です。It has to show IP of it. 別のデバイスが、既知の IP を使用してこのデバイスに接続します。WiFi ルーターを使用せずに、デバイス間で動作する必要があります。無線がすでに AP モードで動作している場合、IP アドレスを取得する方法は? APに関するコードがいくつかあります:

public boolean setWifiApEnabled(WifiConfiguration config, boolean enabled) {         
       try {
        if (enabled) { // disable WiFi in any case
         mWifiManager.setWifiEnabled(false);
        }

        Method method = mWifiManager.getClass().getMethod(
          "setWifiApEnabled", WifiConfiguration.class,
          boolean.class);
        return (Boolean) method.invoke(mWifiManager, config, enabled);
       } catch (Exception e) {
        //Log.e(TAG, "", e);
        return false;
       }
      }

public int getWifiApState() {
       try {
        Method method = mWifiManager.getClass().getMethod(
          "getWifiApState");
        return (Integer) method.invoke(mWifiManager);
       } catch (Exception e) {
        //Log.e(TAG, "", e);
        return WIFI_AP_STATE_FAILED;
       }
      }

 public static boolean IsWifiApEnabled(Context context){ 
          boolean isWifiAPEnabled = false;        
          WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
          Method[] wmMethods = wifi.getClass().getDeclaredMethods();
          for(Method method: wmMethods){
              if(method.getName().equals("isWifiApEnabled")) {  
                  try {
                    isWifiAPEnabled = (Boolean) method.invoke(wifi);
                  } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                  } catch (IllegalAccessException e) {
                    e.printStackTrace();
                  } catch (InvocationTargetException e) {
                    e.printStackTrace();
                  }
              }
          }
          return isWifiAPEnabled;
      }
 }

多分それのIPを取得するためのいくつかのトリックがありますか? 私を助けてください。ありがとう。

4

2 に答える 2

1

これを使用して IP アドレスを決定します。

    private String determineHostAddress() {
        try {
            for (Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); nis.hasMoreElements(); ) {
                NetworkInterface ni = nis.nextElement();
                if (!ni.isLoopback() && !ni.isVirtual() && ni.isUp() && !ni.isPointToPoint() && ni.getHardwareAddress() != null) {
                    for (Enumeration<InetAddress> ips = ni.getInetAddresses(); ips.hasMoreElements(); ) {
                        InetAddress ip = ips.nextElement();
                        if (ip.getAddress().length == 4) {
                            return ip.getHostAddress();
                        }
                    }
                }
            }
        } catch (Exception ignored) {}
        return null;
    }
于 2014-12-03T15:45:29.920 に答える
-1

これはあなたのケースには当てはまらないかもしれませんが、私のアプリケーションでは、ホットスポットに接続している別の Android デバイスに入力して、ホットスポットで実行されている Web サーバーに接続できるように、ホットスポットの IP アドレスを表示することを計画していました。そのシナリオでは、クライアント (ホットスポットに接続されたデバイス) は、ホットスポットに接続されると、接続先のゲートウェイの IP アドレスを照会するだけで済みます。これは常にホットスポットの IP アドレスになります。これが私のコードです:

@SuppressWarnings("deprecation") // Deprecated because it doesn't handle IPv6 but wifimanager only supports IPV4 anyway
private String getGateway()
{
    final WifiManager wifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
    return Formatter.formatIpAddress(dhcpInfo.gateway);
}

ちなみに、これまでにテストしたすべての Android で、ホットスポットの IP アドレスは常に 192.168.43.1 です。この質問によると、Android ソースにハードコーディングされています。

于 2014-12-05T18:15:39.133 に答える