WIFI 简单的连接及常用密码破解

WIFI所需权限及配置<uses-permissionandroid:name="android.permission.INTERNET"/><uses-permissionandroid:name="android.permission.CHANGE_WIFI_STATE"/><u

大家好,又见面了,我是你们的朋友全栈君。

WIFI所需权限及配置

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

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

    <!--使wifi在息屏情况下不断wifi,有需要的话-->
    <uses-permission android:name="android.permission.WAKE_LOCK" />

开启WIFI

mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
mWifiManager.setWifiEnabled(true);//开启wifi

扫描周围WIFI

//Todo 没有mWifiManager.startScan()就可以直接getScanResults,我试过好像23版本以下就不行了,你的测试机型应该也要25以上吧
//23以下有适配问题,比如startScan之后并不能直接getScanResults(),这样得不到数据
List<ScanResult> mScanResultList = mWifiManager.getScanResults();

连接WIFI

核心连接WiFi代码(可以换不同的密码尝试)
String usePassword = "password"; 
int netId = mWifiManager.addNetwork(createWifiConfig(scanResult.SSID, usePassword, WIFICIPHER_WPA));
boolean enable = mWifiManager.enableNetwork(netId, true);
//ssid为wifi名,password为wifi密码,type为wifi连接使用的加密算法(一般为WPA)
private WifiConfiguration createWifiConfig(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 = isExist(ssid);
        if (tempConfig != null) {
            mWifiManager.removeNetwork(tempConfig.networkId);
        }

        if (type == WIFICIPHER_NOPASS) {
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        } else if (type == WIFICIPHER_WEP) {
            config.hiddenSSID = true;
            config.wepKeys[0] = "\"" + password + "\"";
            config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
            config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            config.wepTxKeyIndex = 0;
        } else if (type == 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.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            config.status = WifiConfiguration.Status.ENABLED;
        }

        return config;
    }

WIFIDome

wifi状态描述类 SupplicantState

ASSOCIATED

关联完成。

ASSOCIATING

开始关联到一个BSS或SSID。

COMPLETED

所有用户认证已完成。

DISCONNECTED

当前接入点不可关联,有可能开始下一个关联。

DORMANT

Android特有,当用户明确发出中断指令。

FOUR_WAY_HANDSHAKE

WPA 4次握手完成。

GROUP_HANDSHAKE

WPA group 握手完成。

INACTIVE

处于非活动状态(wpa_supplicant被禁用)。

INVALID

伪状态,通常不应被看到。

SCANNING

正在扫描网络。

UNINITIALIZED

还未连接到wpa_supplicant

wifi连接时如何知晓它连接的状态(如这个wifi密码错误)

 //这个广播在我手机并不能及时反应这个wifi为不能连接状态(手机可能屏蔽部分状态)
 //这个判断标准对于我的手机并不准
 private void registerBroadcastReceiver() {
        mBroadcastReceiver = new BroadcastReceiver() { 
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
                    int message = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, -1);
                    switch (message) {
                        case WifiManager.WIFI_STATE_DISABLED:
                            //Toast.makeText(getApplicationContext(), "不可用", Toast.LENGTH_SHORT).show();
                            break;
                        case WifiManager.WIFI_STATE_DISABLING:
                            //Toast.makeText(getApplicationContext(), "正在关闭或者断开", Toast.LENGTH_SHORT).show();
                            break;
                        case WifiManager.WIFI_STATE_ENABLED:
                            //Toast.makeText(getApplicationContext(), "可用", Toast.LENGTH_SHORT).show();
                            break;
                        case WifiManager.WIFI_STATE_ENABLING:
                            //Toast.makeText(getApplicationContext(), "正在打开或者连接", Toast.LENGTH_SHORT).show();
                            break;
                        case WifiManager.WIFI_STATE_UNKNOWN:
                            //Toast.makeText(getApplicationContext(), "未知", Toast.LENGTH_SHORT).show();
                            break;
                        default:
                            break;
                    }
                } else if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
//                    WifiInfo wifiInfo = mWifiManager.getWifiInfo();
                    WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
                    SupplicantState state = wifiInfo.getSupplicantState();
                    String str = "";
                    if (state == SupplicantState.SCANNING) {
                        str = "正在扫描";
                    }
                    if (state == SupplicantState.ASSOCIATED) {
                        str = "关联AP成功";
                    } else if (state.toString().equals("AUTHENTICATING")) {
                        str = "正在验证";
                    } else if (state == SupplicantState.ASSOCIATING) {
                        str = "正在关联AP...";
                    } else if (state == SupplicantState.COMPLETED) {
                        str = "连接成功";
                    } else if (state == SupplicantState.INACTIVE) {

                    } else if (state.toString().equals("DISCONNECTED")) {//连接失败
                        str = "连接不成功";
                        Log.e(TAG, "mConnectCount: " + mConnectCount);
                        if (mConnectCount < 1)
                            mConnectCount++;
                        else {
                            is_password_error = true;
                            mConnectCount = 0;
//                            Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                    //Toast.makeText(getApplicationContext(),str, Toast.LENGTH_SHORT).show();
                    Log.e(TAG, "连接状态: "+str );
                }
                int linkWifiResult = intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, 123);
                Log.e(TAG, "wifi_link_error" + linkWifiResult); //大多数返回123
                if (linkWifiResult == WifiManager.ERROR_AUTHENTICATING) {
                    is_password_error = true; //监听到连接wifi验证错误,改变flag,认为是密码错误
                }
            }
        };

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); //监听网络状态改变
        intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
        registerReceiver(mBroadcastReceiver, intentFilter);
    }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/129092.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号