Search code examples
androidbluetoothandroid-permissions

missing permissions required by BluetoothAdapter.isEnabled.BLUETOOTH


I add bluetooth to my app but am running into the following problem. When I do the code:

BluetoothAdapter bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
    Toast.makeText(getApplicationContext(),"Device doesnt Support Bluetooth",Toast.LENGTH_SHORT).show();
}

if(!bluetoothAdapter.isEnabled()) {
    Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableAdapter, 0);
}

The error is on the following line:

if(!bluetoothAdapter.isEnabled())

Error:

Missing permissions required by BluetoothAdapter.isEnabled :android.permissions.BLUETOOTH

I added the following permissions to the android manifest file:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<permission android:name="android.permission.BLUETOOTH" android:label="BLUETOOTH" />
<permission android:name="android.permission.BLUETOOTH_ADMIN" />

but am still getting the same issue. Do you know what I did wrong?


Solution

    1. You only need <uses-permission> tag since you are using a permission. You don't need <permissions> tag since this is not your custom permission.
    2. Maybe you have placed <uses-permission> tag in the wrong element in manifest, but I cannot say anything else with the information you provided
    3. Since your bluetoothAdapter can be null, either add to second IF null pointer check, or add a RETURN in first IF
    4. It is also nice to have <uses-feature> tag