androidandroid-wifiwifimanager

How to get Wifi AP details like SSID, Band, etc when connected in Android API level 28 and above?


I am trying to get Wifi AP details. I am using the following code

    private val TAG = "[MainActivity]"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

         val receiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent?) {
                if(WifiManager.NETWORK_STATE_CHANGED_ACTION == intent?.action){
                    val networkInfo = intent.getParcelableExtra<NetworkInfo>(WifiManager.EXTRA_NETWORK_INFO)
                    if(ConnectivityManager.TYPE_WIFI == networkInfo.type){
                        val wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
                        val info = wifiManager.connectionInfo
                        Log.d(TAG, "info = $info")
                    }
                }
            }
        }

        val intentFilter = IntentFilter()
        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION)
        applicationContext.registerReceiver(receiver, intentFilter)
    }

NetworkInfo and ConnectivityManager.TYPE_WIFI are shown to be deprecated. Also I am getting SSID as null.

info = SSID: , BSSID: 02:00:00:00:00:00, MAC: 02:00:00:00:00:00, Supplicant state: COMPLETED

Permissions that I used:

    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

What is the best way to achieve above task?


Solution

  • An addition to @Shlomi's answer,

    Here is the sample of getting permissions from user at runtime

    Perform a check before performing operation

    if (!checkPermissions()) {
        // App does not have permissions, ask for permissions.
        requestPermissions()
    } else {
        // App has permissions
        // Perform your operations here......
    }
    

    Code to check and ask permissions

    Kotlin

    /**
     * Return the current state of the permissions needed.
     */
    private fun checkPermissions(): Boolean {
       
        val permissionState = ActivityCompat.checkSelfPermission(
                requireContext(),
                Manifest.permission.ACCESS_FINE_LOCATION
        )
        return permissionState == PackageManager.PERMISSION_GRANTED
    }
    
    private fun requestPermissions() {
        val shouldProvideRationale = ActivityCompat.shouldShowRequestPermissionRationale(
                requireActivity(),
                Manifest.permission.ACCESS_FINE_LOCATION
        )
    
        // Provide an additional rationale to the user. This would happen if the user denied the
        // request previously, but didn't check the "Don't ask again" checkbox.
        if (shouldProvideRationale) {
            Log.i(
                    TAG,
                    "Displaying permission rationale to provide additional context."
            )
            Snackbar.make(
                    binding.wifiConnectionFragment,
                    "Location permission is needed for core functionality",
                    Snackbar.LENGTH_INDEFINITE
            )
                .setAction("OK") { // Request permission
                    ActivityCompat.requestPermissions(
                            requireActivity(), arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
                            REQUEST_PERMISSIONS_REQUEST_CODE
                    )
                }
                .show()
        } else {
            Log.i(TAG, "Requesting permission")
            // Request permission. It's possible this can be auto answered if device policy
            // sets the permission in a given state or the user denied the permission
            // previously and checked "Never ask again".
            ActivityCompat.requestPermissions(
                    requireActivity(), arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
                    REQUEST_PERMISSIONS_REQUEST_CODE
            )
        }
    }
    
    override fun onRequestPermissionsResult(
            requestCode: Int,
            permissions: Array<out String>,
            grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) {
            if (grantResults.isEmpty()) {
    
            } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
                // Perform your operations
            } else {
                // Permission denied.
    
                // Notify the user via a SnackBar that they have rejected a core permission for the
                // app, which makes the Activity useless. In a real app, core permissions would
                // typically be best requested during a welcome-screen flow.
    
                // Additionally, it is important to remember that a permission might have been
                // rejected without asking the user for permission (device policy or "Never ask
                // again" prompts). Therefore, a user interface affordance is typically implemented
                // when permissions are denied. Otherwise, your app could appear unresponsive to
                // touches or interactions which have required permissions.
                Snackbar.make(
                        binding.wifiConnectionFragment,
                        "Permission was denied, but is needed for core functionality.",
                        Snackbar.LENGTH_INDEFINITE
                )
                    .setAction(
                            "Settings",
                            View.OnClickListener { // Build intent that displays the App settings screen.
                                val intent = Intent()
                                intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
                                val uri = Uri.fromParts(
                                        "package",
                                        BuildConfig.APPLICATION_ID, null
                                )
                                intent.data = uri
                                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                                startActivity(intent)
                            })
                    .show()
            }
        }
    }