Skip to content

Commit

Permalink
Migrate to AndroidX
Browse files Browse the repository at this point in the history
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
  • Loading branch information
samueleperricone committed Nov 18, 2020
1 parent 6df1d34 commit 7432f22
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 22 deletions.
14 changes: 7 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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'
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="com.empatica.sample">

<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.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
Expand Down
36 changes: 23 additions & 13 deletions app/src/main/java/com/empatica/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -189,9 +190,6 @@ public void onClick(DialogInterface dialog, int which) {
@Override
protected void onPause() {
super.onPause();
if (deviceManager != null) {
deviceManager.stopScanning();
}
}

@Override
Expand All @@ -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();
Expand All @@ -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);
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
# org.gradle.parallel=true
android.enableJetifier=true
android.useAndroidX=true

0 comments on commit 7432f22

Please sign in to comment.