请求蓝牙权限的更好方法是什么?
我一直在寻找尝试并了解如何使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论