On Oreo (8.1.0) not getting the correct Wifi SSID. It's showing though it is connected to a wifi with SSID

前端 未结 7 1017
名媛妹妹
名媛妹妹 2021-01-02 03:02

I need to check the current connected wifi SSID on android. I checked with Nokia 6 and OnePlus 5 with respectively Oreo 8.1.0 and Oreo 8.0. Other phones with different OS v

7条回答
  •  孤城傲影
    2021-01-02 03:23

    You need to request permission in manifest :

     
    

    and request it in your activity :

        private void grantPermm()
    {
    
        try
        {
            if (ContextCompat.checkSelfPermission(MainScreenActivity.this,
                      Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
    
            {
    
            }
            else
            {
    
                ActivityCompat.requestPermissions(MainScreenActivity.this,
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                        101);
            }
    
        }
        catch (Exception xx){}
    
    }
    

    then use this function :

        public  static String  getWifiName(Context context)
    {
        String result="";
        try {
            WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            if (manager.isWifiEnabled())
            {
                WifiInfo wifiInfo = manager.getConnectionInfo();
                if (wifiInfo != null)
                {
                    NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
                    if (state == NetworkInfo.DetailedState.CONNECTED || state == NetworkInfo.DetailedState.OBTAINING_IPADDR)
                    {
                        result = wifiInfo.getSSID();
                        if(result.equalsIgnoreCase(""))
                        {
                            Toast.makeText(context,"You are connected to mobile data", Toast.LENGTH_SHORT).show();
                            result="";
                        }
    
                        return result;
                    }
                    else
                    {
                        Toast.makeText(context,"WIFI not connected", Toast.LENGTH_SHORT).show();
                        return "";
                    }
                }
                else
                {
                    Toast.makeText(context,"No WIFI Information", Toast.LENGTH_SHORT).show();
                    return "";
                }
            }
            else
            {
                Toast.makeText(context,"WIFI not enabled", Toast.LENGTH_SHORT).show();
                return "";
            }
        }
        catch (Exception xx)
        {
            Toast.makeText(context, xx.getMessage().toString(), Toast.LENGTH_SHORT).show();
            return "";
        }
    }
    

提交回复
热议问题