Skip to content

Commit

Permalink
compatible with android 12
Browse files Browse the repository at this point in the history
  • Loading branch information
xtc committed Nov 1, 2022
1 parent 493d276 commit eadc419
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 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 28
compileSdkVersion 31
defaultConfig {
applicationId "ttlock.demo"
minSdkVersion 23
targetSdkVersion 28
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
Expand Down
22 changes: 20 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,31 @@
xmlns:tools="http://schemas.android.com/tools"
package="ttlock.demo">

<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.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!--
Needed only if your app looks for Bluetooth devices.
You must add an attribute to this permission, or declare the
ACCESS_FINE_LOCATION permission, depending on the results when you
check location usage in your app.
-->
<uses-permission
android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation"
tools:targetApi="s" />
<!--
Needed only if your app communicates with already-paired Bluetooth
devices.
-->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

<application
android:name=".MyApplication"
tools:replace="android:allowBackup"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/ttlock/demo/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ttlock.demo;

import androidx.databinding.DataBindingUtil;

import android.Manifest;
import android.os.Bundle;

import ttlock.demo.databinding.ActivityMainBinding;
Expand All @@ -9,6 +11,7 @@
import ttlock.demo.iccard.ICCardActivity;
import ttlock.demo.lock.LockApiActivity;
import ttlock.demo.passcode.PasscodeActivity;
import ttlock.demo.utils.AppUtil;
import ttlock.demo.wireless_keyboard.WirelessKeyboardActivity;

;
Expand All @@ -21,6 +24,9 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
initListener();
if (AppUtil.isAndroid12OrOver()) {
AppUtil.checkPermission(this, Manifest.permission.BLUETOOTH_CONNECT);
}
}

private void initListener(){
Expand Down
14 changes: 11 additions & 3 deletions app/src/main/java/ttlock/demo/lock/ScanLockActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import ttlock.demo.lock.model.LockInitResultObj;
import ttlock.demo.retrofit.ApiService;
import ttlock.demo.retrofit.RetrofitAPIManager;
import ttlock.demo.utils.AppUtil;

public class ScanLockActivity extends BaseActivity implements LockListAdapter.onLockItemClick{
ActivityScanLockBinding binding;
Expand Down Expand Up @@ -88,9 +89,16 @@ private void initListener(){
*/
@TargetApi(Build.VERSION_CODES.M)
private void startScan(){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION_REQ_CODE);
return;
if (AppUtil.isAndroid12OrOver()) {//android 12 needs BLUETOOTH_SCAN permission
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.BLUETOOTH_SCAN}, REQUEST_PERMISSION_REQ_CODE);
return;
}
} else {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION_REQ_CODE);
return;
}
}

getScanLockCallback();
Expand Down
40 changes: 40 additions & 0 deletions app/src/main/java/ttlock/demo/utils/AppUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ttlock.demo.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.content.ContextCompat;

public class AppUtil {
public static final int REQUEST_PERMISSION_REQ_CODE = 1;

public static boolean isAndroid12OrOver() {
if (getAndroidSDKVersion() >= 31) {
return true;
}
return false;
}

public static int getAndroidSDKVersion() {
int version = 0;
try {
version = Integer.valueOf(Build.VERSION.SDK_INT);
} catch (NumberFormatException e) {
e.printStackTrace();
}
return version;
}

public static boolean checkPermission(Activity activity, String permission) {
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
activity.requestPermissions(new String[]{permission}, REQUEST_PERMISSION_REQ_CODE);
return false;
}
return true;
}


}
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ ext {
// Sdk and tools
// Support library and architecture components support minSdk 14 and above.
minSdkVersion = 18
targetSdkVersion = 23
compileSdkVersion = 28
buildToolsVersion = '28.0.3'
targetSdkVersion = 31
compileSdkVersion = 31
buildToolsVersion = '31.0.0'
// App dependencies
supportLibraryVersion = '28.0.0'
lifecycle_version = "1.1.1"
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Nov 29 12:55:43 CST 2019
#Tue Oct 18 16:32:06 CST 2022
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip

0 comments on commit eadc419

Please sign in to comment.