【问题标题】:add new network to android device programsticly以编程方式将新网络添加到 android 设备
【发布时间】:2017-06-22 20:27:00
【问题描述】:

我需要使用以下代码以编程方式将新的 wifi 网络连接到我的设备: `

 try 
{
    String ssid = "\"" + SSID + "\"";
    String pass = "\"" + Pass + "\"";

    for (ScanResult result : results) 
    {
        if (result.SSID.equals(SSID)) 
        {  
            String security = getScanResultSecurity(result);
            if (security.equals("PSK")) {

                   WifiConfiguration con = new WifiConfiguration();
                   con.SSID = ssid;
                   AlertDialog a = new AlertDialog.Builder(MainActivity.this).create();
                   a.setMessage("in");
                   a.show();
                   con.preSharedKey = pass;
                   con.hiddenSSID = true;
                    con.status = WifiConfiguration.Status.ENABLED;
                    con.allowedGroupCiphers.set(WifiConfiguration.Grou  pCipher.TKIP);
                    con.allowedGroupCiphers.set(WifiConfiguration.Grou  pCipher.CCMP);
                    con.allowedKeyManagement.set(WifiConfiguration.Key  Mgmt.WPA_PSK);
                    con.allowedPairwiseCiphers.set(WifiConfiguration.P  airwiseCipher.TKIP);
                    con.allowedPairwiseCiphers.set(WifiConfiguration.P  airwiseCipher.CCMP);
                    con.allowedKeyManagement.set(WifiConfiguration.Key  Mgmt.NONE);
                    con.allowedProtocols.set(WifiConfiguration.Protoco  l.RSN);
                    con.allowedProtocols.set(WifiConfiguration.Protoco  l.WPA);

                    int ntid = wifimanager.addNetwork(con);

                    wifimanager.disconnect();
                    wifimanager.enableNetwork(ntid,true);
                    wifimanager.reconnect();
                    boolean b = wifimanager.saveConfiguration();
                    if (ntid != -1 && b) {

                        AlertDialog a2 = new AlertDialog.Builder(MainActivity.this).create();
                        a2.setMessage("saved");
                        a2.show();
                    }
                }

        }
    }
    catch (Exception ex) {
        AlertDialog a = new AlertDialog.Builder(MainActivity.this).create();
        a.setMessage(ex.getMessage());
        a.show();
    }

`
但我无法将网络添加到我的设备 问题在于无法添加到网络的网络配置 我不明白为什么不起作用没有错误但没有结果 请帮帮我

【问题讨论】:

  • 你的日志是怎么说的?
  • 我听不懂很多信息,最后失败了
  • 这是日志 mStopped=false mHashWindowFocus=true sendUserActionEvent() mView=null 并且输入事件接收器已经被释放

标签: java android networking connection wifi


【解决方案1】:

您应该将权限添加到清单中(和运行时权限)

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

【讨论】:

  • 我知道没有问题的权限
猜你喜欢
  • 1970-01-01
  • 2010-12-10
  • 1970-01-01
  • 2020-07-23
  • 2020-06-04
  • 1970-01-01
  • 1970-01-01
  • 2011-07-04
  • 1970-01-01
相关资源
最近更新 更多