添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

WifiNetworkSuggestion官方文档

一.只使用WifiNetworkSpecifier(可直接连接到指定wifi,但会几率出现弹出连接失败框-实际是连接成功的情况)

private void connectWifi(String ssid, String pwd) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
        WifiNetworkSpecifier wifiNetworkSpecifier = new WifiNetworkSpecifier.Builder()
                .setSsid(ssid)
                .setWpa2Passphrase(pwd)
                .build();
        NetworkRequest request = new NetworkRequest.Builder()
                .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                .setNetworkSpecifier(wifiNetworkSpecifier)
                .build();
        mConnectivityManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
        mNetworkCallback = new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(@NonNull Network network) {
                super.onAvailable(network);
            @Override
            public void onUnavailable() {
                super.onUnavailable();
        mConnectivityManager.requestNetwork(request, mNetworkCallback);
    } else {
@Override
public void destroy() {
    if (mConnectivityManager != null && mNetworkCallback != null) {
        mConnectivityManager.unregisterNetworkCallback(mNetworkCallback);

二.WifiNetworkSuggestion + WifiNetworkSpecifier (思路:先添加建议,再连接,成功率高)

private void connectWifi(String ssid, String pwd) {
        // andorid 10以上
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
        //step1-创建建议列表
            WifiNetworkSuggestion suggestion =
                    new WifiNetworkSuggestion.Builder()
                            .setSsid(ssid)
                            .setWpa2Passphrase((pwd))
                            .setIsAppInteractionRequired(true)
                            .build();
            List<WifiNetworkSuggestion> suggestionsList = new ArrayList<>();
            suggestionsList.add(suggestion);
            WifiManager wifiManager = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            int status = wifiManager.addNetworkSuggestions(suggestionsList);
            // step2-添加建议成功
            if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
                WifiNetworkSpecifier wifiNetworkSpecifier = new WifiNetworkSpecifier.Builder()
                        .setSsid(ssid)
                        .setWpa2Passphrase(pwd)
                        .build();
                NetworkRequest request = new NetworkRequest.Builder()
                        .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                        .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                        .setNetworkSpecifier(wifiNetworkSpecifier)
                        .build();
                mConnectivityManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
                mNetworkCallback = new ConnectivityManager.NetworkCallback() {
                    @Override
                    public void onAvailable(@NonNull Network network) {
                        super.onAvailable(network);
                    @Override
                    public void onUnavailable() {
                        super.onUnavailable();
                // step3-连接wifi
                mConnectivityManager.requestNetwork(request, mNetworkCallback);
        } else {}

WifiNetworkSuggestion是给系统添加一个wifi建议,手动关闭再打开wifi开关后会优先连接你建议的wifi,但是他不会主动发起连接。且只能被创建建议的应用使用。 WifiNetworkSpecifier是主动连接,但是只使用它去连接时,会弹出系统连接失败的框(实际上关闭框后时连接成功的),配合WifiNetworkSuggestion一起使用效果较好。

连接指定wifi后,如果此wifi消失,系统不会自动连接到原有wifi。 解决:取消注册后,即可自动恢复到之前的wifi

mConnectivityManager.unregisterNetworkCallback(mNetworkCallback);

五.wifi连接后需要通过Socket与设备通信的可以看第二篇文章

Android10+ Wifi连接后Socket通信(二)

分类:
Android
  •