首页 > 编程语言 >Android 12蓝牙报java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission

Android 12蓝牙报java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission

时间:2023-04-06 10:34:08浏览次数:72  
标签:lang 12 java permission 蓝牙 BLUETOOTH android os

报错如下:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.studay.base.study, PID: 16798
    java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission for AttributionSource { uid = 10392, packageName = com.studay.base.study, attributionTag = null, token = android.os.BinderProxy@3a97bdd, next = null }: enable
        at android.os.Parcel.createExceptionOrNull(Parcel.java:2438)
        at android.os.Parcel.createException(Parcel.java:2422)
        at android.os.Parcel.readException(Parcel.java:2405)
        at android.os.Parcel.readException(Parcel.java:2347)
        at android.bluetooth.IBluetoothManager$Stub$Proxy.enable(IBluetoothManager.java:987)
        at android.bluetooth.BluetoothAdapter.enable(BluetoothAdapter.java:2219)
        at com.studay.base.study.bluetooth.BluetoothFragment$1.onClick(BluetoothFragment.java:85)
        at android.view.View.performClick(View.java:7792)
        at android.widget.TextView.performClick(TextView.java:16112)
        at android.view.View.performClickInternal(View.java:7769)
        at android.view.View.access$3800(View.java:910)
        at android.view.View$PerformClick.run(View.java:30218)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:226)
        at android.os.Looper.loop(Looper.java:313)
        at android.app.ActivityThread.main(ActivityThread.java:8751)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
     Caused by: android.os.RemoteException: Remote stack trace:
        at com.android.server.BluetoothManagerService.checkPermissionForDataDelivery(BluetoothManagerService.java:5034)
        at com.android.server.BluetoothManagerService.checkConnectPermissionForDataDelivery(BluetoothManagerService.java:5052)
        at com.android.server.BluetoothManagerService.checkBluetoothPermissions(BluetoothManagerService.java:1506)
        at com.android.server.BluetoothManagerService.enable(BluetoothManagerService.java:1787)
        at android.bluetooth.IBluetoothManager$Stub.onTransact(IBluetoothManager.java:441)

 

  <!-- Required to be able to connect to paired Bluetooth devices.
         <p>Protection level: dangerous -->
    <permission android:name="android.permission.BLUETOOTH_CONNECT"
        android:permissionGroup="android.permission-group.UNDEFINED"
        android:description="@string/permdesc_bluetooth_connect"
        android:label="@string/permlab_bluetooth_connect"
        android:protectionLevel="dangerous" />

 

近期遇到一个问题,之前发布的APP连接蓝牙都是正常的,现在有人反映连不上了。经过测试发现:android 12 和 harmonyOS 3.0.0 都会有这个问题,而之前的版本就不会有这个。

        经过网上一番查找,原来是因为最近Google发布的Android 12,新引入了 BLUETOOTH_SCAN、BLUETOOTH_CONNECT、BLUETOOTH_ADVERTISE 三个权限。

        从Android 12开始,过去的蓝牙权限被拆分成了3个新的权限,并且全都是运行时权限(需要动态申请):

  • BLUETOOTH_SCAN 用于使用蓝牙扫描附件其他的蓝牙设备
  • BLUETOOTH_ADVERTISE 用于允许当前的设备被其他的蓝牙设备所发现
  • BLUETOOTH_CONNECT 用于连接之前已经配对过的蓝牙设备

        这3个权限都是从Android 12系统才开始有的,所以为了能够兼容过去的老版本,建议在AndroidManifest.xml中这样声明:

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

        新增的3个蓝牙权限都是运行时权限,因此只在AndroidManifest.xml中声明是没有用的,还要在代码中动态申请权限才行。必须先在应用中用户明确批准使用,然后才能查找蓝牙设备、使某个设备可被其他设备检测到,或者与已配对的蓝牙设备通信。

        具体的申请方法如下:首先要判断当前的系统版本,只有当Android 12及以上系统时,才应该去请求新增的蓝牙权限。(PS:3个权限都属于同一个权限组,因此理论上只要申请一个权限,另外2个也就自动授权了。)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String[] permission = checkSelfPermissionArray(this, new String[]{
                    Manifest.permission.BLUETOOTH_SCAN,
                    Manifest.permission.BLUETOOTH_ADVERTISE,
                    Manifest.permission.BLUETOOTH_CONNECT});
            if (permission.length > 0) {
                ActivityCompat.requestPermissions(this, permission, 102);
            }
        }

注意:

