Skip to content

Commit

Permalink
Merge pull request #3 from empatica/fix/android-11
Browse files Browse the repository at this point in the history
Migrate to AndroidX
  • Loading branch information
samueleperricone authored Nov 24, 2020
2 parents 6df1d34 + 2069207 commit d37fed5
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 24 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
65 changes: 50 additions & 15 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,28 @@

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.ScanCallback;
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 +36,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 +162,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 +191,6 @@ public void onClick(DialogInterface dialog, int which) {
@Override
protected void onPause() {
super.onPause();
if (deviceManager != null) {
deviceManager.stopScanning();
}
}

@Override
Expand All @@ -202,11 +201,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,13 +227,35 @@ 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);
}
}
}

@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
Expand All @@ -235,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
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 d37fed5

Please sign in to comment.