Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK 31 Android 12 Solution #7

Closed
peekpt opened this issue Feb 7, 2022 · 3 comments
Closed

SDK 31 Android 12 Solution #7

peekpt opened this issue Feb 7, 2022 · 3 comments

Comments

@peekpt
Copy link

peekpt commented Feb 7, 2022

Since SDK 31 is required now:

( this was my solution to get it working on Android 12 and older devices. I'm open for suggestions.)


1. Go to Android Studio delete all other SDK and select to Install SDK31
(also upgrade Kotlin to 1.6.10 and all other things that it needs to upgrade)

check if you have installed sdk 31 with flutter doctor command in the terminal


2. Create a proguard-rules.pro file in android/app/
save it with this code:

-keepclassmembernames class com.boskokg.flutter_blue_plus.* { *; }
-keep class com.boskokg.flutter_blue_plus.** { *; }

3. Modify these gradle files:

android/app/build.gradle
(don't forget to change bundle name to yours)

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 flutter.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    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 "YOUR.BUNDLE.NAME" <-change this
        minSdkVersion 21
        targetSdkVersion 31
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    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

            // added things from here:

            minifyEnabled true
            useProguard true

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

android/build.gradle

buildscript {
    ext.kotlin_version = '1.6.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

android/gradle/wrapper/gradle-wrapper.properties

#Fri Jun 23 08:50:38 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip

4. Install permission_handler plugin from pub.dev


5. add this to your main function before runApp() or wherever you want to call for permissions.

if (Platform.isAndroid) {
    await Permission.locationWhenInUse.request();
    await Permission.bluetooth.request();
    await Permission.bluetoothScan.request();
    await Permission.bluetoothConnect.request();
  }

6. Add permissions to your android/app/src/main/AndroidManifest.xml

<manifest
(...)
     <!-- required for API 18 - 30 -->
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

    <!-- required for API 23 - 30 -->
    <uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <!-- API 31+ -->
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />

(...)


<application
      (...)
      <activity
        (...)
            android:exported="true"

@ViliusP
Copy link

ViliusP commented Feb 14, 2022

This solution didn't fully fix my problem because it didn't find any Eddystone beacons, so after long research I found this solution. It fixed my problem. Of course, I also followed and implemented peekpt solution.

@ekuleshov
Copy link

@peekpt what about this PR ?
pauldemarco/flutter_blue#940

It seem to fix Android 12 for me and best of all it does not rely on permission_handler plugin which requires some version-specific code for Android (i.e. different permissions between android versions).

If @espresso3389 don't have time to submit it here, I can try to do so if he is okay with it

@keval02
Copy link

keval02 commented Apr 18, 2022

Permission handler is not accepting the Bluetooth scan and bluetoothConnect permission even with the latest packages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants