Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to Use Nearby Wi-Fi Access Permission in Android 13?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Before Android 13 when any android application want to use any wifi related feature within the android application. We have to provide users with fine location permission along with wifi permission to use wifi-related features within the android application. In the new android 13 updates where we can use some of the Wi-Fi-related functionalities without the requirement of any Location permission. In this article, we will be building a simple application in which we will be using nearby Wi-FI access location permission to use Hotspot functionality within our android application. 

Step-By-Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Updating SDK version in build.gradle file.

Navigate to Gradle Scripts>module level build.gradle file and add change compile SDK and target SDK to 33. After that simply sync your project to install it.

Step 3: Working with activity_main.xml.

Navigate to app>res>layout>activity_main.xml and add the below code to it. Comments are added in the code to get to know it in detail. 


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
    <!-- text view for displaying hot spot status-->
        android:textStyle="bold" />
    <!--creating button on add quick tile settings-->
        android:text="Start Hotspot"
        app:backgroundTint="@color/edt_back_color" />

Step 4: Working with MainActivity.kt file. 

Navigate to app>java>your app’s package name>MainActivity.kt file and add the below code to it. Comments are added in the code to get to know it in detail. 


import android.R.attr
import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import java.util.jar.Manifest
class MainActivity : AppCompatActivity() {
    // creating variables on below line.
    private lateinit var permissionLauncher: ActivityResultLauncher<String>
    private lateinit var startHotspotBtn: Button
    private lateinit var statusTV: TextView
    // on below line creating and initializing wifi manager.
    private val wifiManager: WifiManager by lazy {
        applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
    override fun onCreate(savedInstanceState: Bundle?) {
        // on below line initializing request permission launcher.
        permissionLauncher = registerForActivityResult(
            // on below line requesting permission.
        ) { isGranted: Boolean ->
            if (isGranted) {
                // if permission is already granted we are starting the device hotspot.
            } else {
                // displaying toast message if permissions are not provided.
                    "Please allow the Nearby Wi-Fi Devices permission for this app",
        // initializing variables on below line.
        startHotspotBtn = findViewById(
        statusTV = findViewById(
        // adding click listener for button on below line.
        startHotspotBtn.setOnClickListener {
            // on below line calling method to check permissions.
    private fun checkPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // on below line creating a variable for wifi permission.
            val permission: String = android.Manifest.permission.NEARBY_WIFI_DEVICES
            when {
                // on below line checking weather permissions are granted or not.
                    this, permission,
                ) == PackageManager.PERMISSION_GRANTED -> {
                    // if permission are granted starting hotspot by calling below method.
                // below method is called when permissions are not granted .
                // below method is use to display prompt message when permissions are not provided.
                shouldShowRequestPermissionRationale(permission) -> {
                    MaterialAlertDialogBuilder(this).setMessage("This app would not work without Nearby Wi-Fi Devices permission. Do you want to give this app the permission?")
                        .setPositiveButton("Yes") { _, _ ->
                        }.setNegativeButton("No Thanks") { _, _ ->
                else -> {
                    // on below line calling method to request permissions.
        } else {
            // if device is not a android 13 device displaying below toast message.
                "Please use Android 13 device.",
    private fun startHotSpot() {
        // below method is use to start local hotspot.
            object : WifiManager.LocalOnlyHotspotCallback() {
                override fun onStarted(reservation: WifiManager.LocalOnlyHotspotReservation?) {
                    // this method is called when hotspot is started.
                    // on below line disabling button and changing status text view.
                    startHotspotBtn.isEnabled = false
                    statusTV.text = "Status Local Only Hotspot: STARTED"
                override fun onFailed(reason: Int) {
                    // this method is called when device fails to start hotspot.
                    // displaying error toast message on below line.
                        "Error Local Only Hotspot: $reason",
                override fun onStopped() {
                    // below method is called when the app has stopped due to any reason.
                    startHotspotBtn.isEnabled = true
                    statusTV.text = "Status Local Only Hotspot: STOPPED"

Step 5: Adding permissions for using Wifi in the AndroidManifest.xml file. 

Navigate to app>AndroidManifest.xml file and add the below permissions to it. 

    android:usesPermissionFlags="neverForLocation" />
<!-- permission for Hotspot -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />


My Personal Notes arrow_drop_up
Last Updated : 28 Jan, 2023
Like Article
Save Article
Similar Reads