简体   繁体   中英

Delphi XE7 Android How to get current ipaddress, submask and gateway?

My project developing by Delphi XE7 for Androiod device need the function declared in the title.

I found a piece of code which can run and get the correct results on Windows and IOS platform. But on an Android device, the results are always: IP address = 127.0.0.1, subnet mask is blank.

procedure TForm1.RefreshList;
var
  LList: TIdStackLocalAddressList;
  I: Integer;
  AAddresses: TStrings;
begin
  AAddresses := TStringList.Create;
  try
    TIdStack.IncUsage;
    try
      LList := TIdStackLocalAddressList.Create;
      try
        // for backwards compatibility, return only IPv4 addresses
        GStack.GetLocalAddressList(LList);
        if LList.Count > 0 then begin
          AAddresses.BeginUpdate;
          try
            for I := 0 to LList.Count-1 do begin
              if LList[I].IPVersion = Id_IPv4 then begin
                AAddresses.Add(
                  LList[I].IPAddress+':'+
                  TIdStackLocalAddressIPv4(LList[I]).SubNetMask);
              end;
            end;
          finally
            AAddresses.EndUpdate;
          end;
        end;
      finally
        LList.Free;
      end;
    finally
      TIdStack.DecUsage;
    end;
    if AAddresses.Count > 0 then
      Text1.Text:= AAddresses.Text;
  finally
    AAddresses.Free;
  end;
end;

I found that by android.net.wifi.WifiManager.getDhcpInfo can probably get the information, but I do not know how to use the interface in Delphi or is that the right way?

type

  JWifiManager = interface;
  JDhcpInfo = interface;

  JWifiManagerClass = interface( JObjectClass )
    ['{0238345B-CF08-4139-B943-64900FC845F5}']
    function _GetACTION_PICK_WIFI_NETWORK : JString;
    function _GetEXTRA_WIFI_INFO : JString;
    function _GetWIFI_STATE_CHANGED_ACTION : JString;

    property ACTION_PICK_WIFI_NETWORK : JString read _GetACTION_PICK_WIFI_NETWORK;
    property EXTRA_WIFI_INFO : JString read _GetEXTRA_WIFI_INFO;
    property WIFI_STATE_CHANGED_ACTION : JString read _GetWIFI_STATE_CHANGED_ACTION;
  end;

  [ JavaSignature( 'android/net/wifi/WifiManager' ) ]
  JWifiManager = interface( JObject )
    ['{28DF429C-6E3E-4AFE-8372-18D8E81734E4}']
    function isWifiEnabled : Boolean; cdecl;
    function setWifiEnabled( enabled : Boolean ) : Boolean; cdecl;
    function getDhcpInfo : JDHCPInfo; cdecl;
    function getWifiState : Integer; cdecl;
    function getConnectionInfo : JWifiInfo; cdecl;
  end;

  TJWifiManager = class( TJavaGenericImport< JWifiManagerClass, JWifiManager > )
  end;


  JDHCPInfoClass = interface( JObjectClass )
    [ '{65204FA7-CD50-4EA9-85D6-0A9296A01C40}' ]
  end;

  [ JavaSignature( 'android/net/DhcpInfo' ) ]
  JDHCPInfo = interface( JObject )
    [ '{BBA64EF4-C771-4ECE-BD98-D6A706A5137F}' ]
  end;

  TJDHCPInfo = class( TJavaGenericImport< JDHCPInfoClass, JDHCPInfo > )
  end;


procedure TForm1.Button1Click(Sender: TObject);
var
  WifiManagerObj: JObject;
  WifiManager: JWifiManager;
  DhcpInfo: JDHCPInfo;
begin
  WifiManagerObj := SharedActivityContext.getSystemService(TJContext.JavaClass.WIFI_SERVICE);
  WifiManager := TJWifiManager.Wrap((WifiManagerObj as ILocalObject).GetObjectID);
  try
    DhcpInfo:= WifiManager.getDhcpInfo;
    text1.Text:= Text1.Text + #13#13 + JStringToString(DhcpInfo.toString);
  except on E:System.SysUtils.Exception do
    begin
      text1.Text:= e.ToString;
    end;
  end;
end;

the code above can print out right information in string format. Any suggestions about define the JDHCPInfoClass and JDHCPInfo interface to read field value directly? such as : DhcpInfo.netmask, DhcpInfo.gateway ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM