Skip to content

Commit

Permalink
test_suite: add BluetoothTest APP based on AndroidStudio
Browse files Browse the repository at this point in the history
bug: v/52280

Signed-off-by: shenyangsi <[email protected]>
  • Loading branch information
yshome-hb committed Jan 16, 2025
1 parent 5194f75 commit 0af3bed
Show file tree
Hide file tree
Showing 72 changed files with 3,086 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tools/test_suite/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
*.iml
.gradle
/local.properties
/.idea/
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
1 change: 1 addition & 0 deletions tools/test_suite/android/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
52 changes: 52 additions & 0 deletions tools/test_suite/android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
apply plugin: 'com.android.application'

android {
namespace 'com.openvela.bluetoothtest'
compileSdk 34

defaultConfig {
applicationId "com.openvela.bluetoothtest"
minSdk 29
targetSdk 34
versionName "1.0"

testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
vectorDrawables.useSupportLibrary = true
multiDexEnabled true
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
jniDebuggable true
}
}

android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "BluetoothTest_${buildType.name}_v${defaultConfig.versionName}.apk"
}
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'

implementation project(':core')
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.12.0'
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

}

21 changes: 21 additions & 0 deletions tools/test_suite/android/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.openvela.bluetoothtest;

import android.content.Context;
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.openvela.bluetoothtest", appContext.getPackageName());
}
}
52 changes: 52 additions & 0 deletions tools/test_suite/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.BluetoothTest">

<activity android:name=".MainActivity"
android:exported="true"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".ble.BleScanActivity"
android:label="@string/ble_scan"
android:screenOrientation="portrait" />

<activity
android:name=".bredr.BredrInquiryActivity"
android:label="@string/bredr_inquiry"
android:screenOrientation="portrait" />

<activity
android:name=".ble.BleCentralActivity"
android:label="@string/ble_central"
android:screenOrientation="portrait" />

<activity
android:name=".ble.BlePeripheralActivity"
android:label="@string/ble_peripheral"
android:screenOrientation="portrait" />

</application>

</manifest>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/****************************************************************************
* Copyright (C) 2024 Xiaomi Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/

package com.openvela.bluetoothtest;

import java.util.ArrayList;
import java.util.List;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import android.bluetooth.BluetoothAdapter;

import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.openvela.bluetooth.BluetoothStateObserver;
import com.openvela.bluetooth.callback.BluetoothStateCallback;
import com.openvela.bluetoothtest.ble.BleScanActivity;
import com.openvela.bluetoothtest.ble.BlePeripheralActivity;
import com.openvela.bluetoothtest.bredr.BredrInquiryActivity;

public class MainActivity extends AppCompatActivity {
private final String TAG = MainActivity.class.getSimpleName();
private final int REQUEST_ENABLE_BT = 1;

private LinearLayout llBluetoothAdapterTip;
private BluetoothStateObserver btStateObserver;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
requestBluetoothPermission();
listenBluetoothState();
}

@Override
protected void onDestroy() {
super.onDestroy();
btStateObserver.unregisterReceiver();
}

private void initView() {
llBluetoothAdapterTip = findViewById(R.id.ll_adapter_tip);
TextView tvAdapterStates = findViewById(R.id.tv_adapter_states);

tvAdapterStates.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), REQUEST_ENABLE_BT);
}
});
}

@RequiresApi(api = Build.VERSION_CODES.S)
private void requestBluetoothPermission() {
List<String> permissions = new ArrayList<>();
permissions.add(Manifest.permission.BLUETOOTH_SCAN);
permissions.add(Manifest.permission.BLUETOOTH_ADVERTISE);
permissions.add(Manifest.permission.BLUETOOTH_CONNECT);
permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);

registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), map -> {
if (!isBluetoothEnabled()) {
llBluetoothAdapterTip.setVisibility(View.VISIBLE);
}
}).launch(permissions.toArray(new String[0]));
}

private void listenBluetoothState() {
btStateObserver = new BluetoothStateObserver(this);
btStateObserver.registerReceiver(new BluetoothStateCallback() {
@Override
public void onEnabled() {
Log.i(TAG, "BluetoothAdapter is enabled!");
llBluetoothAdapterTip.setVisibility(View.GONE);
}

@Override
public void onDisabled() {
Log.i(TAG, "BluetoothAdapter is disabled!");
llBluetoothAdapterTip.setVisibility(View.VISIBLE);
}
});
}

private boolean isBluetoothEnabled() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
return bluetoothAdapter != null && bluetoothAdapter.isEnabled();
}

public void entryBredrInquiryActivity(View view) {
startActivity(new Intent(this, BredrInquiryActivity.class));
}

public void entryBleCentralActivity(View view) {
startActivity(new Intent(this, BleScanActivity.class));
}

public void entryBlePeripheralActivity(View view) {
startActivity(new Intent(this, BlePeripheralActivity.class));
}
}
Loading

0 comments on commit 0af3bed

Please sign in to comment.