4

I created an application which enables Bluetooth and discovers other devices. In manifest I have the following:

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

However,on the device there is this exception:

11-20 08:08:47.766: E/AndroidRuntime(9380): FATAL EXCEPTION: main
11-20 08:08:47.766: E/AndroidRuntime(9380): java.lang.SecurityException: Need BLUETOOTH permission: Neither user 10111 nor current process has android.permission.BLUETOOTH.

what else do i have to add to the Manifest so that it works?

This is the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="cajun.meet"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">

    <activity android:name=".CajunMeetActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".MyULCard"
              android:label="@string/app_name">
    </activity>

    <activity android:name=".MyULContacts"
              android:label="@string/app_name">
    </activity>

    <service android:name = ".BluetoothExchange" android:exported="true" android:enabled="true">
    </service>

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

</application>
</manifest>
2
  • Can you post your manifest file. Commented Nov 20, 2011 at 14:20
  • 1
    Try to move the permission tags before the activities' tags Commented Nov 20, 2011 at 14:25

5 Answers 5

10

Try moving the permission outside the <application> tag. Like below:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="cajun.meet"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

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

<application android:icon="@drawable/icon" android:label="@string/app_name">

    <activity android:name=".CajunMeetActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".MyULCard"
              android:label="@string/app_name">
    </activity>

    <activity android:name=".MyULContacts"
              android:label="@string/app_name">
    </activity>

    <service android:name = ".BluetoothExchange" android:exported="true" android:enabled="true">
    </service>

</application>
</manifest>
Sign up to request clarification or add additional context in comments.

1 Comment

Mine has been outside the <application> tag but I am still have the same exact issue as the OP.
2

Move the uses-permission tags outside of the application element. Uses-permission is a child element of manifest, not application. See the full structure here.

Comments

2

Move it outside the application:

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

See here in docs that uses-permission is a child of <manifest>

Comments

1

if using mashmallow version of android then add this code:

this error occur due to security now in api 23 (mashmallow) version android improved there security they ask for permission

if (android.os.Build.VERSION.SDK_INT >= 23) {
        // only for gingerbread and newer versions
        String permission = Manifest.permission.READ_PHONE_STATE;
        if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.READ_PHONE_STATE}, MY_PERMISSIONS_REQUEST_READ_CONTACTS);
        } else {
            // Add your function here which open camera
        }
    } else {
        // Add your function here which open camera
    }

then add this method onRequestPermissionsResult

    @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED{ } else { Toast.makeText(getApplication(), "Permission required",Toast.LENGTH_LONG).show(); } return; } }}

1 Comment

BLUETOOTH and BLUETOOTH_ADMIN are not considered by Android "dangerous permissions" so it's not necessary to ask for explicit authorization to the user. See Android docs.
0

As many suggested already, moving the BT permissions outside the <application> tag is the key to getting rid of this SecurityException:

<uses-sdk android:minSdkVersion="8" />

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

<application android:icon="@drawable/icon" android:label="@string/app_name">

However... in my case I had my app using my own library (the only code that actually refers to BT) and I found out that it is critical to place these 2 permissions in the app's manifest, not in the library's manifest.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.