Search code examples
flutterbluetooth-lowenergyandroid-permissionsandroid-12

lacks permission android.permission.BLUETOOTH in flutter when running on Android 12


I'm stuck in the following error...

E/BThermalPrinterPlugin( 9243): UID 10581 / PID 9243 lacks permission android.permission.BLUETOOTH

E/BThermalPrinterPlugin( 9243): java.lang.SecurityException: UID 10581 / PID 9243 lacks permission android.permission.BLUETOOTH

E/BThermalPrinterPlugin( 9243): at android.os.Parcel.createExceptionOrNull(Parcel.java:2426)

This is my AndroidManifest.xml :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.soerjdev.enakabe">
   <application
        android:label="Enakabe"
        android:icon="@mipmap/launcher_icon">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize"
            android:exported="true">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30"/>
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
</manifest>

And this is my build.gradle :

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 33

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.soerjdev.enakabe"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:multidex:2.0.1'
}

And this is dependencies in pubspeck yaml :

dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2
  flutter_typeahead: ^4.1.1
  shared_preferences: ^2.0.1
  go_router: ^5.1.5
  double_back_to_close_app: ^2.1.0
  provider: ^6.0.4
  image_picker: ^0.7.1
  http: ^0.13.5
  path_provider: ^2.0.11
  path: ^1.8.2
  flutter_spinkit: ^5.1.0
  #esc_pos_bluetooth: ^0.4.1
  #flutter_bluetooth_basic: ^0.1.7
  flutter_switch: ^0.3.2
  #flutter_scan_bluetooth: ^2.1.2
  #conditional_builder_null_safety: ^0.0.6
  intl: ^0.17.0
  bluetooth_print: ^4.0.1
  #flutter_blue: ^0.8.0
  blue_thermal_printer: ^1.2.3
  #rxdart: ^0.27.2
  esc_pos_utils_plus: ^2.0.1+6
  flutter_blue_plus: ^1.3.1
  qr_flutter: ^4.0.0
  image: ^3.2.2
  url_launcher: ^6.0.0
  scroll_snap_list: ^0.9.1
  permission_handler: ^10.2.0
  app_settings: ^4.1.8


dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_launcher_icons: "^0.11.0"

Someone please help me to solve this. Thanks...

I have tried so many solutions available in stack overflow, but could not overcome the error.


Solution

  • Use

    <uses-permission android:name="android.permission.BLUETOOTH" tools:remove="android:maxSdkVersion" />
    

    Don't forget to add xmlns on top of manifest

    xmlns:tools="http://schemas.android.com/tools"