简体   繁体   English

列出 Android Q / API 29 上的 WiFi 网络

[英]List WiFi networks on Android Q / API 29

I'm currently migrating my application to Android Q. I want to list all configured WiFi networks and before QI was able to do so with the function getConfiguredNetworks from the WiFiManager.我目前正在将我的应用程序迁移到 Android Q。我想列出所有已配置的 WiFi 网络,并且在 QI 能够使用 WiFiManager 中的getConfiguredNetworks函数执行此操作之前。 Sadly this method was deprecated on API level 29 and returns an empty list on Android Q devices.遗憾的是,此方法在 API 级别 29 上已弃用,并在 Android Q 设备上返回空列表。

The deprecation comment only refers to cases where I also want to connect to these networks.弃用评论仅指我也想连接到这些网络的情况。 I do not want to do this, I just want to list the networks with their name and get their internal id .我不想这样做,我只想列出带有名称的网络并获取其内部id Do you have any ideas how I should do this in Q?你有什么想法我应该如何在 Q 中做到这一点吗?

I guess there is No way to get Configured WiFi Networks in Android Q and Above.我想没有办法在 Android Q 及更高版本中获得配置的 WiFi 网络。 The only is to get the currently available networks using getScanResults() I know this wont actually solve the issue for getting the configured network names.唯一的方法是使用getScanResults()获取当前可用的网络,我知道这实际上并不能解决获取配置的网络名称的问题。 Instead only gets the currently available networks.而是只获取当前可用的网络。

    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());
    }

Currently they are only providing mechanism to trigger connection to a WiFi Network as per their documentations.目前,他们仅根据其文档提供触发连接到 WiFi 网络的机制。 Lets hope something comes up or not让我们希望出现与否

i had the same problem, and my problem was that I has mi targetSdkVersion in 30!我遇到了同样的问题,我的问题是我在 30 中有 mi targetSdkVersion! and the function getConfiguredNetworks was deprecated in api 29, when I run my app in a android 9 devices or below, works great but when I run my app into android 10, the function getConfiguredNetworks return null or empty list, my solution was change my targetSdkVersion to 28, like this并且函数 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'
        }
    }
}

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

dexOptions {
    javaMaxHeapSize "4g"
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM