美文网首页
Android之WIFI-WifiManager简单介绍

Android之WIFI-WifiManager简单介绍

作者: 锄禾豆 | 来源:发表于2022-02-22 10:00 被阅读0次

    WifiManager使用

    WifiManager mWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    
    1.打开wifi:
    mWifi.setWifiEnabled(false)
    
    2.扫描wifi列表:
    1)mWifi.startScan()  --- 可监听广播获取结果:android.net.wifi.SCAN_RESULTS
    mWifi.getScanResults
    
    2)也可以直接获取列表结果
    mWifi.getScanResults
    
    3.根据列表判断连接
    mWifi.connect(config, listener)
    注:
    listener回调状态结果
    
    4.关键点为WifiCongituration怎么来?
    可以根据/data/misc/wifi/wpa_supplicant.conf来处理
                public AccessPoint(ScanResult result, String password) {
    
                    config = new WifiConfiguration();
                    config.SSID = convertToQuotedString(result.SSID);
                    config.BSSID = result.BSSID;
                    config.shared = true;
    
                    int security = getSecurity(result);
                    switch (security) {
                        case SECURITY_NONE:
                            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                            break;
    
                        case AccessPoint.SECURITY_WEP:
                            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                            config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                            config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
    
                            int length = password.length();
    
                            // WEP-40, WEP-104, and 256-bit WEP (WEP-232?)
                            if ((length == 10 || length == 26 || length == 58)
                                    && password.matches("[0-9A-Fa-f]*")) {
                                config.wepKeys[0] = password;
                            } else {
                                config.wepKeys[0] = '"' + password + '"';
                            }
                            break;
                        case SECURITY_PSK:
                            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                            if (password.matches("[0-9A-Fa-f]{64}")) {
                                config.preSharedKey = password;
                            } else {
                                config.preSharedKey = '"' + password + '"';
                            }
                            break;
                        default:
                            Log.w(TAG, "security "+security+" do not supported");
                            break;
                    }
    
                    config.setIpConfiguration(getDefaultIpConfiuration());
                    Log.w(TAG, " config "+config.toString());
                }
    

    相关文章

      网友评论

          本文标题:Android之WIFI-WifiManager简单介绍

          本文链接:https://www.haomeiwen.com/subject/uawqkrtx.html