得到“缺乏权限 android.permission.BLUETOOTH” Android 12 更新后在特定设备上

发布于 2025-01-15 16:01:16 字数 3526 浏览 2 评论 0原文

所以我有一个连接到蓝牙打印机的应用程序。它可以在除以下设备之外的所有设备上运行:

  • 小米红米 note 10 Pro(已更新至 android 12,带有 MIUI 13.0.1)
  • Oppo Reno 5 4G(型号:CPH2159)(已更新至 android 12,带有 Color Os 13)

关于 Google 开发者,android 12 使用这三个权限,

android.permission.BLUETOOTH_CONNECT
android.permission.BLUETOOTH_SCAN
android.permission.BLUETOOTH_ADVERTISE

但是当我尝试连接我的打印机时,我收到了这个错误,让我抓狂。

2022-03-21 09:23:49.039 29022-29206/com.xxxx.xxxxx E/BThermalPrinterPlugin: UID 10324 / PID 29022 lacks permission android.permission.BLUETOOTH
    java.lang.SecurityException: UID 10324 / PID 29022 lacks permission android.permission.BLUETOOTH
        at android.os.Parcel.createExceptionOrNull(Parcel.java:2425)
        at android.os.Parcel.createException(Parcel.java:2409)
        at android.os.Parcel.readException(Parcel.java:2392)
        at android.os.Parcel.readException(Parcel.java:2334)
        at android.bluetooth.IBluetoothSocketManager$Stub$Proxy.connectSocket(IBluetoothSocketManager.java:227)
        at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:404)
        at id.kakzaki.blue_thermal_printer.BlueThermalPrinterPlugin.lambda$connect$1$id-kakzaki-blue_thermal_printer-BlueThermalPrinterPlugin(BlueThermalPrinterPlugin.java:544)
        at id.kakzaki.blue_thermal_printer.BlueThermalPrinterPlugin$$ExternalSyntheticLambda1.run(Unknown Source:6)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:920)

是这种 ROM/平台/设备特定的问题。 android.permission.BLUETOOTH 应该在 android 11 及更低版本上使用,因为 android 12 及更高版本将其替换为 android.permission.BLUETOOTH_CONNECT,对吧?那么为什么我首先会遇到这些特定的设备错误呢?

我已经在三星 A52 和 Android 12 上尝试过,没有任何问题。

上添加了必要的权限

我已在清单AndroidManifest.xml

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

和运行时权限请求


        var perm = arrayListOf(
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.BLUETOOTH,
            Manifest.permission.BLUETOOTH_ADMIN
        )
        //permission
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { //  >= android 12
                perm.add(Manifest.permission.BLUETOOTH_CONNECT)
                perm.add(Manifest.permission.BLUETOOTH_SCAN)
                perm.add(Manifest.permission.BLUETOOTH_ADVERTISE)
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(
                    perm.toTypedArray(), ActivityBase.REQ_PERM
                )
            }

So i have an application that connect to bluetooth printer. It is fine and running on all devices except these devices:

  • Xiaomi redmi note 10 Pro (Updated to android 12 with MIUI 13.0.1)
  • Oppo Reno 5 4G (model: CPH2159) (Updated to android 12 with Color Os 13)

Regarding Google developer, android 12 using these three permission

android.permission.BLUETOOTH_CONNECT
android.permission.BLUETOOTH_SCAN
android.permission.BLUETOOTH_ADVERTISE

but when i tried to connect on my printer, i got this error that makes me pulls my hair out.

2022-03-21 09:23:49.039 29022-29206/com.xxxx.xxxxx E/BThermalPrinterPlugin: UID 10324 / PID 29022 lacks permission android.permission.BLUETOOTH
    java.lang.SecurityException: UID 10324 / PID 29022 lacks permission android.permission.BLUETOOTH
        at android.os.Parcel.createExceptionOrNull(Parcel.java:2425)
        at android.os.Parcel.createException(Parcel.java:2409)
        at android.os.Parcel.readException(Parcel.java:2392)
        at android.os.Parcel.readException(Parcel.java:2334)
        at android.bluetooth.IBluetoothSocketManager$Stub$Proxy.connectSocket(IBluetoothSocketManager.java:227)
        at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:404)
        at id.kakzaki.blue_thermal_printer.BlueThermalPrinterPlugin.lambda$connect$1$id-kakzaki-blue_thermal_printer-BlueThermalPrinterPlugin(BlueThermalPrinterPlugin.java:544)
        at id.kakzaki.blue_thermal_printer.BlueThermalPrinterPlugin$ExternalSyntheticLambda1.run(Unknown Source:6)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:920)

