请求蓝牙权限的更好方法是什么?

发布于 2025-01-20 03:18:56 字数 4694 浏览 1 评论 0原文

我一直在寻找尝试并了解如何使用 ActivityCompat 来请求蓝牙和位置权限,但我不了解它们的实际工作原理。我已经检查了 Android 文档,但在弄清楚如何在我的项目中实现它方面运气不佳。任何人都可以帮助我了解请求权限的更好方法或为我指出一个很好的资源来理解它们吗?

这是我的代码:

@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()
            }
        }
    }

以及位置权限的回调函数:

// 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
            }
        }
    }

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
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文