解决Android 8.0和9.0无法获取SSID (unknow ssid)

一直都在做wifi相关的东西

今天遇到一个问题是8.0的机型无法获取到ssid

然后就查询了一番 终于找到解决方案

wifi名称获取代码

     WifiManager my_wifiManager = ((WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE));
            assert my_wifiManager != null;
            wifiInfo = my_wifiManager.getConnectionInfo();
            ssid = wifiInfo.getSSID();

Android8.0 解决方案

一、就是用下面的代码可以获取(不建议使用)

         if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
                ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                assert cm != null;
                NetworkInfo info = cm.getActiveNetworkInfo();
                if (info != null && info.isConnected()) {
                    ssid = info.getExtraInfo();
                    logger.info("WiFi SSID: " + ssid);

                }
            }

二、Android8.0强烈推荐解决方案

关注公众号【码兄】回复【8.0】获取解决方案

解决Android 8.0和9.0无法获取SSID (unknow ssid)_第1张图片

Android9.0华为、ViVO等手机解决方案

关注公众号【码兄】回复【wifi】获取解决方案

解决Android 8.0和9.0无法获取SSID (unknow ssid)_第2张图片

权限申请指南

使用开源库

    implementation 'com.yanzhenjie:permission:2.0.0-rc12'

https://github.com/yanzhenjie/AndPermission

 if (AndPermission.hasPermissions(this, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)) {
            if (NetUtils.getInstance().isWifi(TabActivity.this)) {
                startActivity(new Intent(TabActivity.this, WifiManagerActivity.class));
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_right);
            } else {
                com.xinli.wenet.utils.Toast.show(TabActivity.this, "请连接Wi-Fi后操作", 1);

            }
        } else {
            requestPermission(Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION);
        }

 

你可能感兴趣的:(Android)