Skip to content

Commit

Permalink
Changes for Android 12 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
weliem committed Aug 22, 2021
1 parent 0bf77d1 commit 73f3359
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 19 deletions.
12 changes: 8 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 30
compileSdkVersion 31
defaultConfig {
applicationId "com.welie.blessedexample"
minSdkVersion 21
targetSdkVersion 30
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -16,12 +16,16 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'com.jakewharton.timber:timber:4.7.1'
implementation project(':blessed')
testImplementation 'junit:junit:4.13.2'
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/com/welie/blessedexample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,11 @@ private String[] getMissingPermissions(String[] requiredPermissions) {

private String[] getRequiredPermissions() {
int targetSdkVersion = getApplicationInfo().targetSdkVersion;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && targetSdkVersion >= Build.VERSION_CODES.Q)
return new String[] {Manifest.permission.ACCESS_FINE_LOCATION};
else return new String[] {Manifest.permission.ACCESS_COARSE_LOCATION};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && targetSdkVersion >= Build.VERSION_CODES.S) {
return new String[]{Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT};
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && targetSdkVersion >= Build.VERSION_CODES.Q) {
return new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
} else return new String[]{Manifest.permission.ACCESS_COARSE_LOCATION};
}

private void permissionsGranted() {
Expand Down
12 changes: 7 additions & 5 deletions blessed/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 29
compileSdkVersion 31

defaultConfig {
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
targetSdkVersion 31

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand All @@ -18,6 +16,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}

dependencies {
Expand All @@ -26,5 +28,5 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
testImplementation "org.robolectric:robolectric:4.5.1"
testImplementation "org.mockito:mockito-core:3.8.0"
testImplementation 'androidx.test:core:1.3.0'
testImplementation 'androidx.test:core:1.4.0'
}
8 changes: 5 additions & 3 deletions blessed/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.welie.blessed" >

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation"
tools:targetApi="s" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,12 @@ public boolean isBluetoothEnabled() {

private boolean permissionsGranted() {
final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && targetSdkVersion >= Build.VERSION_CODES.Q) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && targetSdkVersion >= Build.VERSION_CODES.S) {
if (context.checkSelfPermission(Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
throw new SecurityException("app does not have BLUETOOTH_SCAN permission, cannot start scan");
} else return true;
}
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && targetSdkVersion >= Build.VERSION_CODES.Q) {
if (context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
throw new SecurityException("app does not have ACCESS_FINE_LOCATION permission, cannot start scan");
} else return true;
Expand Down Expand Up @@ -976,6 +981,8 @@ public void startPairingPopupHack() {
// Check if we are on a Samsung device because those don't need the hack
final String manufacturer = Build.MANUFACTURER;
if (!manufacturer.equalsIgnoreCase("samsung")) {
if (bleNotReady()) return;

bluetoothAdapter.startDiscovery();

callBackHandler.postDelayed(new Runnable() {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.3'
classpath 'com.android.tools.build:gradle:7.0.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip

0 comments on commit 73f3359

Please sign in to comment.