1 year ago

#387813

test-img

Dylon Jaynes

What is a better way to request bluetooth permissions?

I have been searching to try and understand how to use ActivityCompat to request bluetooth and location permissions but I am not understanding how they really work. I have checked Android documentation but haven't had much luck in figuring out how to implement it in my project. Can anyone help me to understand a better way to request permissions or point me to a good resource for understanding them?

Here is my code:

@RequiresApi(Build.VERSION_CODES.S)
    override fun onStart() {
        super.onStart()
        if (!adapter.isEnabled) {
            // Check if bluetooth is enabled on the device.
            val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
            requestBluetooth.launch(enableBtIntent)
        }

        if (ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.BLUETOOTH_CONNECT
            ) != PackageManager.PERMISSION_GRANTED
        ) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(
                    this@MainActivity,
                    android.Manifest.permission.BLUETOOTH_CONNECT
            )) {
                ActivityCompat.requestPermissions(
                    this@MainActivity,
                    arrayOf(Manifest.permission.BLUETOOTH_CONNECT), 1
                )
            }
        }

        if (ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.BLUETOOTH_SCAN
            ) != PackageManager.PERMISSION_GRANTED
        ) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(
                    this@MainActivity,
                    android.Manifest.permission.BLUETOOTH_SCAN
                )) {
                ActivityCompat.requestPermissions(
                    this@MainActivity,
                    arrayOf(Manifest.permission.BLUETOOTH_SCAN), 1
                )
            }
        }

        // Checks if location permissions are granted and asks for them if they are not.
        if (ContextCompat.checkSelfPermission(
                this@MainActivity,
                android.Manifest.permission.ACCESS_FINE_LOCATION
            ) !==
            PackageManager.PERMISSION_GRANTED
        ) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(
                    this@MainActivity,
                    android.Manifest.permission.ACCESS_FINE_LOCATION
                )
            ) {
                ActivityCompat.requestPermissions(
                    this@MainActivity,
                    arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), 1
                )
            } else {
                ActivityCompat.requestPermissions(
                    this@MainActivity,
                    arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), 1
                )
            }
        } else {
            if (recyclerView.isNotEmpty()){
                // scan without the loading progress bar.
                Log.d("progressBar", "onStart: hey!")
                progressBar.visibility = GONE
                scanLeDevice()
            }else {
                // scan with the loading progress bar.
                progressBar.visibility = VISIBLE
                scanLeDevice()
            }
        }
    }

and a callback function for the location permission:

// Processes location permission results.
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>,
                                            grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {
            1 -> {
                if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    if ((ContextCompat.checkSelfPermission(this@MainActivity, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
                    ) {
                        Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show()

                        if (recyclerView.isNotEmpty()){
                            // scan without the loading progress bar.
                            Log.d("progressBar", "onStart: hey!")
                            progressBar.visibility = GONE
                            scanLeDevice()
                        }else {
                            // scan with the loading progress bar.
                            progressBar.visibility = VISIBLE
                            scanLeDevice()
                        }
                    }
                } else {
                    Toast.makeText(this, "Location permission have been denied. Please enable location permissions to scan for ble devices.", Toast.LENGTH_LONG).show()
                    progressBar.visibility = GONE
                }
                return
            }
        }
    }

android

kotlin

android-permissions

0 Answers

Your Answer

Accepted video resources