13

我目前正在将我的应用程序迁移到 Android Q。我想列出所有已配置的 WiFi 网络,并且在 QI 能够使用WiFiManager 中的函数getConfiguredNetworks之前列出。遗憾的是,此方法在 API 级别 29 上已弃用,并在 Android Q 设备上返回一个空列表。

弃用评论仅指我也想连接到这些网络的情况。我不想这样做,我只想列出网络及其名称并获取它们的 internal id。你有什么想法我应该如何在 Q 中做到这一点?

4

2 回答 2

0

我想没有办法在 Android Q 及更高版本中获得配置的 WiFi 网络。唯一的方法是获取当前可用的网络,getScanResults()我知道这实际上并不能解决获取配置的网络名称的问题。而是只获取当前可用的网络。

    List<ScanResult> results = null;
    TextView outputs = (TextView)findViewById(R.id.outputs);
    try{
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
        results = wifiManager.getScanResults();
        int len = results.size();
        String res = null;
        for (int i = 0; i < len; i++) {
            res = outputs.getText() + "\n" + results.get(i).SSID;
            outputs.setText(res);

        }

    }
    catch(Exception e)
    {
        Log.e("MainActivity",e.getMessage());
    }

目前,他们仅根据其文档提供触发与 WiFi 网络连接的机制。让我们希望有什么事情发生或不发生

于 2019-08-08T15:52:13.320 回答
0

我有同样的问题,我的问题是我有 30 的 mi targetSdkVersion!并且函数 getConfiguredNetworks 在 api 29 中已被弃用,当我在 android 9 或以下设备中运行我的应用程序时,效果很好,但是当我在 android 10 中运行我的应用程序时,函数 getConfiguredNetworks 返回 null 或空列表,我的解决方案是更改我的 targetSdkVersion到28,像这样

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.0"

    defaultConfig {
        applicationId "asdaadsda"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

参考资料:https://developer.android.com/reference/android/net/wifi/WifiManager#getConfiguredNetworks()

于 2020-07-16T23:32:27.337 回答