Android蓝牙聊天应用权限问题求助:DeviceListActivity权限报错
解决蓝牙聊天应用中获取配对设备的权限错误问题
问题原因
在Android 12(API 31)及更高版本中,调用BluetoothAdapter.getBondedDevices()、BluetoothDevice.getName()和BluetoothDevice.getAddress()需要BLUETOOTH_CONNECT权限,你的代码仅在MainActivity中检查了位置权限,未针对该蓝牙权限做显式校验,导致触发权限安全警告。
解决方案
1. 在DeviceListActivity中添加权限检查逻辑
修改DeviceListActivity.java,在获取配对设备前先校验BLUETOOTH_CONNECT权限,并处理权限请求结果:
package com.example.bluetoothchatapp; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import android.Manifest; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.Context; import android.content.pm.PackageManager; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.Set; public class DeviceListActivity extends AppCompatActivity { private ListView listPairedDevices, listAvailableDevices; private ArrayAdapter<String> adapterPairedDevices; private ArrayAdapter<String> adapterAvailableDevices; private static final int REQUEST_BLUETOOTH_CONNECT = 102; private Context context; private BluetoothAdapter bluetoothAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_device_list); context = this; initViews(); checkBluetoothConnectPermission(); } private void initViews() { listAvailableDevices = findViewById(R.id.list_available_devices); listPairedDevices = findViewById(R.id.list_paired_devices); adapterAvailableDevices = new ArrayAdapter<>(context, R.layout.device_list_item); adapterPairedDevices = new ArrayAdapter<>(context, R.layout.device_list_item); listPairedDevices.setAdapter(adapterPairedDevices); listAvailableDevices.setAdapter(adapterAvailableDevices); // 修正原代码重复设置适配器的错误 bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); } private void checkBluetoothConnectPermission() { if (ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_CONNECT}, REQUEST_BLUETOOTH_CONNECT); } else { loadPairedDevices(); } } private void loadPairedDevices() { if (bluetoothAdapter == null) return; Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices(); if (pairedDevices != null && pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { String deviceInfo = device.getName() + "\n" + device.getAddress(); adapterPairedDevices.add(deviceInfo); } } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_BLUETOOTH_CONNECT) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { loadPairedDevices(); } } } }
2. 优化MainActivity的权限检查(可选)
为了在进入DeviceListActivity前确保所有必要权限已获取,可修改MainActivity的checkPermissions方法,同时请求位置权限和BLUETOOTH_CONNECT权限:
private void checkPermissions(){ String[] requiredPermissions = { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.BLUETOOTH_CONNECT }; boolean allGranted = true; for(String perm : requiredPermissions){ if(ContextCompat.checkSelfPermission(context, perm) != PackageManager.PERMISSION_GRANTED){ allGranted = false; break; } } if(!allGranted){ ActivityCompat.requestPermissions(MainActivity.this, requiredPermissions, REQUEST_LOCATION_PERMISSION); } else{ Intent intent = new Intent(context, DeviceListActivity.class); startActivity(intent); } }
关键说明
- Android 12+将蓝牙权限拆分为
BLUETOOTH_SCAN、BLUETOOTH_CONNECT、BLUETOOTH_ADVERTISE三个细分权限,不同操作对应不同权限要求 - 获取已配对设备信息、访问设备名称/地址属于蓝牙连接相关操作,必须校验
BLUETOOTH_CONNECT权限
内容的提问来源于stack exchange,提问作者Akash Soam
相关产品推荐
相关产品推荐

