From 5e04f024d13aabbaf25ac9f9e229532c1568c90a Mon Sep 17 00:00:00 2001 From: weiwentan23 Date: Thu, 25 Nov 2021 11:54:17 +0800 Subject: [PATCH 01/20] Add a shimmer3 connection test app --- .../.idea/gradle.xml | 4 +- .../settings.gradle | 2 +- .../shimmerConnectionTest/.gitignore | 1 + .../shimmerConnectionTest/README.md | 22 ++ .../shimmerConnectionTest/build.gradle | 69 +++++ .../shimmerConnectionTest/proguard-rules.pro | 21 ++ .../src/main/AndroidManifest.xml | 25 ++ .../shimmerconnectiontest/MainActivity.java | 253 ++++++++++++++++++ .../drawable-v24/ic_launcher_foreground.xml | 34 +++ .../res/drawable/ic_launcher_background.xml | 170 ++++++++++++ .../src/main/res/layout/activity_main.xml | 188 +++++++++++++ .../res/mipmap-anydpi-v26/ic_launcher.xml | 5 + .../mipmap-anydpi-v26/ic_launcher_round.xml | 5 + .../src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3056 bytes .../res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 5024 bytes .../src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2096 bytes .../res/mipmap-mdpi/ic_launcher_round.png | Bin 0 -> 2858 bytes .../src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4569 bytes .../res/mipmap-xhdpi/ic_launcher_round.png | Bin 0 -> 7098 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 6464 bytes .../res/mipmap-xxhdpi/ic_launcher_round.png | Bin 0 -> 10676 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 9250 bytes .../res/mipmap-xxxhdpi/ic_launcher_round.png | Bin 0 -> 15523 bytes .../src/main/res/values/colors.xml | 6 + .../src/main/res/values/strings.xml | 3 + .../src/main/res/values/styles.xml | 8 + 26 files changed, 813 insertions(+), 3 deletions(-) create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/.gitignore create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/README.md create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/build.gradle create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/proguard-rules.pro create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/AndroidManifest.xml create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/java/shimmerresearch/com/shimmerconnectiontest/MainActivity.java create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/drawable/ic_launcher_background.xml create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/layout/activity_main.xml create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/mipmap-mdpi/ic_launcher_round.png create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/mipmap-xhdpi/ic_launcher_round.png create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/mipmap-xxhdpi/ic_launcher_round.png create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/values/colors.xml create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/values/strings.xml create mode 100644 ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/values/styles.xml diff --git a/ShimmerAndroidInstrumentDriver/.idea/gradle.xml b/ShimmerAndroidInstrumentDriver/.idea/gradle.xml index 167b8ae3..b8296709 100644 --- a/ShimmerAndroidInstrumentDriver/.idea/gradle.xml +++ b/ShimmerAndroidInstrumentDriver/.idea/gradle.xml @@ -4,7 +4,7 @@ diff --git a/ShimmerAndroidInstrumentDriver/settings.gradle b/ShimmerAndroidInstrumentDriver/settings.gradle index dcdbe5ee..5d51416d 100644 --- a/ShimmerAndroidInstrumentDriver/settings.gradle +++ b/ShimmerAndroidInstrumentDriver/settings.gradle @@ -1,5 +1,5 @@ include ':ShimmerAndroidInstrumentDriver', ':bluetoothManagerExample', ':shimmerServiceExample', ':shimmerLegacyExample', ':shimmerBasicExample', ':efficientDataArrayExample' -include ':shimmer3DOrientationExample' +include ':shimmer3DOrientationExample', ':shimmerConnectionTest' //Don't need shimmeradvancelibrary is just an example on how to import projects from different folders //include ':ShimmerAdvanceLibrary' //project (":ShimmerAdvanceLibrary").projectDir = new File("../../ShimmerAdvanceAPI/ShimmerAdvanceLibrary") diff --git a/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/.gitignore b/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/.gitignore @@ -0,0 +1 @@ +/build diff --git a/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/README.md b/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/README.md new file mode 100644 index 00000000..b1905f75 --- /dev/null +++ b/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/README.md @@ -0,0 +1,22 @@ +# Shimmer Basic Example +This example is only applicable for Shimmer3 devices onwards. For Shimmer2 devices, please see the Legacy Example + +- This example demonstrates connecting and streaming data from a Shimmer directly, without the use of the Bluetooth Manager +- The build.gradle file shows how to retrieve the library from bintray. Take note of the following:- + +``` +compile 'ShimmerAndroidInstrumentDriver:ShimmerAndroidInstrumentDriver:3.0.65Beta' +``` + +- Note you will need to specify the url for the shimmer bintray repository, this is done in the build.gradle file (root projects folder) +``` +allprojects { + repositories { + google() + jcenter() + maven { + url "http://dl.bintray.com/shimmerengineering/Shimmer" + } + } +} +``` \ No newline at end of file diff --git a/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/build.gradle b/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/build.gradle new file mode 100644 index 00000000..c1ac7e30 --- /dev/null +++ b/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/build.gradle @@ -0,0 +1,69 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 26 + + + + defaultConfig { + applicationId "shimmerresearch.com.shimmerconnectiontest" + minSdkVersion 17 + targetSdkVersion 26 + versionCode 1 + versionName "1.0" + multiDexEnabled true + + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + + packagingOptions { + exclude 'META-INF/DEPENDENCIES' + exclude 'META-INF/LICENSE' + exclude 'META-INF/LICENSE.txt' + exclude 'META-INF/license.txt' + exclude 'META-INF/NOTICE' + exclude 'META-INF/NOTICE.txt' + exclude 'META-INF/notice.txt' + exclude 'META-INF/ASL2.0' + } + + configurations.all { + resolutionStrategy.force 'com.android.support:support-annotations:25.3.1' + } + +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + + androidTestCompile 'com.android.support:support-annotations:25.3.1' + compile(group: 'com.shimmersensing', name: 'ShimmerAndroidInstrumentDriver', version: '3.0.71Beta', ext: 'aar') + implementation (group: 'com.shimmersensing', name: 'ShimmerBluetoothManager', version:'0.9.42beta'){ + // excluding org.json which is provided by Android + exclude group: 'io.netty' + exclude group: 'com.google.protobuf' + exclude group: 'org.apache.commons.math' + } + implementation (group: 'com.shimmersensing', name: 'ShimmerDriver', version:'0.9.138beta'){ + // excluding org.json which is provided by Android + exclude group: 'io.netty' + exclude group: 'com.google.protobuf' + exclude group: 'org.apache.commons.math' + } + implementation 'com.android.support.constraint:constraint-layout:1.0.2' + testImplementation 'junit:junit:4.12' + androidTestImplementation 'com.android.support.test:runner:1.0.1' + androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" +} +repositories { + mavenCentral() +} diff --git a/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/proguard-rules.pro b/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/proguard-rules.pro new file mode 100644 index 00000000..f1b42451 --- /dev/null +++ b/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/AndroidManifest.xml b/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/AndroidManifest.xml new file mode 100644 index 00000000..647a3145 --- /dev/null +++ b/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/AndroidManifest.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/java/shimmerresearch/com/shimmerconnectiontest/MainActivity.java b/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/java/shimmerresearch/com/shimmerconnectiontest/MainActivity.java new file mode 100644 index 00000000..3be21695 --- /dev/null +++ b/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/java/shimmerresearch/com/shimmerconnectiontest/MainActivity.java @@ -0,0 +1,253 @@ +package shimmerresearch.com.shimmerconnectiontest; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.text.InputType; +import android.util.Log; +import android.view.View; +import android.widget.TextView; +import android.widget.Toast; + +import com.shimmerresearch.android.Shimmer; +import com.shimmerresearch.android.guiUtilities.ShimmerBluetoothDialog; +import com.shimmerresearch.bluetooth.ShimmerBluetooth; +import com.shimmerresearch.driver.CallbackObject; +import com.shimmerresearch.driver.Configuration; +import com.shimmerresearch.driver.FormatCluster; +import com.shimmerresearch.driver.ObjectCluster; +import com.shimmerresearch.driver.ShimmerDevice; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Collection; +import java.util.Timer; +import java.util.TimerTask; + +import android.widget.EditText; + +import static com.shimmerresearch.android.guiUtilities.ShimmerBluetoothDialog.EXTRA_DEVICE_ADDRESS; + +public class MainActivity extends Activity { + + private final static String LOG_TAG = "ShimmerConnectionTest"; + + private EditText editTextShimmerStatus; + private EditText editTextInterval; + private EditText editTextSuccessCount; + private EditText editTextFailureCount; + private EditText editTextTotalIteration; + private EditText editTextTestProgress; + private EditText editTextFirmware; + + private int interval = 0; + private int successCount = 0; + private int failureCount = 0; + private int totalIteration = 0; + private int currentIteration = 0; + + private Shimmer shimmer; + private String macAdd; + private boolean isCurrentIterationSuccess; + private boolean isTestStarted = false; + private Timer timer; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + editTextShimmerStatus = (EditText) findViewById(R.id.shimmerStatus); + editTextInterval = (EditText) findViewById(R.id.interval); + editTextSuccessCount = (EditText) findViewById(R.id.success); + editTextFailureCount = (EditText) findViewById(R.id.fail); + editTextTotalIteration = (EditText) findViewById(R.id.testIterations); + editTextTestProgress = (EditText) findViewById(R.id.testProgress); + editTextFirmware = (EditText) findViewById(R.id.firmware); + + editTextTotalIteration.setText("10"); + editTextInterval.setText("5"); + } + + public void startTest(View v){ + if(!isTestStarted){ + Intent intent = new Intent(getApplicationContext(), ShimmerBluetoothDialog.class); + startActivityForResult(intent, ShimmerBluetoothDialog.REQUEST_CONNECT_SHIMMER); + } + } + + public void stopTest(View v){ + if(isTestStarted){ + editTextInterval.setEnabled(true); + editTextTotalIteration.setEnabled(true); + if(shimmer != null){ + shimmer.disconnect(); + } + if(timer != null){ + timer.cancel(); + } + isTestStarted = false; + } + } + + /** + * Messages from the Shimmer device including sensor data are received here + */ + Handler mHandler = new Handler() { + + @Override + public void handleMessage(Message msg) { + + switch (msg.what) { + case ShimmerBluetooth.MSG_IDENTIFIER_DATA_PACKET: + if ((msg.obj instanceof ObjectCluster)) { + + //Print data to Logcat + ObjectCluster objectCluster = (ObjectCluster) msg.obj; + + //Retrieve all possible formats for the current sensor device: + Collection allFormats = objectCluster.getCollectionOfFormatClusters(Configuration.Shimmer3.ObjectClusterSensorName.TIMESTAMP); + FormatCluster timeStampCluster = ((FormatCluster)ObjectCluster.returnFormatCluster(allFormats,"CAL")); + double timeStampData = timeStampCluster.mData; + Log.i(LOG_TAG, "Time Stamp: " + timeStampData); + allFormats = objectCluster.getCollectionOfFormatClusters(Configuration.Shimmer3.ObjectClusterSensorName.ACCEL_LN_X); + FormatCluster accelXCluster = ((FormatCluster)ObjectCluster.returnFormatCluster(allFormats,"CAL")); + if (accelXCluster!=null) { + double accelXData = accelXCluster.mData; + Log.i(LOG_TAG, "Accel LN X: " + accelXData); + } + } + break; + case Shimmer.MESSAGE_TOAST: + /** Toast messages sent from {@link Shimmer} are received here. E.g. device xxxx now streaming. + * Note that display of these Toast messages is done automatically in the Handler in {@link com.shimmerresearch.android.shimmerService.ShimmerService} */ + Toast.makeText(getApplicationContext(), msg.getData().getString(Shimmer.TOAST), Toast.LENGTH_SHORT).show(); + break; + case ShimmerBluetooth.MSG_IDENTIFIER_STATE_CHANGE: + ShimmerBluetooth.BT_STATE state = null; + String macAddress = ""; + + if (msg.obj instanceof ObjectCluster) { + state = ((ObjectCluster) msg.obj).mState; + macAddress = ((ObjectCluster) msg.obj).getMacAddress(); + } else if (msg.obj instanceof CallbackObject) { + state = ((CallbackObject) msg.obj).mState; + macAddress = ((CallbackObject) msg.obj).mBluetoothAddress; + } + + switch (state) { + case CONNECTED: + editTextShimmerStatus.setText("CONNECTED"); + editTextFirmware.setText(shimmer.getFirmwareVersionParsed()); +// if(!isCurrentIterationSuccess){ +// isCurrentIterationSuccess = true; +// editTextFirmware.setText(shimmer.getFirmwareVersionParsed()); +// successCount += 1; +// editTextSuccessCount.setText(String.valueOf(successCount)); +// } + break; + case CONNECTING: + editTextShimmerStatus.setText("CONNECTING"); + break; + case STREAMING: + editTextShimmerStatus.setText("STREAMING"); + break; + case STREAMING_AND_SDLOGGING: + editTextShimmerStatus.setText("STREAMING AND LOGGING"); + break; + case SDLOGGING: + editTextShimmerStatus.setText("SDLOGGING"); + break; + case DISCONNECTED: + editTextShimmerStatus.setText("DISCONNECTED"); + break; + } + break; + } + super.handleMessage(msg); + } + }; + + /** + * Get the result from the paired devices dialog + * @param requestCode + * @param resultCode + * @param data + */ + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if(requestCode == 2) { + if (resultCode == Activity.RESULT_OK) { + //Get the Bluetooth mac address of the selected device: + macAdd = data.getStringExtra(EXTRA_DEVICE_ADDRESS); + shimmer = new Shimmer(mHandler); + + totalIteration = Integer.parseInt(editTextTotalIteration.getText().toString()); + currentIteration = 0; + successCount = 0; + failureCount = 0; + editTextFailureCount.setText(String.valueOf(failureCount)); + editTextSuccessCount.setText(String.valueOf(successCount)); + editTextInterval.setEnabled(false); + editTextTotalIteration.setEnabled(false); + isCurrentIterationSuccess = true; + isTestStarted = true; + + timer = new Timer(); + timer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + if(currentIteration > 0){ + if(shimmer.getBluetoothRadioState() != ShimmerBluetooth.BT_STATE.CONNECTED){ + failureCount += 1; + runOnUiThread(new Runnable() { + public void run() { + editTextFailureCount.setText(String.valueOf(failureCount)); + } + }); + } + else{ + successCount += 1; + runOnUiThread(new Runnable() { + public void run() { + editTextSuccessCount.setText(String.valueOf(successCount)); + } + }); + } + } + + if(shimmer != null){ + shimmer.disconnect(); + } + + if(currentIteration == totalIteration){ + runOnUiThread(new Runnable() { + public void run() { + editTextInterval.setEnabled(true); + editTextTotalIteration.setEnabled(true); + } + }); + timer.cancel(); + isTestStarted = false; + return; + } + + runOnUiThread(new Runnable() { + public void run() { + editTextTestProgress.setText(String.valueOf(currentIteration + 1) + " of " + String.valueOf(totalIteration)); + currentIteration += 1; + } + }); + + //isCurrentIterationSuccess = false; + shimmer.connect(macAdd, "default"); + } + }, 0, Integer.parseInt(editTextInterval.getText().toString()) * 1000); + } + } + super.onActivityResult(requestCode, resultCode, data); + } +} diff --git a/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/drawable-v24/ic_launcher_foreground.xml b/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 00000000..c7bd21db --- /dev/null +++ b/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/drawable/ic_launcher_background.xml b/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 00000000..d5fccc53 --- /dev/null +++ b/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/layout/activity_main.xml b/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/layout/activity_main.xml new file mode 100644 index 00000000..39583691 --- /dev/null +++ b/ShimmerAndroidInstrumentDriver/shimmerConnectionTest/src/main/res/layout/activity_main.xml @@ -0,0 +1,188 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +