Madrugada
Madrugada

Reputation: 1289

Android permission error

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>

Upvotes: 4

Views: 17327

Answers (5)

WebViewer
WebViewer

Reputation: 811

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.

Upvotes: 0

Shubham Mahajan
Shubham Mahajan

Reputation: 311

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

Upvotes: 1

kaspermoerch
kaspermoerch

Reputation: 16570

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>

Upvotes: 10

MByD
MByD

Reputation: 137322

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>

Upvotes: 2

Chris
Chris

Reputation: 23171

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.

Upvotes: 2

Related Questions