android开发连接wifi addNetwork 返回-1

如下代码:(注释掉的是会返回-1的代码,未注释的是能够正常连接wifi的代码)

//     public WifiConfiguration CreateWifiInfo(String SSID, String Password,int Type) {
//         WifiConfiguration config = new WifiConfiguration();
//         config.allowedAuthAlgorithms.clear();
//         config.allowedGroupCiphers.clear();
//         config.allowedKeyManagement.clear();
//         config.allowedPairwiseCiphers.clear();
//         config.allowedProtocols.clear();
////         config.SSID = "\"" + SSID + "\"";
//         config.SSID = SSID;
//
//         WifiConfiguration tempConfig = this.IsExsits(SSID);
//         if (tempConfig != null) {
//             mWifiManager.removeNetwork(tempConfig.networkId);
//         }
//
//         if (Type == 1) // WIFICIPHER_NOPASS
//         {
//             config.wepKeys[0] = "";
//             config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
//             config.wepTxKeyIndex = 0;
//         }
//         if (Type == 2) // WIFICIPHER_WEP
//         {
//             config.hiddenSSID = true;
////             config.wepKeys[0] = "\"" + Password + "\"";
//             config.wepKeys[0] = Password;
//             config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
//             config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
//             config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
//             config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
//             config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
//             config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
//             config.wepTxKeyIndex = 0;
//         }
//         if (Type == 3) // WIFICIPHER_WPA
//         {
////             config.preSharedKey = "\"" + Password + "\"";
//             config.preSharedKey =Password;
//             config.hiddenSSID = true;
//             config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
//             config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
//             config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
//             config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
//             // config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
//             config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
//             config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
//             config.status = WifiConfiguration.Status.ENABLED;
//         }
//         return config;
//     }
    public WifiConfiguration CreateWifiInfo(String SSID, String Password,
            int Type) {
        WifiConfiguration config = new WifiConfiguration();
        config.allowedAuthAlgorithms.clear();
        config.allowedGroupCiphers.clear();
        config.allowedKeyManagement.clear();
        config.allowedPairwiseCiphers.clear();
        config.allowedProtocols.clear();
        config.SSID = "\"" + SSID + "\"";

        WifiConfiguration tempConfig = this.IsExsits(SSID);
        if (tempConfig != null) {
            mWifiManager.removeNetwork(tempConfig.networkId);
        }

        if (Type == 1) // WIFICIPHER_NOPASS
        {
            config.wepKeys[0] = "";
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            config.wepTxKeyIndex = 0;
        }
        if (Type == 2) // WIFICIPHER_WEP
        {
            config.hiddenSSID = true;
            config.wepKeys[0] = "\"" + Password + "\"";
            config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            config.wepTxKeyIndex = 0;
        }
        if (Type == 3) // WIFICIPHER_WPA
        {
            config.preSharedKey = "\"" + Password + "\"";
            config.hiddenSSID = true;
            config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            // config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            config.status = WifiConfiguration.Status.ENABLED;
        }
        return config;
    }

有知道为什么ssid和pwd需要加上双引号的大侠请在下面加个评论,感谢!

时间: 2024-11-06 10:00:38

android开发连接wifi addNetwork 返回-1的相关文章

Android代码连接Wifi时被系统切换到其他Wifi的问题

首先说下Android代码连接Wifi的几个步骤:(以下涉及到具体API函数自查哈,写的时候凭借印象大致写了下) 转载请注明出处: 胖虎:http://blog.csdn.net/ljphhj 1.首先要开启Wifi连接开关,mWifiManager.setWifiEnabled(true) 2.通过获取List<ScanResult>来获取到Wifi连接列表.(mWifiManager.getScanResults) 3.获取List<WifiConfiguration>列表.(

Android自动连接WiFi优先级规则,以及查看已连接WiFi的密码

目前Android的WiFi自动连接的优先级规则如下: 1.priority值的范围设定为[0,1000000),如果超出此范围则会reset; 2.最近连接过的AP拥有最高priority,在自动连接中会首先尝试连接它; 3.未连接过但是扫描到的AP,按其信号值强弱排序,越强的显示靠前,但是,还得综合 AP的安全因素,基本情况是:WPA/WPA2 > WEP > signal level high > signal level low > noise low > noise

[android开发篇][ wifi模块] [2] wifibase

package com.softwinner.network.wifi; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration;

android 网络连接 wifi gprs的连接

package com.example.androidday15_network1; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo.State; import android.os.Bundle; import andr

Android开发中WIFI和GPRS网络的切换

在项目的开发中因为要使用到WIFI和GPRS网络的切换,因此就研究了一下通过代码打开WIFI和GPRS的工作. 无论是切换WIFI还是切换GPRS网络都需要设置相应的权限,所以需要在AndroidManifest.xml文件中加入以下几行代码. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="andro

[android开发篇][ wifi模块] [3] wifiConnectAndCheck

package com.softwinner.network.wifi; import android.content.Context; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; import android.support.test.InstrumentationRegistry; import andro

android 开发通过 wifi 无线调试(不用root)

首先保证电脑和手机在统一局域网.并知道手机的局域网 ip (以下用<DEVICE_IP_ADDRESS>代替) 1. 用usb线连接手机和电脑,并在电脑终端输入 adb tcpip 5555 adb connect <DEVICE_IP_ADDRESS> 2. 取消usb连接线即可

Android开发实践:WIFI连接功能的封装

在上一篇文章<Android开发实践:WIFI扫描功能的封装>介绍了如何利用Andriod的API实现WIFI的扫描,本文则重点讲述一下如何连接WIFI吧,在此,也给出一个封装WIFI连接过程的类,提供简单的接口以供在各个代码工程中复用. 与WIFI扫描类似,WIFI的连接同样是一个耗时的过程,所以需要放到线程中执行,通过回调来通知调用者连接结果.该回调接口的定义如下: public interface WifiConnectListener { public void OnWifiConne

【Android开发—智能家居系列】(三):手机连接WIFI模块

版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 概述 实现连接WIFI的功能会用到一个工具类,源码可以点击链接下载.网上这些类似的工具类里的代码差不多是一样的.连接无线网主要有两个方法: 其中有一个Connect方法,还有一个connectSpecificAP方法,对于不需要密码就能链接的WIFI模块,我使用的是后者. 主要步骤 连接WIFI大致分为一下几个步骤: [1]打开WIFI openWifi [2]配置网络信息 createWifiInfo返回WIFiConfi