Select Page

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

Team 2 - Languages
Published: January 28, 2023

import android.R.attr

import android.annotation.SuppressLint

import android.app.StatusBarManager

import android.content.ComponentName

import android.content.Context

import android.content.Intent

import android.content.pm.PackageManager

import android.graphics.drawable.Icon

import android.net.Uri

import android.net.wifi.WifiManager

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.appcompat.app.AppCompatActivity

import androidx.core.content.ContextCompat

import com.google.android.material.dialog.MaterialAlertDialogBuilder

import java.util.jar.Manifest

  

class MainActivity : AppCompatActivity() {

    

    private lateinit var permissionLauncher: ActivityResultLauncher<String>

    private lateinit var startHotspotBtn: Button

    private lateinit var statusTV: TextView

  

    

    private val wifiManager: WifiManager by lazy {

        applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager

    }

  

    @SuppressLint("WrongConstant")

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

  

        

        permissionLauncher = registerForActivityResult(

            

            ActivityResultContracts.RequestPermission()

        ) { isGranted: Boolean ->

            if (isGranted) {

                

                startHotSpot()

            } else {

                

                Toast.makeText(

                    this,

                    "Please allow the Nearby Wi-Fi Devices permission for this app",

                    Toast.LENGTH_LONG,

                ).show()

            }

        }

  

        

        startHotspotBtn = findViewById(R.id.idBtnHotspot)

        statusTV = findViewById(R.id.idTVHotspotStatue)

  

        

        startHotspotBtn.setOnClickListener {

            

            checkPermissions()

        }

    }

  

    @SuppressLint("InlinedApi")

    private fun checkPermissions() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            

            val permission: String = android.Manifest.permission.NEARBY_WIFI_DEVICES

            when {

                

                ContextCompat.checkSelfPermission(

                    this, permission,

                ) == PackageManager.PERMISSION_GRANTED -> {

                    

                    startHotSpot()

                }

                

                

                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") { _, _ ->

                            permissionLauncher.launch(permission)

                        }.setNegativeButton("No Thanks") { _, _ ->

  

                        }.show()

                }

                else -> {

                    

                    permissionLauncher.launch(permission)

                }

            }

        } else {

            

            Toast.makeText(

                this,

                "Please use Android 13 device.",

                Toast.LENGTH_SHORT,

            ).show()

        }

    }

  

    @SuppressLint("NewApi")

    private fun startHotSpot() {

        

        wifiManager.startLocalOnlyHotspot(

            object : WifiManager.LocalOnlyHotspotCallback() {

                override fun onStarted(reservation: WifiManager.LocalOnlyHotspotReservation?) {

                    super.onStarted(reservation)

                    

                    

                    startHotspotBtn.isEnabled = false

                    statusTV.text = "Status Local Only Hotspot: STARTED"

                }

  

                override fun onFailed(reason: Int) {

                    

                    super.onFailed(reason)

                    

                    Toast.makeText(

                        this@MainActivity,

                        "Error Local Only Hotspot: $reason",

                        Toast.LENGTH_SHORT,

                    ).show()

                }

  

                override fun onStopped() {

                    

                    super.onStopped()

                    startHotspotBtn.isEnabled = true

                    statusTV.text = "Status Local Only Hotspot: STOPPED"

                }

            },

            null,

        )

    }

}

Source: www.geeksforgeeks.org