在Android 11或更低版本上,蓝牙FINE_LOCATION和COARSE_LOCATION权限

huangapple go评论80阅读模式
英文:

Bluetooth FINE_LOCATION and COARSE_LOCATION Permissions on Android 11 or lower

问题

我想通过蓝牙 SPP 套接字与 Android 设备建立连接并进行通信。因此,我需要在清单中指定正确的蓝牙权限,并在应用程序中运行时请求它们。

根据官方蓝牙权限文档,在 Android 11 或更低版本上,我需要请求以下权限:

  • BLUETOOTH
  • BLUETOOTH_ADMIN
  • ACCESS_FINE_LOCATION

然而,它指出:
> 如果您的应用目标为 Android 9(API 级别 28)或更低版本,您可以声明 ACCESS_COARSE_LOCATION 权限,而不是 ACCESS_FINE_LOCATION 权限。

因此,我期望我不需要声明 ACCESS_COARSE_LOCATION,即使是对于 Android 9 或更低版本。

然而,IDE 表示:
> 如果您需要访问精确位置,您必须同时请求 ACCESS_FINE_LOCATION 和 ACCESS_COARSE_LOCATION。

为什么我需要同时请求 ACCESS_FINE_LOCATION 和 ACCESS_COARSE_LOCATION 权限,难道只请求 ACCESS_FINE_LOCATION 不足够吗?

当我同时请求两者时,权限要求得到满足,但我希望保持我的代码清晰简洁,并理解权限准则。

英文:

I want to connect and communicate with an Android device via a Bluetooth SPP socket. Therefore, I need to specify the correct Bluetooth permissions in the Manifest and request them at runtime in the app.

According to the official Bluetooth permission documentation, on Android 11 or lower, I need to request the following permissions:

  • BLUETOOTH
  • BLUETOOTH_ADMIN
  • ACCESS_FINE_LOCATION

However it is stated:
> If your app targets Android 9 (API level 28) or lower, you can declare the ACCESS_COARSE_LOCATION permission instead of the ACCESS_FINE_LOCATION permission.

Therefore, I would expect that I don't need to declare ACCESS_COARSE_LOCATION, even for Android 9 or lower.

Nevertheless, the IDE states:
> If you need access to FINE location, you must request both ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION

Why do i need to request booth ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions, shouldn't it be sufficient to only request ACCESS_FINE_LOCATION?

The permission requirements are fulfilled when I request both but i want to keep my code clean and minimal in addition to understanding the permission guidelines.

答案1

得分: 1

关于蓝牙连接,ACCESS_COARSE_LOCATION权限通常是不必要的。IDE建议同时请求ACCESS_FINE_LOCATION和ACCESS_COARSE_LOCATION权限可能在蓝牙通信的情境下不适用。这似乎更像是基于典型的基于位置的应用程序开发场景而提出的通用建议,而不是真正的需求...

在蓝牙的情况下,通常只需要ACCESS_FINE_LOCATION权限。ACCESS_COARSE_LOCATION权限不是蓝牙功能的特定要求。甚至在提供的文档中没有说明在Android 9(API级别28)或更低版本中可以声明ACCESS_COARSE_LOCATION权限来替代ACCESS_FINE_LOCATION权限。相反,文档中有以下说明:

ACCESS_FINE_LOCATION是必需的,因为在Android 11及更低版本中,蓝牙扫描可能会被用来获取用户位置信息。

总结一下:

对于蓝牙连接,你应该在应用程序清单中声明和请求BLUETOOTH和BLUETOOTH_ADMIN权限。

此外,你应该声明和请求ACCESS_FINE_LOCATION权限,因为从Android 6.0(API级别23)开始,蓝牙扫描被视为一种位置访问形式。

IDE建议同时请求ACCESS_FINE_LOCATION和ACCESS_COARSE_LOCATION权限可能是基于典型的基于位置的应用程序开发场景的通用建议。然而,在蓝牙通信的情况下,只请求ACCESS_FINE_LOCATION权限就足够了。

英文:

You are correct that for Bluetooth connectivity, the ACCESS_COARSE_LOCATION permission is not necessary. The IDE suggestion to request both ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions may not be applicable in the context of Bluetooth communication. It seems to be more likely a generic recommendation based on typical location-based app development scenarios than a really need...

In the case of Bluetooth, the requirement is typically limited to ACCESS_FINE_LOCATION permission. The ACCESS_COARSE_LOCATION permission is not specifically required for Bluetooth functionality. There is even no statement in provided documentation that you can declare ACCESS_COARSE_LOCATION permission instead of the ACCESS_FINE_LOCATION permission at Android 9 (API level 28) or lower. On the contrary there is written following statement:

> ACCESS_FINE_LOCATION is necessary because, on Android 11 and lower, a Bluetooth scan could potentially be used to gather information about the location of the user.

To summarize:

For Bluetooth connectivity, you should declare and request the BLUETOOTH and BLUETOOTH_ADMIN permissions in your app's manifest.

Additionally, you should declare and request the ACCESS_FINE_LOCATION permission, as Bluetooth scans are considered a form of location access starting from Android 6.0 (API level 23).

The IDE suggestion to request both ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions might be a generic recommendation based on typical location-based app development scenarios. However, in the case of Bluetooth communication, it is sufficient to request only the ACCESS_FINE_LOCATION permission.

huangapple
  • 本文由 发表于 2023年3月9日 20:21:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定