之前的Android系统中有一个很奇怪的现象,当我们在应用中使用蓝牙扫描附件设备的时候,需要申请地理位置权限。蓝牙权限并不是运行时权限,但地理位置权限却是。

(299条消息) Android连接蓝牙设备问题(android.permission.BLUETOOTH)_老杜_d的博客-CSDN博客

(299条消息) android设置打开蓝牙时报错:java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission_YaXinShi的博客-CSDN博客

标签:lang,12,java,permission,蓝牙,BLUETOOTH,android,os
From: https://www.cnblogs.com/wanglongjiang/p/17291859.html

相关文章

  • [oeasy]python0128_unicode_字符集_character_set_八卦_星座
    unicode回忆上次内容中国的简体和繁体汉字字符数量都超级大彼此还认对方为乱码 如果有一种编码所有的字符都能编进去就好了中日韩(CJK)欧洲拼音梵文阿拉伯文卢恩字符等等等都包括进去 ​ 添加图片注释,不超过1......
  • golang开发需要掌握的核心包以及中间件,涵盖项目的各个领域,值得收藏
    golang开发需要掌握的核心包以及中间件,涵盖项目的各个领域,值得收藏。常用包常用包 说明fmt 实现格式化的输入输出操作,其中的fmt.Printf()和fmt.Println()是开发者使用最为频繁的函数。io 实现了一系列非平台相关的IO相关接口和实现,比如提供了对os中系统相关的IO功能的封装。我们......
  • Golang基础-Select
    基本概念select是Go中的一个控制结构,类似于switch语句。select语句只能用于通道(channel)操作,每个case必须是一个通道操作,要么是发送要么是接收。select语句会监听所有指定的通道上的操作,一旦其中一个通道准备好就会执行相应的代码块。如果多个通道都准备好,那么sel......
  • VS2012、VS2013、VS2015、VS2019 代码自动注释插件【2】
    Git代码自动注释工具源码地址 VS2010、VS2012、VS2013的代码自动注释插件。安装该插件后,可以在VS的菜单中显示“注释”主菜单,可以给类、函数、成员添加标准的注释,与Doxygen配合使用,可以直接生成项目的注释文档。【插件下载】高版本的VS,可以下载源码后,自行编译使用。【插件安装】......
  • 3d打印 LCD2004/12864显示不清楚 正面看不清 背光太强 的问题
    第一次买相关配件,没经验解决方法:背面有一个调节显示电压的旋钮。背光强调低点,字体弱,调高点。背部调节电压的旋钮:   原因:用专业语言就是液晶屏鬼影和字浅,鬼影是本不该显示的内容显示出来了,一般是电路供给液晶屏的电压高于液晶屏的工作电压造成的;字浅就是液晶屏上的内容颜......
  • Failed to start bean 'documentationPluginsBootstrapper';nested exception is jav
    报错:Failedtostartbean‘documentationPluginsBootstrapper’;nestedexceptionisjava.lang.NullPointerException错误项目版本:springboot最新版本<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-sta......
  • Golang入门教程(一)GOPATH与工作空间(Windows)
    https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/01.2.md Windows环境:下面我就以mymath为例来讲述如何编写应用包:cd$GOPATH/srcmkdirmymath//我的环境:$GOPATH=c:\mygo新建文件sqrt.go,内容如下://$GOPATH/src/mymath/sqrt.go源码如......
  • [oeasy]python0127_中文系统_gbk_BIG5_南极星_内码转化
    中文系统bgk回忆上次内容汉字字形通过点阵式打字机像素级寻址的屏幕进入了计算机的世界在海峡对岸的台湾同胞也进入了汉字时代他们会使用GB2312编码吗?能互通吗?......
  • [oeasy]python0127_中文系统_gbk_BIG5_南极星_内码转化
    中文系统bgk回忆上次内容汉字字形通过点阵式打字机像素级寻址的屏幕进入了计算机的世界 ​ 添加图片注释,不超过140字(可选) 在海峡对岸的台湾同胞也进入了汉字时代 他们会使用GB2312编码吗?能互通吗?......
  • 如何在ubuntu22下安装docker版的golang来编译go语言写的代码
    为了让我们的ubuntu22系统更干净清爽我们使用docker首先使用snapinstalldocker安装docker后即可使用docker了docker命令的使用方法1:将你的代码下载到用户目录(~)下面(例如~/github/xixi/...)2:使用cd命令进到你代码需要运行gobuild的文件夹3:然后使用下面命令即可给你的代码编译......