From 7432f2236c3a165f99f979aeca9ffb73bbc1fecb Mon Sep 17 00:00:00 2001 From: Samuele Perricone Date: Wed, 18 Nov 2020 11:33:09 +0100 Subject: [PATCH 1/2] 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 From 2069207f5468b0248b44114c081e7be9b396d6fa Mon Sep 17 00:00:00 2001 From: Samuele Perricone Date: Wed, 18 Nov 2020 11:42:53 +0100 Subject: [PATCH 2/2] Add details about new function from `EmpaStatusDelegate` --- .../com/empatica/sample/MainActivity.java | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/empatica/sample/MainActivity.java b/app/src/main/java/com/empatica/sample/MainActivity.java index 2121ad7..8750eac 100644 --- a/app/src/main/java/com/empatica/sample/MainActivity.java +++ b/app/src/main/java/com/empatica/sample/MainActivity.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.app.AlertDialog; import android.bluetooth.BluetoothAdapter; +import android.bluetooth.le.ScanCallback; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; @@ -233,7 +234,28 @@ public void didDiscoverDevice(EmpaticaDevice bluetoothDevice, String deviceName, @Override public void didFailedScanning(int errorCode) { - + + /* + A system error occurred while scanning. + @see https://developer.android.com/reference/android/bluetooth/le/ScanCallback + */ + switch (errorCode) { + case ScanCallback.SCAN_FAILED_ALREADY_STARTED: + Log.e(TAG,"Scan failed: a BLE scan with the same settings is already started by the app"); + break; + case ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED: + Log.e(TAG,"Scan failed: app cannot be registered"); + break; + case ScanCallback.SCAN_FAILED_FEATURE_UNSUPPORTED: + Log.e(TAG,"Scan failed: power optimized scan feature is not supported"); + break; + case ScanCallback.SCAN_FAILED_INTERNAL_ERROR: + Log.e(TAG,"Scan failed: internal error"); + break; + default: + Log.e(TAG,"Scan failed with unknown error (errorCode=" + errorCode + ")"); + break; + } } @Override @@ -245,7 +267,10 @@ public void didRequestEnableBluetooth() { @Override public void bluetoothStateChanged() { - + // E4link detected a bluetooth adapter change + // Check bluetooth adapter and update your UI accordingly. + boolean isBluetoothOn = BluetoothAdapter.getDefaultAdapter().isEnabled(); + Log.i(TAG, "Bluetooth State Changed: " + isBluetoothOn); } @Override