我遇到了一个奇怪的问题。我想定期获取Wifi信号条并更新一个小显示屏。我已经尝试在代码中运行它并使用API。两者都会运行一段时间,但过了一段时间,也许30分钟Iot默认应用程序显示没有网络信息。如果我进入设置并单击“网络和Wi-Fi”,设备将挂起并需要进行硬重置。
我想也许我扫描的速度太快,所以我把计时器更改为每一分钟,但仍然有这个问题。这可能是我的方法的一个问题,也许有一种更清晰的方法来获取这些信号信息而无需扫描?我在Raspberry Pi3上运行Windows Iot Core v.10.0.16299.309。这是我的代码。
从Run
调用ThreadPoolTimer UpdateWifiSignalTimer = ThreadPoolTimer.CreatePeriodicTimer((source) =>
{
WifiSignal();
}, TimeSpan.FromMinutes(1));
尝试1
if (WiFiAdapter != null)
{
networkAddapterId = WiFiAdapter.NetworkAdapter.NetworkAdapterId.ToString();
await WiFiAdapter.ScanAsync();
if (WiFiAdapter.NetworkAdapter.GetConnectedProfileAsync() != null)
{
var connectedProfile = await WiFiAdapter.NetworkAdapter.GetConnectedProfileAsync();
if (connectedProfile != null && !connectedProfile.ProfileName.Equals(savedProfileName))
{
savedProfileName = connectedProfile.ProfileName;
wifiProfile.Add( new WifiProfile { MacAddress = networkAddapterId, Ssid = savedProfileName }) ;
}
}
var wifiPoint = new List<WiFiSignal>();
WiFiNetworkReport report = WiFiAdapter.NetworkReport;
foreach (var availableNetwork in report.AvailableNetworks)
{
WiFiSignal wifiSignal = new WiFiSignal()
{
MacAddress = availableNetwork.Bssid,
Ssid = availableNetwork.Ssid,
SignalBars = availableNetwork.SignalBars,
ChannelCenterFrequencyInKilohertz = availableNetwork.ChannelCenterFrequencyInKilohertz,
NetworkKind = availableNetwork.NetworkKind.ToString(),
PhysicalKind = availableNetwork.PhyKind.ToString(),
Encryption = availableNetwork.SecuritySettings.NetworkEncryptionType.ToString()
};
wifiPoint.Add(wifiSignal);
}
int bars = 0;
foreach (var localNetworks in wifiPoint)
{
if (localNetworks.Ssid == savedProfileName)
{
bars = localNetworks.SignalBars;
display.ClearDisplayBuf();
}
}
尝试2
using (HttpClient client = new HttpClient())
{
if (interfaceGUID == null)
{
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, string.Format("http://{0}:8080/api/wifi/interfaces", DeviceName)))
{
request.Headers.Authorization = CreateBasicCredentials("Administrator", PASSWORD);
using (HttpResponseMessage response = await client.SendAsync(request))
{
var data = await response.Content.ReadAsStringAsync();
var interfaces = JsonConvert.DeserializeObject<AvailableInterfaces>(data);
var ourInterface = interfaces.Interfaces.First();
interfaceGUID = ourInterface.GUID.Replace('{', ' ').Replace('}', ' ').Trim();
}
}
}
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, string.Format("http://{0}:8080/api/wifi/networks?interface={1}", DeviceName, interfaceGUID)))
{
request.Headers.Authorization = CreateBasicCredentials("Administrator", PASSWORD);
using (HttpResponseMessage response = await client.SendAsync(request))
{
var data = await response.Content.ReadAsStringAsync();
var networks = JsonConvert.DeserializeObject<AvailableNetworksObject>(data);
this.Networks = networks.AvailableNetworks;
//this.Status = "Refresh Wifi Succeeded";
}
}
foreach (var item in this.Networks)
{
if (item.AlreadyConnected == true)
{
display.ClearDisplayBuf();
display.WriteLineDisplayBuf(item.SignalQuality.ToString(), 0, 0);
display.DisplayUpdate();
}
}
}