为什么我的android wlan0网络接口没有默认网关



我要做的是:找到安卓连接的Wi-Fi热主机的IP。

我正在使用带有c#的Unity3D,下面是我的代码来获取有关我的wlan0接口的信息:获取有关wlan0 的所有信息的代码

NetworkInterface ni = NetworkInterface.GetAllNetworkInterfaces()
.Where(n => n.OperationalStatus == OperationalStatus.Up)
.Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.FirstOrDefault();
Debug.LogWarning($"Up Interface = {ni.Name} " +
$"has {ni.GetIPProperties().GatewayAddresses.Count} gateway adresses " +
$"& has {ni.GetIPProperties().DhcpServerAddresses.Count} DHCP Adresses " +
$"& has {ni.GetIPProperties().DnsAddresses.Count} DNS adresses");
var gateways = ni.GetIPProperties()?.GatewayAddresses
.Select(g => g?.Address)
.Where(a => a != null);
foreach(var g in gateways)
Debug.LogWarning($"ip of defaut gateway = {g.MapToIPv4()}");
foreach(var d in ni.GetIPProperties()?.DhcpServerAddresses)
Debug.LogWarning($"ip of dhcp = {d.MapToIPv4()}");
foreach (var d in ni.GetIPProperties()?.DnsAddresses)
Debug.LogWarning($"ip of dns = {d.MapToIPv4()}");

当我在Pico VR耳机上启动此代码时,结果如下:没有网关,没有DHCP,没有DNS(是的,我的耳机连接到Wifi(

Up-Interface=wlan0有0个地址&具有0个DHCP地址&有0个DNS地址

  1. 为什么android上有0默认网关?(在PC上它正在工作,我可以找到我的Wi-Fi热点的IP(。

    1. 如何获取安卓连接的Wi-Fi热点的IP

我不知道"为什么wlan0没有默认网关">

但我已经找到了"找到android连接的Wi-Fi热主机的IP"的答案:

public string GetHotspotIP()
{
IPAddress myIP = Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault();
Debug.LogWarning($"DNS Host Adress = {myIP}");
string ipString = myIP.MapToIPv4().ToString(); //ie : 192.168.48.165
string[] ipParts = ipString.Split('.'); //ie : { "192", "168", "48", "165"}
return string.Concat(ipParts[0], ".", ipParts[1], ".", ipParts[2], ".", "1"); //ie : 192.168.48.1
}