From 7432f2236c3a165f99f979aeca9ffb73bbc1fecb Mon Sep 17 00:00:00 2001 From: Samuele Perricone Date: Wed, 18 Nov 2020 11:33:09 +0100 Subject: [PATCH] Migrate to AndroidX Android 11 as Target SDK (API 30) Use FINE LOCATION Permission to find Bluetooth Device on Android 10+ Use `onStop` instead of `onPause` to stop scanning in case the Activity is no longer visible --- app/build.gradle | 14 ++++---- app/src/main/AndroidManifest.xml | 1 + .../com/empatica/sample/MainActivity.java | 36 ++++++++++++------- build.gradle | 7 +++- gradle.properties | 4 ++- 5 files changed, 40 insertions(+), 22 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 3350ab0..3807bee 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,14 +1,14 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 28 - buildToolsVersion '29.0.3' + compileSdkVersion 30 + buildToolsVersion '30.0.2' defaultConfig { applicationId 'com.empatica.empalinksample' minSdkVersion 19 - targetSdkVersion 28 + targetSdkVersion 30 versionCode 2323 - versionName "1.1" + versionName "1.2" } buildTypes { release { @@ -19,8 +19,8 @@ android { productFlavors { } compileOptions { - sourceCompatibility JavaVersion.VERSION_1_6 - targetCompatibility JavaVersion.VERSION_1_6 + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 } } @@ -54,7 +54,7 @@ android { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation 'com.android.support:appcompat-v7:28.0.0' + implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.empatica.empalink:E4link:1.0.0@aar' implementation 'com.squareup.okhttp:okhttp:2.7.5' } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8f4d68e..e9ca5b0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ package="com.empatica.sample"> + diff --git a/app/src/main/java/com/empatica/sample/MainActivity.java b/app/src/main/java/com/empatica/sample/MainActivity.java index a859853..2121ad7 100644 --- a/app/src/main/java/com/empatica/sample/MainActivity.java +++ b/app/src/main/java/com/empatica/sample/MainActivity.java @@ -2,28 +2,27 @@ import android.Manifest; import android.app.Activity; +import android.app.AlertDialog; import android.bluetooth.BluetoothAdapter; -import android.bluetooth.BluetoothDevice; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; -import android.os.Handler; import android.provider.Settings; -import android.support.annotation.NonNull; -import android.support.v4.app.ActivityCompat; -import android.support.v4.content.ContextCompat; -import android.support.v7.app.AlertDialog; -import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; +import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; -import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; + import com.empatica.empalink.ConnectionNotAllowedException; import com.empatica.empalink.EmpaDeviceManager; import com.empatica.empalink.EmpaticaDevice; @@ -36,6 +35,8 @@ public class MainActivity extends AppCompatActivity implements EmpaDataDelegate, EmpaStatusDelegate { + private static final String TAG = "MainActivity"; + private static final int REQUEST_ENABLE_BT = 1; private static final int REQUEST_PERMISSION_ACCESS_COARSE_LOCATION = 1; @@ -160,8 +161,8 @@ public void onClick(DialogInterface dialog, int which) { private void initEmpaticaDeviceManager() { // Android 6 (API level 23) now require ACCESS_COARSE_LOCATION permission to use BLE - if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { - ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION }, REQUEST_PERMISSION_ACCESS_COARSE_LOCATION); + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, REQUEST_PERMISSION_ACCESS_COARSE_LOCATION); } else { if (TextUtils.isEmpty(EMPATICA_API_KEY)) { @@ -189,9 +190,6 @@ public void onClick(DialogInterface dialog, int which) { @Override protected void onPause() { super.onPause(); - if (deviceManager != null) { - deviceManager.stopScanning(); - } } @Override @@ -202,11 +200,22 @@ protected void onDestroy() { } } + @Override + protected void onStop() { + super.onStop(); + if (deviceManager != null) { + deviceManager.stopScanning(); + } + } + @Override public void didDiscoverDevice(EmpaticaDevice bluetoothDevice, String deviceName, int rssi, boolean allowed) { // Check if the discovered device can be used with your API key. If allowed is always false, // the device is not linked with your API key. Please check your developer area at // https://www.empatica.com/connect/developer.php + + Log.i(TAG, "didDiscoverDevice" + deviceName + "allowed: " + allowed); + if (allowed) { // Stop scanning. The first allowed device will do. deviceManager.stopScanning(); @@ -217,6 +226,7 @@ public void didDiscoverDevice(EmpaticaDevice bluetoothDevice, String deviceName, } catch (ConnectionNotAllowedException e) { // This should happen only if you try to connect when allowed == false. Toast.makeText(MainActivity.this, "Sorry, you can't connect to this device", Toast.LENGTH_SHORT).show(); + Log.e(TAG, "didDiscoverDevice" + deviceName + "allowed: " + allowed + " - ConnectionNotAllowedException", e); } } } diff --git a/build.gradle b/build.gradle index 230f4e5..c03e7a8 100644 --- a/build.gradle +++ b/build.gradle @@ -1,12 +1,17 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. + buildscript { + ext { + kotlin_version = '1.4.10' + } repositories { jcenter() google() } dependencies { - classpath 'com.android.tools.build:gradle:3.1.2' + classpath 'com.android.tools.build:gradle:4.1.1' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } diff --git a/gradle.properties b/gradle.properties index 1d3591c..915f0e6 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,4 +15,6 @@ # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects -# org.gradle.parallel=true \ No newline at end of file +# org.gradle.parallel=true +android.enableJetifier=true +android.useAndroidX=true \ No newline at end of file