how to get the IP of the wifi hotspot in Android?

后端 未结 7 1237
半阙折子戏
半阙折子戏 2021-02-02 14:11

As the title says... I\'m trying to be able to get the IP of the wifi iface when it is configured as hotspot. Ideally, I would like to find something that works for all the phon

7条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-02 15:04

    When the Wifi is not setup as a hotspot, it has a name android-xx7632x324x32423 home when hotspot is turned on, that name is gone. Also the ip address changes.

    So if you are able to get the Wifi config before enabling the hotspot, first of all you can use intf.getName() to get a reference to it.

    Second, the ip changed, so if you know which interface the wifi is in CONNECTED mode, you can use that info to identify it later on after enabling the hotspot.

    Below is some code I used for debugging. I just spit out everything I can find, make a huge mess then clean it up when I figured my problem out. GL

    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.util.Collections;
    import android.net.ConnectivityManager;
    
    textStatus = (TextView) findViewById(R.id.textStatus);
    
    try {
      for (NetworkInterface intf : Collections.list(NetworkInterface.getNetworkInterfaces())) {
        for (InetAddress addr : Collections.list(intf.getInetAddresses())) {
          if (!addr.isLoopbackAddress()){
            textStatus.append("\n\n IP Address: " + addr.getHostAddress() );
            textStatus.append("\n" + addr.getHostName() );
            textStatus.append("\n" + addr.getCanonicalHostName() );
            textStatus.append("\n\n" + intf.toString() );
            textStatus.append("\n\n" + intf.getName() );
            textStatus.append("\n\n" + intf.isUp() );
          } 
        }
      }
    } catch (Exception ex) {
      textStatus.append("\n\n Error getting IP address: " + ex.getLocalizedMessage() );
    }
    
    
    connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    allInfo = connectivity.getAllNetworkInfo();
    mobileInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    
    textStatus.append("\n\n TypeName: " + mobileInfo.getTypeName());
    textStatus.append("\n State: " + mobileInfo.getState());
    textStatus.append("\n Subtype: " + mobileInfo.getSubtype());
    textStatus.append("\n SubtypeName: " + mobileInfo.getSubtypeName());
    textStatus.append("\n Type: " + mobileInfo.getType());
    textStatus.append("\n ConnectedOrConnecting: " + mobileInfo.isConnectedOrConnecting());
    textStatus.append("\n DetailedState: " + mobileInfo.getDetailedState());
    textStatus.append("\n ExtraInfo: " + mobileInfo.getExtraInfo());
    textStatus.append("\n Reason: " + mobileInfo.getReason());
    textStatus.append("\n Failover: " + mobileInfo.isFailover());
    textStatus.append("\n Roaming: " + mobileInfo.isRoaming()); 
    
    textStatus.append("\n\n 0: " + allInfo[0].toString());
    textStatus.append("\n\n 1: " + allInfo[1].toString());
    textStatus.append("\n\n 2: " + allInfo[2].toString());
    

提交回复
热议问题