android - 无法在 Android 5.0 上添加新的 WiFi 网络

标签 android android-wifi android-5.0-lollipop

我将此代码用于 Android <5.0,没有任何问题:

// //////////// save network
WifiConfiguration wc = new WifiConfiguration();
ConfigurationSecuritiesV8 conf = new ConfigurationSecuritiesV8();
conf.setupSecurity(wc, conf.getScanResultSecurity(scanResult), password);
wc.BSSID = scanResult.BSSID;
wc.SSID = "\"" + scanResult.SSID.replace("\"", "") + "\"";

int id = wifiManager.addNetwork(wc);
if (id == -1)
    return RESULT_CANT_CREATE_NETWORK;

我刚刚在我的 Nexus 5 上安装了新的 Android 5.0,现在我无法以编程方式添加网络。

最佳答案

我发现了问题,我需要将我的代码更改为

if (Build.VERSION.SDK_INT >= 21)
    wc.SSID = "" + scanResult.SSID.replace("\"", "") + "";
else
    wc.SSID = "\"" + scanResult.SSID.replace("\"", "") + "\"";

关于android - 无法在 Android 5.0 上添加新的 WiFi 网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26982253/

相关文章:

android - JNI 在应用程序中检测到错误 : Null pointer in Mapbox android SDK

android - 我在哪里可以获得适用于 ADT 的功能性 Wi-Fi Direct 演示 Android 应用程序项目?

android - 关于 Android "Attribute elevation is only used in API level 21 and higher"的说明

android - 有没有办法在多个嵌套的 RecyclerView 之间共享同一个 LayoutManager

android - 数字格式 Android 5.0

android - rxJava,定期刷新 api 数据

java - 什么是 .CONTENT_URI

android - Kotlin 中的 Room Persistence lib 实现(Gradle 错误)

android - 如果 targetSdkVersion >= 26,WifiManager.WifiInfo getSSID 和 getBSSID 不工作

Android 6.0.1 无法以编程方式启用 wifi 热点