Android WiFi Manager的enableNetwork方法返回true,但NetworkInfo的状态始终为DISCONNECTED/SCANNING。
创始人
2024-08-19 11:00:47
0次

要解决这个问题,你可以尝试以下几个步骤:

  1. 确保你已经获取了正确的网络ID(networkId),并且已经连接到了正确的WiFi网络。

  2. 使用WifiManager的disconnect()方法断开当前的WiFi连接。

  3. 使用enableNetwork()方法连接到目标WiFi网络。

  4. 注册一个BroadcastReceiver来监听WiFi连接状态的变化。

下面是一个可能的解决方案的代码示例:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

public class WiFiHelper {
    private Context mContext;
    private WifiManager mWifiManager;
    private BroadcastReceiver mWifiReceiver;

    public WiFiHelper(Context context) {
        mContext = context;
        mWifiManager = (WifiManager) mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        initWifiReceiver();
    }

    private void initWifiReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        mWifiReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
                    NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
                    if (networkInfo != null && networkInfo.isConnected()) {
                        // WiFi已连接
                        // 在这里可以进行一些操作,比如获取IP地址等
                    }
                }
            }
        };
        mContext.registerReceiver(mWifiReceiver, intentFilter);
    }

    public void connectToWiFi(String ssid, String password) {
        // 获取WiFi网络ID
        int networkId = getNetworkId(ssid);
        if (networkId != -1) {
            // 断开当前的WiFi连接
            mWifiManager.disconnect();
            // 连接到目标WiFi网络
            boolean success = mWifiManager.enableNetwork(networkId, true);
            if (success) {
                // 连接成功
            } else {
                // 连接失败
            }
        } else {
            // 未找到目标WiFi网络
        }
    }

    private int getNetworkId(String ssid) {
        int networkId = -1;
        for (WifiConfiguration config : mWifiManager.getConfiguredNetworks()) {
            if (config.SSID != null && config.SSID.equals("\"" + ssid + "\"")) {
                networkId = config.networkId;
                break;
            }
        }
        return networkId;
    }

    public void unregisterWifiReceiver() {
        if (mWifiReceiver != null) {
            mContext.unregisterReceiver(mWifiReceiver);
        }
    }
}

这里我们创建了一个WiFiHelper类来管理WiFi连接,其中connectToWiFi()方法用于连接到目标WiFi网络。该方法会先断开当前的WiFi连接,然后调用enableNetwork()方法连接到目标网络。同时,我们还注册了一个BroadcastReceiver来监听WiFi连接状态的变化,当连接成功时会执行相应的操作。

你可以根据你的需求进行适当的修改和调整。

相关内容

热门资讯

WPK德州局窍要!wepoke... WPK德州局窍要!wepoker辅助器怎么用,德州微扑克好像是真的有透视挂方法1、wepoker辅助...
WPK下载窍门!aapoker... WPK下载窍门!aapoker辅助挂,德州微扑克原来是真的有开挂插件aapoker辅助挂能透视中分为...
WPK透视大纲!wepoker... WPK透视大纲!wepokerplus外挂,德州微扑克本来有辅助app1、下载好wepokerplu...
WPK安装策略!wepoker... WPK安装策略!wepoker透视脚本免费,德州微扑克都是是真的有开挂方法1、许多玩家不知道wepo...
WPK私人局要领!wepoke... WPK私人局要领!wepoker私人辅助器,德州微扑克竟然有透视工具1、许多玩家不知道wepoker...
WPK德州局诀窍!WePOke... WPK德州局诀窍!WePOker有没有透视方法,德州微扑克其实真的有透视app在进入WePOker有...
WPK有透视资料!we-pok... WPK有透视资料!we-poker辅助器,德州微扑克原来存在有开挂攻略1、操作简单,无需we-pok...
WPK透视操作!wepoker... WPK透视操作!wepoker透视脚本视频,德州微扑克一贯存在有辅助器工具1、该软件可以轻松地帮助玩...
WPK脚本窍门!wepoker... WPK脚本窍门!wepoker黑侠破解,德州微扑克好像是真的有辅助app1、操作简单,无需wepok...
WPK作弊手筋!wepoker... 您好,wepoker怎么设置房间这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...