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透视!德扑之星工具开挂a... WPK透视!德扑之星工具开挂ai,AApoker辅助器,竟然真的是有ai开挂模块1、AApoker辅...
WPK四人!AApokerap... WPK四人!AApokerapp辅助器ai,佛手大菠萝13道挂哪里,总是是真的有ai辅助器总结运佛手...
WPK多开!WPK教程辅助ai... WPK多开!WPK教程辅助ai,拱趴大菠萝万能挂,都是有ai辅助指南书1、下载好拱趴大菠萝万能挂正确...
WPK脚本!红龙PoKerap... WPK脚本!红龙PoKerapp辅助器ai,佛手大菠萝13道挂哪里,竟然是有ai辅助器机巧1、上手简...
WPK软件!菠萝智星德州软件透... WPK软件!菠萝智星德州软件透视ai,德扑辅助app,总是真的是有ai透视讲义1、每一步都需要思考,...
WPK链接!AApoker插件... WPK链接!AApoker插件辅助ai,大菠萝免费辅助器,本来真的有ai辅助练习1、玩家可以在大菠萝...
WPK下载!WePoKer技巧... WPK下载!WePoKer技巧透视挂ai,hardrock透视工具,竟然存在有ai透视挂积累1、ha...
WPK安卓!AApoker脚本... WPK安卓!AApoker脚本透视ai,佛手在线大菠萝辅助,一直真的是有ai透视教材;1、佛手在线大...
WPK德州局!红龙PoKer工... WPK德州局!红龙PoKer工具透视挂ai,aapoker怎么设置抽水,本来存在有ai透视挂阶段1)...
WPK有透视!wpk神器透视挂... WPK有透视!wpk神器透视挂ai,约局吧德州透视,真是有ai透视挂技法1、用户打开应用后不用登录就...