如何针对Android Things 1.0.2以编程方式连接到wifi

时间:2018-08-09 13:43:26

标签: xamarin.forms android-wifi android-things

我正在尝试通过我们部署在Android Things 1.0.2上的应用程序以编程方式连接到Wifi网络。我遵循了turorial(https://spin.atomicobject.com/2018/02/15/connecting-wifi-xamarin-forms/)。 wifi设置的代码如下:

var ssid = "linksys";
var password = "123";
var wifiManager = (WifiManager)Android.App.Application.Context
                        .GetSystemService(Context.WifiService);
var formattedSsid = $"\"{ssid}\"";
var formattedPassword = $"\"{password}\"";
wifiManager.EnableNetwork(true);
var wifiConfig = new WifiConfiguration
            {
                Ssid = formattedSsid,
                PreSharedKey = formattedPassword
            };
var addNetwork = wifiManager.AddNetwork(wifiConfig);
var network = wifiManager.ConfiguredNetworks
                 .FirstOrDefault(n => n.Ssid == ssid);

if (network == null)
   {
     Console.WriteLine($"Cannot connect to network: {ssid}");
     return;
   }

wifiManager.Disconnect();
var enableNetwork = wifiManager.EnableNetwork(network.NetworkId, true);

但是当我尝试在“ WifiManager”上获取“ ConfiguredNetworks”时,结果为空。我还尝试手动授予两个android权限

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

通过adb命令,但出现“权限不可更改”错误。谢谢您的提前帮助。

0 个答案:

没有答案