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

前端 未结 7 1058
名媛妹妹
名媛妹妹 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:04

    You need to ask for runtime permission of read phone state. This is included for better security.

    code is as follows:-

        TelephonyManager telephonyManager = (TelephonyManager) getContext()
                .getSystemService(Context.TELEPHONY_SERVICE);
    
        String IMEI ="";
    
        if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
           ActivityCompat.requestPermissions(getActivity(),
                   new String[]{Manifest.permission.READ_PHONE_STATE},
                   123);
    
        }
        int permission = ContextCompat.checkSelfPermission(getContext(),
                Manifest.permission.READ_PHONE_STATE);
        if(permission == PackageManager.PERMISSION_GRANTED){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                IMEI = telephonyManager.getImei();
            }
            else{
                IMEI = telephonyManager.getDeviceId();
            }
        }
    

提交回复
热议问题