美文网首页
Android10和Android10以下版本连接热点

Android10和Android10以下版本连接热点

作者: orgcheng | 来源:发表于2020-07-14 14:17 被阅读0次
    package com.beantechs.wificonn;
    
    import android.content.Context;
    import android.content.Intent;
    import android.net.ConnectivityManager;
    import android.net.Network;
    import android.net.NetworkCapabilities;
    import android.net.NetworkRequest;
    import android.net.NetworkSpecifier;
    import android.net.wifi.WifiConfiguration;
    import android.net.wifi.WifiManager;
    import android.net.wifi.WifiNetworkSpecifier;
    import android.os.PatternMatcher;
    import android.provider.Settings;
    import android.text.TextUtils;
    
    import static android.content.Context.WIFI_SERVICE;
    
    public class WifiApUtil {
        /**
         * 创建 WifiConfiguration,这里创建的是wpa2加密方式的wifi
         *
         * @param ssid     wifi账号
         * @param password wifi密码
         * @return
         */
        public static WifiConfiguration createWifiInfo(String ssid, String password) {
            WifiConfiguration config = new WifiConfiguration();
            config.allowedAuthAlgorithms.clear();
            config.allowedGroupCiphers.clear();
            config.allowedKeyManagement.clear();
            config.allowedPairwiseCiphers.clear();
            config.allowedProtocols.clear();
            config.SSID = "\"" + ssid + "\"";
            if (TextUtils.isEmpty(password)) {
                config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                LogUtils.e("password is ''");
                return config;
            }
            config.preSharedKey = "\"" + password + "\"";
            config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            config.status = WifiConfiguration.Status.ENABLED;
            return config;
        }
    
        public interface CallBack {
            void connnectResult(boolean connectResult);
        }
    
        /**
         * 通过热点用户名和密码连接热点
         *
         * @param context
         * @param wifiApName
         * @param password
         */
        public static void connectWifiApByNameAndPwd(Context context, String wifiApName, String password, final CallBack callBack) {
            if (context == null || callBack == null) {
                LogUtils.e("context == null || callBack == null");
                return;
            }
            WifiManager mWifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
            //Andorid10.以下
    //        if (android.os.Build.VERSION.SDK_INT < 29) {
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.Q) {
                WifiConfiguration wifiNewConfiguration = createWifiInfo(wifiApName, password);//使用wpa2的wifi加密方式
                int newNetworkId = mWifiManager.addNetwork(wifiNewConfiguration);
                if (newNetworkId == -1) {
                    LogUtils.e("操作失败,需要您到手机wifi列表中取消对设备连接的保存");
                    callBack.connnectResult(false);
                    return;
                }
                LogUtils.e("newNetworkId is:" + newNetworkId);
                // 如果wifi权限没打开(1、先打开wifi,2,使用指定的wifi
                if (!mWifiManager.isWifiEnabled()) {
                    mWifiManager.setWifiEnabled(true);
                }
                boolean enableNetwork = mWifiManager.enableNetwork(newNetworkId, true);
                if (!enableNetwork) {
                    LogUtils.e("切换到指定wifi失败");
                    callBack.connnectResult(false);
                    return;
                }
                LogUtils.e("切换到指定wifi成功");
                callBack.connnectResult(true);
            } else {
                boolean isOpenWifi = mWifiManager.isWifiEnabled();
                if (!isOpenWifi) {
                    LogUtils.e("用户需要打开wifi开关");
                    context.startActivity(new Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY));
                    callBack.connnectResult(false);
                    return;
                }
                NetworkSpecifier specifier =
                        new WifiNetworkSpecifier.Builder()
                                .setSsidPattern(new PatternMatcher(wifiApName, PatternMatcher.PATTERN_PREFIX))
                                .setWpa2Passphrase(password)
                                .build();
    
                NetworkRequest request =
                        new NetworkRequest.Builder()
                                .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                                .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                                .setNetworkSpecifier(specifier)
                                .build();
    
                ConnectivityManager connectivityManager = (ConnectivityManager)
                        context.getSystemService(Context.CONNECTIVITY_SERVICE);
    
                ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
                    @Override
                    public void onAvailable(Network network) {
                        // do success processing here..
                        LogUtils.e("onAvailable success");
                        LogUtils.e("network" + network.toString());
                        callBack.connnectResult(true);
                    }
    
                    @Override
                    public void onUnavailable() {
                        // do failure processing here..
                        LogUtils.e("onUnavailable fail");
                        callBack.connnectResult(false);
                    }
                };
                connectivityManager.requestNetwork(request, networkCallback);
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Android10和Android10以下版本连接热点

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