is this sort of ROM/platform/device specific problem. android.permission.BLUETOOTH supposed to be used on android 11 and below because android 12 and above replaces it with android.permission.BLUETOOTH_CONNECT, right? then why i got those specific device errors in the first place?

i have tried in Samsung A52 with android 12 and got no problem at all.

i have added necessary permission on manifest

AndroidManifest.xml

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

and runtime permission request


        var perm = arrayListOf(
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.BLUETOOTH,
            Manifest.permission.BLUETOOTH_ADMIN
        )
        //permission
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { //  >= android 12
                perm.add(Manifest.permission.BLUETOOTH_CONNECT)
                perm.add(Manifest.permission.BLUETOOTH_SCAN)
                perm.add(Manifest.permission.BLUETOOTH_ADVERTISE)
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(
                    perm.toTypedArray(), ActivityBase.REQ_PERM
                )
            }

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷︶言冷语的世界 2025-01-22 16:01:16

正如您已经发现的,小米似乎对蓝牙规范的实现不正确。

我遇到了和你一样的问题,并在 AndroidManifest.xml 中声明 android.permission.BLUETOOTH 解决了这个问题,即使对于 Android 12 也是如此。

因此,不要添加文档建议的以下行:

<uses-permission android:name="android.permission.BLUETOOTH"
             android:maxSdkVersion="30" />

我添加了:

<uses-permission android:name="android.permission.BLUETOOTH" tools:remove="android:maxSdkVersion" />

我必须添加 tools:remove 属性,因为我使用的蓝牙库已经添加了 maxSdkVersion 属性。因此,即使我自己没有添加它,它也会包含在我的合并清单中。

另请注意,在清单中添加此权限不会在三星等其他 Android 12 设备上导致任何进一步的问题或权限提示。

As you already found, Xiaomi seems to have an incorrect implementation of the Bluetooth spec.

I had the same problem as you do and solved it declaring android.permission.BLUETOOTH in AndroidManifest.xml, even for Android 12.

So, instead of adding the following line, which the documentation suggests:

<uses-permission android:name="android.permission.BLUETOOTH"
             android:maxSdkVersion="30" />

I added:

<uses-permission android:name="android.permission.BLUETOOTH" tools:remove="android:maxSdkVersion" />

I had to add the tools:remove attribute because the Bluetooth library I was using was adding the maxSdkVersion attribute already. So it was being included in my merged manifest even if I didn't add it myself.

Also note that adding this permission in the manifest didn't cause any further problems or permission prompts on other Android 12 devices like Samsung.

夜灵血窟げ 2025-01-22 16:01:16

我认为这是小米/MIUI的问题。要使其正常工作,您可以与 android:targetSdkVersion="30" 一起使用,而不是与 android:targetSdkVersion="31" 一起使用。

I think it is a Xiaomi/MIUI issues. To make it work, you can use with android:targetSdkVersion="30" and not with android:targetSdkVersion="31".

〃温暖了心ぐ 2025-01-22 16:01:16

以下代码片段演示了如果应用程序面向 Android 12 或更高版本,如何在应用程序中声明与蓝牙相关的权限:

<manifest>
<!-- Request legacy Bluetooth permissions on older devices. -->
<uses-permission android:name="android.permission.BLUETOOTH"
                 android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
                 android:maxSdkVersion="30" />

<!-- Needed only if your app looks for Bluetooth devices.
     If your app doesn't use Bluetooth scan results to derive physical
     location information, you can strongly assert that your app
     doesn't derive physical location. -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

<!-- Needed only if your app makes the device discoverable to Bluetooth
     devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />

<!-- Needed only if your app communicates with already-paired Bluetooth
     devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

<!-- Needed only if your app uses Bluetooth scan results to derive physical location. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
...

您可以在此处查看更多详细信息 来源。

The following code snippet demonstrates how to declare Bluetooth-related permissions in your app if it targets Android 12 or higher:

<manifest>
<!-- Request legacy Bluetooth permissions on older devices. -->
<uses-permission android:name="android.permission.BLUETOOTH"
                 android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
                 android:maxSdkVersion="30" />

<!-- Needed only if your app looks for Bluetooth devices.
     If your app doesn't use Bluetooth scan results to derive physical
     location information, you can strongly assert that your app
     doesn't derive physical location. -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

<!-- Needed only if your app makes the device discoverable to Bluetooth
     devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />

<!-- Needed only if your app communicates with already-paired Bluetooth
     devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

<!-- Needed only if your app uses Bluetooth scan results to derive physical location. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
...

you can check more details here source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文