gpt4 book ai didi

android - 问题 : Android's isConnected() used to get current state of WiFi often returns false even when the device is connected

转载 作者:行者123 更新时间:2023-11-29 22:30:11 24 4
gpt4 key购买 nike

我的 Android 应用程序只能在连接到互联网的 WiFi 下运行。因此,我使用以下代码来检查设备是否已连接:

ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE);
boolean wifi = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();

但是,当应用程序启动并且 WiFi 连接到 Internet 时,我经常收到仅在 wifi = false 时显示的通知。我是不是遗漏了什么,或者支票不够准确?

最佳答案

我的项目也依赖 Wifi(尽管我使用的是专用网络)。以下是我在启动时设置 Wifi 连接的代码:

private void initWIFI (WifiManager wifiMgr, String SSID, String key)
{
WifiInfo curr;
if (null == (curr = wifiMgr.getConnectionInfo())) // Get current wifi state
{
joinNetwork (wifiMgr, SSID, key);
}
else switch (curr.getSupplicantState())
{
case DISCONNECTED:
case DORMANT:
case INACTIVE:
case SCANNING:
joinNetwork (wifiMgr, SSID, key);
break;

default:
if (!curr.getSSID().equals (SSID))
joinNetwork (wifiMgr, SSID, key);
}

while (wifiMgr.getConnectionInfo().getIpAddress() == 0)
{
try
{
Thread.sleep (1000);
}
catch (Exception e)
{ }
}
}

/**This method is used to join the proper WiFi network when necessary. Normally,
* the Android retains network configuration and it is not necessary to manually
* re-join the desired network on software startup. However, when it is determined
* that the Android is not currently attached to the proper network, this function
* is used to correct that situation. */
private void joinNetwork (WifiManager wifiMgr, String SSID, String key)
{
try
{
WifiConfiguration wc = new WifiConfiguration();

wc.allowedAuthAlgorithms.set (WifiConfiguration.AuthAlgorithm.OPEN);
wc.allowedAuthAlgorithms.set (WifiConfiguration.AuthAlgorithm.SHARED);

wc.allowedGroupCiphers.set (WifiConfiguration.GroupCipher.WEP40);
wc.allowedGroupCiphers.set (WifiConfiguration.GroupCipher.WEP104);

wc.allowedKeyManagement.set (WifiConfiguration.KeyMgmt.NONE);

wc.allowedPairwiseCiphers.set (WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set (WifiConfiguration.PairwiseCipher.CCMP);

wc.allowedProtocols.set (WifiConfiguration.Protocol.WPA);
wc.allowedProtocols.set (WifiConfiguration.Protocol.RSN);

wc.hiddenSSID = false;
wc.priority = 32;

wc.SSID = "\"" + SSID + "\"";
wc.status = WifiConfiguration.Status.ENABLED;

wc.wepKeys[0] = key;
wc.wepTxKeyIndex = 0;

int netID;
if (-1 == (netID = wifiMgr.addNetwork (wc)))
{
listener.lostConnection (true);
}
else
{
wifiMgr.enableNetwork (netID, true);
Thread.sleep (5000); // Delay to allow the DHCP process to work
}
}
catch (Exception e)
{
listener.lostConnection (true);
}
}

需要指出的是,我一直使用同一个无线接入点,joinNetwork()中的代码是专门为它配置的,所以如果你的配置需要更灵活,那么你的解决方案可能会更复杂。遗憾的是,我不记得我在哪个网站上找到了这段代码的起点,但我并不需要大量的谷歌搜索就可以找到它。最后,我非常确定您的应用程序需要具有 ACCESS_WIFI_STATE 和 CHANGE_WIFI_STATE 权限。

关于android - 问题 : Android's isConnected() used to get current state of WiFi often returns false even when the device is connected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4324986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com