Search code examples
androidbluetoothandroid-permissions

Android Bluetooth function permission problem


I'am trying to make bluetooth connection android App. There is a button click 'BluetoothSearchBtn' event handler. And I wanted to implement bluetooth searching function. But there is a problem with permission, and shows me redlines under the codes.

The problems occured methods:
1. BluetoothSearchBtn.setOnClickListener(new View.OnClickListener()){}
2. private final BroadcastReceiver receiver = new BroadcastReceiver(){}

  • variable declaration
    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<>();

This is my code which shows the redlines.

  1. BluetoothSearchBtn.setOnClickListener(new 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();
                    }
                }

            }
        });

Red underline locations : btAdpater.isDiscovering(), btAdapter.cancelDiscovering(), btAdatper.startDiscovering() Problem : Call requires permission which may be rejected by user:code should explicitly check to see if permission is available (with 'checkPersmission) or explicitly handle a potential 'SecurityException'.

  1. private final BroadcastReceiver receiver = 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();
            }
        }
    };

Red underline locations : device.getName() Problem : Call requires permission which may be rejected by user:code should explicitly check to see if permission is available (with 'checkPersmission) or explicitly handle a potential 'SecurityException'.

I have already added permissions on 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" />

But I can't still find out what is wrong with permissions... I'm sorry my english bad.. but I want to know the reason why this problem occurs and how to solve.


Solution

  • First of all, you should check whether you have required permission or not, just add permission check by clicking on this button:

    enter image description here

    or press alt+enter and then Android Studio will generate the needed code for you

    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;
            }