我正在尝试检查PCL中移动设备上的 wifi 连接,我使用了CrossConnectivity
插件,但以下代码抛出了异常(null异常,没有描述)。
bool isConnected = CrossConnectivity.Current.IsConnected;
我想知道我做错了什么?
ViewModel.cs
using Plugin.Connectivity;
private async Task Connection()
{
Regex r = new Regex(@"^(?<proto>\w+)://[^/]+?:(?<port>\d+)?/",
RegexOptions.None, TimeSpan.FromMilliseconds(150));
Match m = r.Match(serviceURL);
try
{
if (m.Success)
{
Int32.Parse(r.Match(serviceURL).Result("${port}"));
Uri myUri = new Uri(serviceURL);
string host = myUri.Host;
bool isConnected = CrossConnectivity.Current.IsConnected;
}
catch (Exception ex)
{
ConnectionError = ex.GetUserMessage();
}
}
}
在 Android Manifest
中 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />