我的手机是三星s10,Android12,遇到的这个问题,供参考
问题出现场景,我是最近在研究一下蓝牙相关的,找了文档学习但是触动mBluetoothAdapter.enable();开启或者打开蓝牙的时候就报了下面这个错:
开始解错:
表面提示就是需要许可证/就是权限之类的缺少,
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)
然后我就顺着这个错误去网上查询资料是吧,就在清单文件里加了一堆蓝牙权限,文章来源:https://www.toymoban.com/news/detail-539453.html
<!-- 蓝牙权限 -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="BLUETOOTH_CONNECT" />
<!-- Required if your app derives physical location from Bluetooth
scan results. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
但是报错是依然的,后面在代码文件加了动态申请权限才解错ok:
重点来了:在初始化的代码里加上下面这个方法就不会报错了文章来源地址https://www.toymoban.com/news/detail-539453.html
// todo 蓝牙动态申请权限
private void initPermission(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){
// Android 版本大于等于 Android12 时
// 只包括蓝牙这部分的权限,其余的需要什么权限自己添加
mPermissionList.add(Manifest.permission.BLUETOOTH_SCAN);
mPermissionList.add(Manifest.permission.BLUETOOTH_ADVERTISE);
mPermissionList.add(Manifest.permission.BLUETOOTH_CONNECT);
} else {
// Android 版本小于 Android12 及以下版本
mPermissionList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
mPermissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if(mPermissionList.size() > 0){
ActivityCompat.requestPermissions(getActivity(),mPermissionList.toArray(new String[0]),1001);
}
}
到了这里,关于android设置打开蓝牙时报错:java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!