Android蓝牙功能权限问题

Android Bluetooth function permission problem

我正在尝试建立蓝牙连接 android 应用程序。有一个按钮单击 'BluetoothSearchBtn' 事件处理程序。我想实现蓝牙搜索功能。 但是权限有问题,在代码下显示了红线。

出现问题的方法:
1。 BluetoothSearchBtn.setOnClickListener(新View.OnClickListener()){}
2。 private final BroadcastReceiver 接收器 = new BroadcastReceiver(){}

    Set<BluetoothDevice> pairedDevices;
    ArrayAdapter<String> btArrayAdapter;
    ArrayList<String> deviceAddressArray;
    BluetoothAdapter btAdapter;

    btAdapter = BluetoothAdapter.getDefaultAdapter();
    btArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
    deviceAddressArray = new ArrayList<>();

这是我的代码,其中显示了红线。

  1. BluetoothSearchBtn.setOnClickListener(新View.OnClickListener()
BluetoothSearchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Check if the device is already discovering
                if(btAdapter.isDiscovering()){
                    btAdapter.cancelDiscovery();
                } else {
                    if (btAdapter.isEnabled()) {
                        btAdapter.startDiscovery();
                        btArrayAdapter.clear();
                        if (deviceAddressArray != null && !deviceAddressArray.isEmpty()) {
                            deviceAddressArray.clear();
                        }
                        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
                        registerReceiver(receiver, filter);
                    } else {
                        Toast.makeText(getApplicationContext(), "bluetooth not on", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });

红色下划线位置:btAdpater.isDiscovering()btAdapter.cancelDiscovering()btAdatper.startDiscovering() 问题:调用需要可能被 user:code 拒绝的权限,应明确检查权限是否可用(使用“checkPersmission”)或明确处理潜在的 'SecurityException'.

  1. private final BroadcastReceiver 接收器 = new BroadcastReceiver()
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Discovery has found a device. Get the BluetoothDevice
                // object and its info from the Intent.
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String deviceName = device.getName();
                String deviceHardwareAddress = device.getAddress(); // MAC address
                btArrayAdapter.add(deviceName);
                deviceAddressArray.add(deviceHardwareAddress);
                btArrayAdapter.notifyDataSetChanged();
            }
        }
    };

红色下划线位置:device.getName() 问题:调用需要可能被 user:code 拒绝的权限,应明确检查权限是否可用(使用“checkPersmission”)或明确处理潜在的 'SecurityException'.

我已经在 AndroidManifest.xml

上添加了权限
<uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

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

    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

但我仍然无法找出权限有什么问题... 对不起我的英语不好..但我想知道出现这个问题的原因以及如何解决。

首先,你应该检查你是否有需要的权限,只需点击这个按钮添加权限检查:

或按 alt+enter,然后 Android Studio 将为您生成所需的代码

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }