Android 给Wifi模块配网

private void onWifiChanged(WifiInfo info) {
        mInfo =info;
        if (info == null) {
            mApSsidTV.setText("");
            mApSsidTV.setTag(null);


            if (mTask != null) {
                mTask.cancelEsptouch();
                mTask = null;
                new AlertDialog.Builder(WifiAddActivity.this)
                        .setMessage("Wifi disconnected or changed")
                        .setNegativeButton(android.R.string.cancel, null)
                        .show();
            }
        } else {
            String ssid = info.getSSID();
            if (ssid.startsWith("\"") && ssid.endsWith("\"")) {
                ssid = ssid.substring(1, ssid.length() - 1);
            }
            mApSsidTV.setText(ssid);
            mApSsidTV.setTag(ByteUtil.getBytesByString(ssid));
            byte[] ssidOriginalData = EspUtils.getOriginalSsidBytes(info);
            mApSsidTV.setTag(ssidOriginalData);

//            String bssid = info.getBSSID();
//            mApBssidTV.setText(bssid);

          //  mConfirmBtn.setEnabled(true);
           // mMessageTV.setText("");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                int frequence = info.getFrequency();
                if (frequence > 4900 && frequence < 5900) {
                    // Connected 5G wifi. Device does not support 5G
                  //  mMessageTV.setText(R.string.wifi_5g_message);
                }
            }
        }
    }

开启WIFI:

 

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        WifiManager wifi =(WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        wifi.setWifiEnabled(true);
    }
});
关闭WIFI:

 

 

button2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        wifi.setWifiEnabled(false);
    }
});
获得WIFI状态:

 

 

button3.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        int wifiState = wifi.getWifiState();
        Toast.makeText(mContext, "状态是"+wifiState, Toast.LENGTH_SHORT).show();
    }
});
最后记得要在注册文件中加上权限

 

 




你可能感兴趣的:(Android 给Wifi模块配网)