以@Remy Lebeau在此 SO 專案中非常有用的代碼示例為模型,我有以下函式,該函式在 Windows 10 中按預期作業回傳;例如:
No of Addresses: 4
IPv4 Addresses:
IP Address #0: 192.168.56.1 - 255.255.255.0 - 11
IP Address #1: 192.168.1.7 - 255.255.255.0 - 8
IP: 192.168.56.1
但是,當我將平臺更改為 Android 64 位并在啟用 WiFi 并連接到我的 LAN 的情況下在三星 S21 上運行它時,它只回傳本地環回 IP 而沒有其他值;例如:
No of Addresses: 1
IPv4 Addresses:
IP Address #0: 127.0.0.1 - - 0
IP: 127.0.0.1
我曾希望這與缺乏一些許可有關,但正如 Remy 在評論中指出的那樣,問題在于 Android 的 Indy10 方法已損壞,需要使用以下 Dave Nottage 的解決方法。(如果你還想獲得 Indy 方法應該回傳的 NetMask,你需要進一步的作業。如果/當我把它弄出來時,我會在這里發布我的解決方案作為答案)
function getLocalIP: string;
begin
Result := '';
try
var IPList := TIdStackLocalAddressList.Create;
try
TIdStack.IncUsage;
try
GStack.GetLocalAddressList(IPList);
finally
TIdStack.DecUsage;
end;
WriteLog('DEBUG', 'No of Addresses: ' IntToStr(IPList.Count));
WriteLog('DEBUG', 'IPv4 Addresses:');
var IPStrings := TStringList.Create;
try
for var i in IPList do
begin
if TIdStackLocalAddressIPv4(i).IPVersion = Id_IPv4 then
begin
IPStrings.Add(TIdStackLocalAddressIPv4(i).IPAddress ' - ' TIdStackLocalAddressIPv4(i).SubNetMask
' - ' TIdStackLocalAddressIPv4(i).InterfaceIndex.ToString);
end;
end;
// show IP Addresses in the log file
for var i := 0 to IPStrings.Count-1 do
WriteLog('DEBUG', 'IP Address #' IntToStr(i) ': ' IPStrings[i]);
Result := IPStrings[0].Split([' - '])[0];
WriteLog('DEBUG', 'IP: ' Result);
finally
IPStrings.Free;
end;
finally
IPList.Free;
end;
except
On E: Exception do
begin
Result := '';
WriteLog('ERROR', 'IP Error: ' E.message);
end;
end;
end;
uj5u.com熱心網友回復:
根據這個要點,但在這里重復:
interface
uses
IdStack;
procedure GetLocalAddressList(const AAddresses: TIdStackLocalAddressList);
implementation
uses
System.SysUtils,
Androidapi.JNI.Java.Net, Androidapi.JNI.JavaTypes, Androidapi.Helpers, Androidapi.JNIBridge,
IdGlobal;
procedure GetLocalAddressList(const AAddresses: TIdStackLocalAddressList);
var
LInterfaces, LAddresses: JEnumeration;
LInterface: JNetworkInterface;
LAddress: JInetAddress;
LInet4Address: JInet4Address;
LInet6Address: JInet6Address;
LName, LHostAddress: string;
begin
AAddresses.Clear;
LInterfaces := TJNetworkInterface.JavaClass.getNetworkInterfaces;
while LInterfaces.hasMoreElements do
begin
LInterface := TJNetworkInterface.Wrap(JObjectToID(LInterfaces.nextElement));
LAddresses := LInterface.getInetAddresses;
while LAddresses.hasMoreElements do
begin
LAddress := TJInetAddress.Wrap(JObjectToID(LAddresses.nextElement));
if LAddress.isLoopbackAddress then
Continue;
// Hack until I can find out how to check properly
LName := JStringToString(LAddress.getClass.getName);
LHostAddress := JStringToString(LAddress.getHostAddress);
// Trim excess stuff
if LHostAddress.IndexOf('%') > -1 then
LHostAddress := LHostAddress.Substring(0, LHostAddress.IndexOf('%'));
if LName.Contains('Inet4Address') then
TIdStackLocalAddressIPv4.Create(AAddresses, LHostAddress, '')
else if LName.Contains('Inet6Address') then
TIdStackLocalAddressIPv6.Create(AAddresses, LHostAddress);
end;
end;
end;
使用風險自負
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/513243.html
標籤:安卓德尔福印地10
