Android 13下Kivy应用蓝牙权限报错求助:需BLUETOOTH_CONNECT权限
解决方案
1. 更新buildozer.spec的权限配置
Android 13(API 33)对蓝牙权限进行了细分,原有的BLUETOOTH和BLUETOOTH_ADMIN权限已不足以完成绑定设备访问操作。你需要在buildozer.spec中添加BLUETOOTH_CONNECT权限:
android.permissions = BLUETOOTH, BLUETOOTH_ADMIN, BLUETOOTH_CONNECT
保留旧权限是为了兼容Android 11及以下版本。
2. 动态申请危险权限
Android 6.0(API 23)及以上版本要求危险权限必须在运行时动态申请,BLUETOOTH_CONNECT属于危险权限范畴,仅在manifest中声明无法生效。推荐使用plyer库简化权限请求操作:
步骤1:添加plyer依赖到buildozer.spec
requirements = python3,kivy,plyer
步骤2:在Kivy应用中添加权限请求代码
from kivy.app import App from plyer import permissions from plyer.utils import platform class BluetoothDataApp(App): def on_start(self): if platform == 'android': # 动态请求BLUETOOTH_CONNECT权限 permissions.request_permissions([permissions.BLUETOOTH_CONNECT]) if __name__ == '__main__': BluetoothDataApp().run()
问题原因说明
Android 13将原有的单一BLUETOOTH权限拆分为三个细分权限:
BLUETOOTH_SCAN:用于扫描蓝牙设备BLUETOOTH_CONNECT:用于连接蓝牙设备、访问已绑定设备列表BLUETOOTH_ADVERTISE:用于蓝牙广播
你的应用调用getBondedDevices()获取已绑定设备列表,必须持有BLUETOOTH_CONNECT权限,且需要在运行时主动向用户申请该权限,否则会触发SecurityException。
内容的提问来源于stack exchange,提问作者Fathi
相关产品推荐
相关产品推荐

