Sunday, January 29, 2023
HomeSoftware DevelopmentHow one can Use Close by Wi-Fi Entry Permission in Android 13?

How one can Use Close by Wi-Fi Entry Permission in Android 13?


import android.R.attr

import android.annotation.SuppressLint

import android.app.StatusBarManager

import android.content material.ComponentName

import android.content material.Context

import android.content material.Intent

import android.content material.pm.PackageManager

import android.graphics.drawable.Icon

import android.internet.Uri

import android.internet.wifi.WifiManager

import android.os.Construct

import android.os.Bundle

import android.supplier.MediaStore

import android.widget.Button

import android.widget.ImageView

import android.widget.TextView

import android.widget.Toast

import androidx.exercise.outcome.ActivityResultLauncher

import androidx.exercise.outcome.contract.ActivityResultContracts

import androidx.appcompat.app.AppCompatActivity

import androidx.core.content material.ContextCompat

import com.google.android.materials.dialog.MaterialAlertDialogBuilder

import java.util.jar.Manifest

  

class MainActivity : AppCompatActivity() {

    

    non-public lateinit var permissionLauncher: ActivityResultLauncher<String>

    non-public lateinit var startHotspotBtn: Button

    non-public lateinit var statusTV: TextView

  

    

    non-public val wifiManager: WifiManager by lazy {

        applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager

    }

  

    @SuppressLint("WrongConstant")

    override enjoyable onCreate(savedInstanceState: Bundle?) {

        tremendous.onCreate(savedInstanceState)

        setContentView(R.format.activity_main)

  

        

        permissionLauncher = registerForActivityResult(

            

            ActivityResultContracts.RequestPermission()

        ) { isGranted: Boolean ->

            if (isGranted) {

                

                startHotSpot()

            } else {

                

                Toast.makeText(

                    this,

                    "Please enable the Close by Wi-Fi Units permission for this app",

                    Toast.LENGTH_LONG,

                ).present()

            }

        }

  

        

        startHotspotBtn = findViewById(R.id.idBtnHotspot)

        statusTV = findViewById(R.id.idTVHotspotStatue)

  

        

        startHotspotBtn.setOnClickListener {

            

            checkPermissions()

        }

    }

  

    @SuppressLint("InlinedApi")

    non-public enjoyable checkPermissions() {

        if (Construct.VERSION.SDK_INT >= Construct.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 wouldn't work with out Close by Wi-Fi Units permission. Do you need to give this app the permission?")

                        .setPositiveButton("Sure") { _, _ ->

                            permissionLauncher.launch(permission)

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

  

                        }.present()

                }

                else -> {

                    

                    permissionLauncher.launch(permission)

                }

            }

        } else {

            

            Toast.makeText(

                this,

                "Please use Android 13 gadget.",

                Toast.LENGTH_SHORT,

            ).present()

        }

    }

  

    @SuppressLint("NewApi")

    non-public enjoyable startHotSpot() {

        

        wifiManager.startLocalOnlyHotspot(

            object : WifiManager.LocalOnlyHotspotCallback() {

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

                    tremendous.onStarted(reservation)

                    

                    

                    startHotspotBtn.isEnabled = false

                    statusTV.textual content = "Standing Native Solely Hotspot: STARTED"

                }

  

                override enjoyable onFailed(cause: Int) {

                    

                    tremendous.onFailed(cause)

                    

                    Toast.makeText(

                        this@MainActivity,

                        "Error Native Solely Hotspot: $cause",

                        Toast.LENGTH_SHORT,

                    ).present()

                }

  

                override enjoyable onStopped() {

                    

                    tremendous.onStopped()

                    startHotspotBtn.isEnabled = true

                    statusTV.textual content = "Standing Native Solely Hotspot: STOPPED"

                }

            },

            null,

        )

    }

}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments