first
|
|
@ -0,0 +1,12 @@
|
|||
*.o
|
||||
*.d
|
||||
*.crf
|
||||
*.axf
|
||||
*.elf
|
||||
*.map
|
||||
*.lst
|
||||
*.bak
|
||||
*.lnp
|
||||
*.dep
|
||||
*.uvgui.*
|
||||
*.uvoptx
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
*.iml
|
||||
.gradle
|
||||
/local.properties
|
||||
/.idea/caches
|
||||
/.idea/libraries
|
||||
/.idea/modules.xml
|
||||
/.idea/workspace.xml
|
||||
/.idea/navEditor.xml
|
||||
/.idea/assetWizardSettings.xml
|
||||
.DS_Store
|
||||
/build
|
||||
/captures
|
||||
.externalNativeBuild
|
||||
.cxx
|
||||
local.properties
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="AndroidProjectSystem">
|
||||
<option name="providerId" value="com.android.tools.idea.GradleProjectSystem" />
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<bytecodeTargetLevel target="21" />
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="deploymentTargetSelector">
|
||||
<selectionStates>
|
||||
<SelectionState runConfigName="app">
|
||||
<option name="selectionMode" value="DROPDOWN" />
|
||||
</SelectionState>
|
||||
</selectionStates>
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="GradleMigrationSettings" migrationVersion="1" />
|
||||
<component name="GradleSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<GradleProjectSettings>
|
||||
<option name="testRunner" value="CHOOSE_PER_TEST" />
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="gradleJvm" value="#GRADLE_LOCAL_JAVA_HOME" />
|
||||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
<option value="$PROJECT_DIR$/app" />
|
||||
</set>
|
||||
</option>
|
||||
</GradleProjectSettings>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectMigrations">
|
||||
<option name="MigrateToGradleLocalJavaHome">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
</set>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="jbr-21" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||
</component>
|
||||
<component name="ProjectType">
|
||||
<option name="id" value="Android" />
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RunConfigurationProducerService">
|
||||
<option name="ignoredProducers">
|
||||
<set>
|
||||
<option value="com.intellij.execution.junit.AbstractAllInDirectoryConfigurationProducer" />
|
||||
<option value="com.intellij.execution.junit.AllInPackageConfigurationProducer" />
|
||||
<option value="com.intellij.execution.junit.PatternConfigurationProducer" />
|
||||
<option value="com.intellij.execution.junit.TestInClassConfigurationProducer" />
|
||||
<option value="com.intellij.execution.junit.UniqueIdConfigurationProducer" />
|
||||
<option value="com.intellij.execution.junit.testDiscovery.JUnitTestDiscoveryConfigurationProducer" />
|
||||
<option value="org.jetbrains.kotlin.idea.junit.KotlinJUnitRunConfigurationProducer" />
|
||||
<option value="org.jetbrains.kotlin.idea.junit.KotlinPatternConfigurationProducer" />
|
||||
</set>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
kotlin version: 2.0.21
|
||||
error message: The daemon has terminated unexpectedly on startup attempt #1 with error code: 0. The daemon process output:
|
||||
1. Kotlin compile daemon is ready
|
||||
|
||||
|
|
@ -0,0 +1 @@
|
|||
/build
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
plugins {
|
||||
alias(libs.plugins.android.application)
|
||||
alias(libs.plugins.kotlin.android)
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "com.example.cmake_project_test"
|
||||
compileSdk = 36
|
||||
|
||||
defaultConfig {
|
||||
applicationId = "com.example.cmake_project_test"
|
||||
minSdk = 24
|
||||
targetSdk = 36
|
||||
versionCode = 1
|
||||
versionName = "1.0"
|
||||
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
externalNativeBuild {
|
||||
cmake {
|
||||
cppFlags += "-std=c++17"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
isMinifyEnabled = false
|
||||
proguardFiles(
|
||||
getDefaultProguardFile("proguard-android-optimize.txt"),
|
||||
"proguard-rules.pro"
|
||||
)
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility = JavaVersion.VERSION_11
|
||||
targetCompatibility = JavaVersion.VERSION_11
|
||||
}
|
||||
kotlinOptions {
|
||||
jvmTarget = "11"
|
||||
}
|
||||
externalNativeBuild {
|
||||
cmake {
|
||||
path = file("src/main/cpp/CMakeLists.txt")
|
||||
version = "3.22.1"
|
||||
}
|
||||
}
|
||||
buildFeatures {
|
||||
viewBinding = true
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
implementation(libs.androidx.core.ktx)
|
||||
implementation(libs.androidx.appcompat)
|
||||
implementation(libs.material)
|
||||
implementation(libs.androidx.constraintlayout)
|
||||
testImplementation(libs.junit)
|
||||
androidTestImplementation(libs.androidx.junit)
|
||||
androidTestImplementation(libs.androidx.espresso.core)
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package com.example.cmake_project_test
|
||||
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
import org.junit.Assert.*
|
||||
|
||||
/**
|
||||
* Instrumented test, which will execute on an Android device.
|
||||
*
|
||||
* See [testing documentation](http://d.android.com/tools/testing).
|
||||
*/
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class ExampleInstrumentedTest {
|
||||
@Test
|
||||
fun useAppContext() {
|
||||
// Context of the app under test.
|
||||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
|
||||
assertEquals("com.example.cmake_project_test", appContext.packageName)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
<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.Cmake_project_test">
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
# For more information about using CMake with Android Studio, read the
|
||||
# documentation: https://d.android.com/studio/projects/add-native-code.html.
|
||||
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.
|
||||
|
||||
# Sets the minimum CMake version required for this project.
|
||||
cmake_minimum_required(VERSION 3.22.1)
|
||||
|
||||
# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
|
||||
# Since this is the top level CMakeLists.txt, the project name is also accessible
|
||||
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
|
||||
# build script scope).
|
||||
project("cmake_project_test")
|
||||
|
||||
# Creates and names a library, sets it as either STATIC
|
||||
# or SHARED, and provides the relative paths to its source code.
|
||||
# You can define multiple libraries, and CMake builds them for you.
|
||||
# Gradle automatically packages shared libraries with your APK.
|
||||
#
|
||||
# In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define
|
||||
# the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME}
|
||||
# is preferred for the same purpose.
|
||||
#
|
||||
# In order to load a library into your app from Java/Kotlin, you must call
|
||||
# System.loadLibrary() and pass the name of the library defined here;
|
||||
# For GameActivity/NativeActivity derived applications, the same library name must be
|
||||
# used in the AndroidManifest.xml file.
|
||||
|
||||
# Core static library (no JNI, pure C++)
|
||||
add_library(core_math STATIC
|
||||
src/add.cpp)
|
||||
|
||||
target_include_directories(core_math PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||
|
||||
# Data parser static library
|
||||
add_library(data_parser STATIC
|
||||
src/data_praser.cpp)
|
||||
|
||||
target_include_directories(data_parser PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||
|
||||
# JNI shared library: expose only JNI bridge, link against core
|
||||
add_library(${CMAKE_PROJECT_NAME} SHARED
|
||||
jni/jni_bridge.cpp)
|
||||
|
||||
# Specifies libraries CMake should link to your target library. You
|
||||
# can link libraries from various origins, such as libraries defined in this
|
||||
# build script, prebuilt third-party libraries, or Android system libraries.
|
||||
target_link_libraries(${CMAKE_PROJECT_NAME}
|
||||
core_math
|
||||
data_parser
|
||||
android
|
||||
log)
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
#pragma once
|
||||
|
||||
namespace core {
|
||||
namespace math {
|
||||
|
||||
int add(int a, int b);
|
||||
|
||||
} // namespace math
|
||||
} // namespace core
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,151 @@
|
|||
#ifndef DATA_PRASER_H
|
||||
#define DATA_PRASER_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <jni.h>
|
||||
#include <queue>
|
||||
#include <functional>
|
||||
|
||||
// 设备类型枚举
|
||||
enum class DataType {
|
||||
EEG, // 脑电
|
||||
ECG_2LEAD, // 胸腹心电/肌电
|
||||
ECG_12LEAD, // 12导联心电
|
||||
PPG, // 血氧
|
||||
RESPIRATION, // 呼吸/姿态
|
||||
SNORE, // 鼾声
|
||||
STETHOSCOPE, // 数字听诊
|
||||
MIT_BIH // 添加MIT-BIH心律失常数据集类型
|
||||
};
|
||||
|
||||
// 导联脱落状态
|
||||
struct LeadOffStatus {
|
||||
uint8_t status[2]; // 导联状态字节
|
||||
};
|
||||
|
||||
// 统一传感器数据结构
|
||||
struct SensorData {
|
||||
DataType data_type;
|
||||
uint16_t packet_sn; // 数据包序列号
|
||||
LeadOffStatus lead_status; // 导联状态
|
||||
int64_t timestamp; // 系统时间戳(微秒)
|
||||
float sqi; //信号质量
|
||||
|
||||
// 通道数据
|
||||
std::variant<
|
||||
std::vector<float>, // 单通道数据
|
||||
std::vector<std::vector<float>> // 多通道数据
|
||||
> channel_data;
|
||||
std::vector<uint8_t> raw_data; // 原始二进制数据
|
||||
// 附加数据
|
||||
struct {
|
||||
uint16_t movement; // 运动强度 (呼吸设备)
|
||||
uint8_t posture; // 姿态 (呼吸设备)
|
||||
uint8_t ambient_light; // 环境光 (呼吸设备)
|
||||
uint8_t hr; // 心率 (血氧)
|
||||
uint8_t spo2; // 血氧饱和度 (血氧)
|
||||
float temperature; // 温度 (修正为浮点类型)
|
||||
uint8_t gpio_state; // GPIO状态 (12导联心电)
|
||||
} additional;
|
||||
};
|
||||
|
||||
// 前向声明
|
||||
struct SensorData;
|
||||
struct FileManager;
|
||||
enum class DataType;
|
||||
|
||||
// 辅助函数:从字节数组读取小端整数
|
||||
template <typename T>
|
||||
T read_le(const uint8_t* data);
|
||||
|
||||
// 脑电设备解析 (0x4230)
|
||||
SensorData parse_eeg(const uint8_t* data);
|
||||
|
||||
// 心电设备解析 (0x4211)
|
||||
SensorData parse_ecg_emg(const uint8_t* data);
|
||||
|
||||
// 血氧设备解析(0x4302)
|
||||
SensorData parse_ppg(const uint8_t* data);
|
||||
|
||||
// 12导联心电解析 (0x4402)
|
||||
SensorData parse_12lead_ecg(const uint8_t* data);
|
||||
|
||||
// MIT-BIH 212格式解析器
|
||||
SensorData parse_mit_bih_212(const uint8_t* data, size_t size);
|
||||
|
||||
// 数字听诊解析 (0x1102)
|
||||
SensorData parse_stethoscope(const uint8_t* data);
|
||||
|
||||
// 鼾声解析 (0x4212)
|
||||
SensorData parse_snore(const uint8_t* data);
|
||||
|
||||
// 呼吸/姿态/环境光解析(0x4213)
|
||||
SensorData parse_respiration(const uint8_t* data);
|
||||
|
||||
// 统一解析入口函数 - 支持多个包头和数据包格式
|
||||
std::vector<SensorData> parse_device_data(const std::vector<uint8_t>& file_data);
|
||||
|
||||
// 工具函数:将数值转换为十六进制字符串
|
||||
std::string to_hex_string(uint16_t value);
|
||||
|
||||
// JNI辅助函数:将C++的SensorData转换为Java对象
|
||||
jobject convertSensorDataToJava(JNIEnv* env, const SensorData& sensorData);
|
||||
|
||||
// JNI辅助函数:将Java的byte数组转换为C++的vector
|
||||
std::vector<uint8_t> convertJavaByteArrayToVector(JNIEnv* env, jbyteArray byteArray);
|
||||
|
||||
// JNI辅助函数:将C++的vector<SensorData>转换为Java的List
|
||||
jobject convertSensorDataVectorToJavaList(JNIEnv* env, const std::vector<SensorData>& sensorDataVector);
|
||||
|
||||
|
||||
class StreamParser {
|
||||
public:
|
||||
StreamParser();
|
||||
~StreamParser();
|
||||
|
||||
// 添加新接收到的数据
|
||||
void appendData(const uint8_t* data, size_t length);
|
||||
|
||||
// 检查是否有完整的数据包可用
|
||||
bool hasCompletePacket() const;
|
||||
|
||||
// 获取下一个完整的数据包
|
||||
SensorData getNextPacket();
|
||||
|
||||
// 获取所有可用数据包
|
||||
std::vector<SensorData> getAllPackets();
|
||||
|
||||
// 设置数据包处理回调函数
|
||||
void setPacketCallback(std::function<void(const SensorData&)> callback);
|
||||
|
||||
// 清空缓冲区
|
||||
void clear();
|
||||
|
||||
// 新增:状态查询方法
|
||||
size_t getBufferSize() const;
|
||||
size_t getTotalPackets() const;
|
||||
size_t getPendingDataSize() const;
|
||||
bool isBufferEmpty() const;
|
||||
size_t getQueuedPacketCount() const;
|
||||
|
||||
private:
|
||||
std::vector<uint8_t> buffer_;
|
||||
std::queue<SensorData> complete_packets_;
|
||||
static const size_t PACKET_SIZE = 238; // 固定数据包大小
|
||||
std::function<void(const SensorData&)> packet_callback_;
|
||||
|
||||
// 新增:成员变量
|
||||
size_t buffer_size_; // 累计接收的数据大小
|
||||
size_t total_packets_; // 累计解析的数据包数量
|
||||
|
||||
// 内部解析方法
|
||||
void parseBuffer();
|
||||
|
||||
// 新增:数据包验证方法
|
||||
bool isValidPacket(const uint8_t* data);
|
||||
};
|
||||
|
||||
|
||||
#endif // DATA_PRASER_H
|
||||
|
|
@ -0,0 +1,146 @@
|
|||
#include <jni.h>
|
||||
#include <string>
|
||||
#include <android/log.h>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "../include/cpp/add.h"
|
||||
#include "../include/cpp/data_praser.h"
|
||||
|
||||
using core::math::add;
|
||||
|
||||
extern "C" JNIEXPORT jstring JNICALL
|
||||
Java_com_example_cmake_1project_1test_MainActivity_stringFromJNI(
|
||||
JNIEnv* env,
|
||||
jobject /* this */) {
|
||||
std::string hello = "Hello from C++ (multi-file)";
|
||||
return env->NewStringUTF(hello.c_str());
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
Java_com_example_cmake_1project_1test_MainActivity_addFromJNI(
|
||||
JNIEnv* env,
|
||||
jobject /* this */,
|
||||
jint a,
|
||||
jint b) {
|
||||
(void)env;
|
||||
return static_cast<jint>(add(static_cast<int>(a), static_cast<int>(b)));
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jobject JNICALL
|
||||
Java_com_example_cmake_1project_1test_MainActivity_parseDeviceDataFromJNI(
|
||||
JNIEnv* env,
|
||||
jobject /* this */,
|
||||
jbyteArray fileData) {
|
||||
|
||||
try {
|
||||
// 将Java的byte数组转换为C++的vector
|
||||
std::vector<uint8_t> cppFileData = convertJavaByteArrayToVector(env, fileData);
|
||||
|
||||
if (cppFileData.empty()) {
|
||||
__android_log_print(ANDROID_LOG_WARN, "DataParser", "Empty file data received");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// 调用C++的解析函数
|
||||
std::vector<SensorData> result = parse_device_data(cppFileData);
|
||||
|
||||
// 将结果转换为Java对象
|
||||
jobject javaResult = convertSensorDataVectorToJavaList(env, result);
|
||||
|
||||
if (javaResult == nullptr) {
|
||||
__android_log_print(ANDROID_LOG_ERROR, "DataParser", "Failed to convert result to Java object");
|
||||
} else {
|
||||
__android_log_print(ANDROID_LOG_INFO, "DataParser", "Successfully parsed %zu data packets", result.size());
|
||||
}
|
||||
|
||||
return javaResult;
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
__android_log_print(ANDROID_LOG_ERROR, "DataParser", "Exception during parsing: %s", e.what());
|
||||
return nullptr;
|
||||
} catch (...) {
|
||||
__android_log_print(ANDROID_LOG_ERROR, "DataParser", "Unknown exception during parsing");
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ================= 流式解析 JNI 接口 =================
|
||||
|
||||
// 创建解析器实例,返回 native 句柄
|
||||
extern "C" JNIEXPORT jlong JNICALL
|
||||
Java_com_example_cmake_1project_1test_MainActivity_createStreamParser(
|
||||
JNIEnv* env,
|
||||
jobject /* this */) {
|
||||
(void)env;
|
||||
try {
|
||||
auto* parser = new StreamParser();
|
||||
return reinterpret_cast<jlong>(parser);
|
||||
} catch (...) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 释放解析器实例
|
||||
extern "C" JNIEXPORT void JNICALL
|
||||
Java_com_example_cmake_1project_1test_MainActivity_destroyStreamParser(
|
||||
JNIEnv* env,
|
||||
jobject /* this */,
|
||||
jlong handle) {
|
||||
(void)env;
|
||||
auto* parser = reinterpret_cast<StreamParser*>(handle);
|
||||
delete parser;
|
||||
}
|
||||
|
||||
// 追加一段字节流数据
|
||||
extern "C" JNIEXPORT void JNICALL
|
||||
Java_com_example_cmake_1project_1test_MainActivity_streamParserAppend(
|
||||
JNIEnv* env,
|
||||
jobject /* this */,
|
||||
jlong handle,
|
||||
jbyteArray chunk) {
|
||||
auto* parser = reinterpret_cast<StreamParser*>(handle);
|
||||
if (!parser || !chunk) return;
|
||||
|
||||
jsize len = env->GetArrayLength(chunk);
|
||||
if (len <= 0) return;
|
||||
std::vector<uint8_t> buf(static_cast<size_t>(len));
|
||||
env->GetByteArrayRegion(chunk, 0, len, reinterpret_cast<jbyte*>(buf.data()));
|
||||
parser->appendData(buf.data(), buf.size());
|
||||
}
|
||||
|
||||
// 拉取已完成的所有数据包,转换为 Java List<SensorData>
|
||||
extern "C" JNIEXPORT jobject JNICALL
|
||||
Java_com_example_cmake_1project_1test_MainActivity_streamParserDrainPackets(
|
||||
JNIEnv* env,
|
||||
jobject /* this */,
|
||||
jlong handle) {
|
||||
auto* parser = reinterpret_cast<StreamParser*>(handle);
|
||||
if (!parser) return nullptr;
|
||||
std::vector<SensorData> packets = parser->getAllPackets();
|
||||
return convertSensorDataVectorToJavaList(env, packets);
|
||||
}
|
||||
|
||||
// 便捷方法:从整块数据按固定块大小模拟流式解析,返回解析到的所有包
|
||||
extern "C" JNIEXPORT jobject JNICALL
|
||||
Java_com_example_cmake_1project_1test_MainActivity_parseStreamFromBytes(
|
||||
JNIEnv* env,
|
||||
jobject /* this */,
|
||||
jbyteArray data,
|
||||
jint chunkSize) {
|
||||
if (data == nullptr || chunkSize <= 0) return nullptr;
|
||||
|
||||
std::vector<uint8_t> src = convertJavaByteArrayToVector(env, data);
|
||||
auto* parser = new StreamParser();
|
||||
size_t offset = 0;
|
||||
while (offset < src.size()) {
|
||||
size_t n = std::min(static_cast<size_t>(chunkSize), src.size() - offset);
|
||||
parser->appendData(src.data() + offset, n);
|
||||
offset += n;
|
||||
}
|
||||
std::vector<SensorData> packets = parser->getAllPackets();
|
||||
delete parser;
|
||||
return convertSensorDataVectorToJavaList(env, packets);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
#include "../include/cpp/add.h"
|
||||
|
||||
namespace core {
|
||||
namespace math {
|
||||
|
||||
int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
} // namespace math
|
||||
} // namespace core
|
||||
|
|
@ -0,0 +1,919 @@
|
|||
#include "../include/cpp/data_praser.h"
|
||||
#include <cstring> // 添加 memcpy 头文件
|
||||
#include <sstream> // 添加 stringstream 头文件
|
||||
#include <iomanip> // 添加 iomanip 头文件
|
||||
#include <jni.h>
|
||||
#include <android/log.h>
|
||||
#include <map>
|
||||
|
||||
// 辅助函数:从字节数组读取小端整数
|
||||
template <typename T>
|
||||
T read_le(const uint8_t* data) {
|
||||
T value = 0;
|
||||
for (size_t i = 0; i < sizeof(T); ++i) {
|
||||
value |= static_cast<T>(data[i]) << (i * 8);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
// 脑电设备解析 (0x4230)
|
||||
SensorData parse_eeg(const uint8_t* data) {
|
||||
SensorData result;
|
||||
result.data_type = DataType::EEG;
|
||||
result.packet_sn = read_le<uint16_t>(data);
|
||||
|
||||
// 跳过 DataType(2) 和 data_len(2)
|
||||
const uint8_t* payload = data + 6;
|
||||
|
||||
// 导联状态
|
||||
std::memcpy(result.lead_status.status, payload, 2);
|
||||
payload += 2;
|
||||
|
||||
// 解析EEG数据 (6通道 × 14采样点)
|
||||
auto& eeg_data = result.channel_data.emplace<std::vector<std::vector<float>>>();
|
||||
eeg_data.resize(6);
|
||||
|
||||
for (int ch = 0; ch < 6; ++ch) {
|
||||
eeg_data[ch].reserve(14);
|
||||
for (int i = 0; i < 14; ++i) {
|
||||
int16_t adc_value = read_le<int16_t>(payload);
|
||||
payload += 2;
|
||||
eeg_data[ch].push_back(adc_value * 0.318f); // 转换为μV
|
||||
}
|
||||
}
|
||||
|
||||
// 解析EOG数据 (2通道 × 14采样点)
|
||||
for (int ch = 0; ch < 2; ++ch) {
|
||||
eeg_data.push_back({}); // 添加新通道
|
||||
for (int i = 0; i < 14; ++i) {
|
||||
int16_t adc_value = read_le<int16_t>(payload);
|
||||
payload += 2;
|
||||
eeg_data.back().push_back(adc_value * 0.318f); // 转换为μV
|
||||
}
|
||||
}
|
||||
|
||||
// 跳过预留区(6字节)
|
||||
payload += 6;
|
||||
|
||||
// 赋值原始二进制数据
|
||||
result.raw_data.assign(data, data + 6 + 2 + 6 * 14 * 2 + 2 * 14 * 2 + 6);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// 心电设备解析 (0x4211)
|
||||
SensorData parse_ecg_emg(const uint8_t* data) {
|
||||
SensorData result;
|
||||
result.data_type = DataType::ECG_2LEAD;
|
||||
result.packet_sn = read_le<uint16_t>(data);
|
||||
|
||||
// 跳过 data_type(2) 和 data_len(2)
|
||||
const uint8_t* payload = data + 6;
|
||||
|
||||
// 导联状态
|
||||
std::memcpy(result.lead_status.status, payload, 2);
|
||||
payload += 2;
|
||||
|
||||
// 解析数据 (多通道混合)
|
||||
auto& channels = result.channel_data.emplace<std::vector<std::vector<float>>>();
|
||||
|
||||
// ECG1 (25采样点)
|
||||
channels.push_back({});
|
||||
for (int i = 0; i < 25; ++i) {
|
||||
int16_t adc_value = read_le<int16_t>(payload);
|
||||
payload += 2;
|
||||
channels.back().push_back(adc_value * 0.318f); // μV
|
||||
}
|
||||
|
||||
// ECG2 (25采样点)
|
||||
channels.push_back({});
|
||||
for (int i = 0; i < 25; ++i) {
|
||||
int16_t adc_value = read_le<int16_t>(payload);
|
||||
payload += 2;
|
||||
channels.back().push_back(adc_value * 0.318f); // μV
|
||||
}
|
||||
|
||||
// EMG1 (25采样点)
|
||||
channels.push_back({});
|
||||
for (int i = 0; i < 25; ++i) {
|
||||
int16_t adc_value = read_le<int16_t>(payload);
|
||||
payload += 2;
|
||||
channels.back().push_back(adc_value * 0.318f); // μV
|
||||
}
|
||||
|
||||
// EMG2 (25采样点)
|
||||
channels.push_back({});
|
||||
for (int i = 0; i < 25; ++i) {
|
||||
int16_t adc_value = read_le<int16_t>(payload);
|
||||
payload += 2;
|
||||
channels.back().push_back(adc_value * 0.318f); // μV
|
||||
}
|
||||
|
||||
// 呼吸温度 (5采样点)
|
||||
channels.push_back({});
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
int16_t temp_value = read_le<int16_t>(payload);
|
||||
payload += 2;
|
||||
channels.back().push_back(temp_value * 0.477f); // 存储所有采样点
|
||||
}
|
||||
|
||||
// 呼吸阻抗1 (5采样点)
|
||||
channels.push_back({});
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
int16_t imp_value = read_le<int16_t>(payload);
|
||||
payload += 2;
|
||||
channels.back().push_back(static_cast<float>(imp_value)); // 原始值
|
||||
}
|
||||
|
||||
// 呼吸阻抗2 (5采样点)
|
||||
channels.push_back({});
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
int16_t imp_value = read_le<int16_t>(payload);
|
||||
payload += 2;
|
||||
channels.back().push_back(static_cast<float>(imp_value)); // 原始值
|
||||
}
|
||||
|
||||
// 赋值原始二进制数据
|
||||
result.raw_data.assign(data, data + 238); // 完整238字节
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// 血氧设备解析(0x4302)
|
||||
SensorData parse_ppg(const uint8_t* data) {
|
||||
SensorData result;
|
||||
result.data_type = DataType::PPG;
|
||||
result.packet_sn = read_le<uint16_t>(data);
|
||||
|
||||
// 跳过 DataType(2) 和 data_len(2)
|
||||
const uint8_t* payload = data + 6;
|
||||
|
||||
// 读取生命体征
|
||||
result.additional.hr = *payload++;
|
||||
result.additional.spo2 = *payload++;
|
||||
|
||||
// 注意:温度值放大100倍存储,需要除以100
|
||||
int16_t temp_raw = read_le<int16_t>(payload);
|
||||
result.additional.temperature = static_cast<float>(temp_raw) / 100.0f;
|
||||
payload += 2;
|
||||
|
||||
// 解析红光数据 (57采样点)
|
||||
auto& channels = result.channel_data.emplace<std::vector<std::vector<float>>>();
|
||||
channels.push_back({});
|
||||
for (int i = 0; i < 57; ++i) {
|
||||
int16_t adc_value = read_le<int16_t>(payload);
|
||||
payload += 2;
|
||||
channels.back().push_back(adc_value * 0.879f); // mV
|
||||
}
|
||||
|
||||
// 解析红外光数据(57采样点)
|
||||
channels.push_back({});
|
||||
for (int i = 0; i < 57; ++i) {
|
||||
int16_t adc_value = read_le<int16_t>(payload);
|
||||
payload += 2;
|
||||
channels.back().push_back(adc_value * 0.879f); // mV
|
||||
}
|
||||
|
||||
// 赋值原始二进制数据
|
||||
result.raw_data.assign(data, data + 6 + 4 + 57 * 2 * 2);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// 12导联心电解析 (0x4402)
|
||||
SensorData parse_12lead_ecg(const uint8_t* data) {
|
||||
SensorData result;
|
||||
result.data_type = DataType::ECG_12LEAD;
|
||||
result.packet_sn = read_le<uint16_t>(data);
|
||||
|
||||
// 跳过 DataType(2) 和 data_len(2)
|
||||
const uint8_t* payload = data + 6;
|
||||
|
||||
// 导联状态(按文档说明处理STAT寄存器)
|
||||
uint8_t stat_dh = payload[0]; // STAT.DH
|
||||
uint8_t stat_dm = payload[1]; // STAT.DM
|
||||
|
||||
// 正确拼接方式: (STAT.DH << 4) | (STAT.DM >> 4)
|
||||
result.lead_status.status[0] = (stat_dh << 4) | (stat_dm >> 4) ;
|
||||
|
||||
result.lead_status.status[1] = 0; // 第二个字节未使用
|
||||
payload += 2;
|
||||
|
||||
// 解析8通道ECG数据
|
||||
auto& channels = result.channel_data.emplace<std::vector<std::vector<float>>>();
|
||||
channels.resize(8);
|
||||
|
||||
for (int ch = 0; ch < 8; ++ch) {
|
||||
channels[ch].reserve(14);
|
||||
for (int i = 0; i < 14; ++i) {
|
||||
int16_t adc_value = read_le<int16_t>(payload);
|
||||
payload += 2;
|
||||
channels[ch].push_back(adc_value * 0.318f); //原始数据进行量纲转换
|
||||
}
|
||||
}
|
||||
|
||||
// GPIO状态(取低4位)
|
||||
result.additional.gpio_state = *payload & 0x0F;
|
||||
payload += 1;
|
||||
|
||||
// 跳过预留区(5字节)
|
||||
payload += 5;
|
||||
|
||||
// 赋值原始二进制数据
|
||||
result.raw_data.assign(data, data + 6 + 2 + 8 * 14 * 2 + 1 + 5);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// 数字听诊解析 (0x1102)
|
||||
SensorData parse_stethoscope(const uint8_t* data) {
|
||||
SensorData result;
|
||||
result.data_type = DataType::STETHOSCOPE;
|
||||
result.packet_sn = read_le<uint16_t>(data);
|
||||
|
||||
// 跳过 DataType(2) 和 data_len(2)
|
||||
const uint8_t* payload = data + 6;
|
||||
|
||||
// 解析双通道音频数据
|
||||
auto& channels = result.channel_data.emplace<std::vector<std::vector<float>>>();
|
||||
channels.resize(2);
|
||||
|
||||
// 通道1数据 (116采样点)
|
||||
for (int i = 0; i < 116; ++i) {
|
||||
int8_t sample = static_cast<int8_t>(*payload++);
|
||||
channels[0].push_back(sample * 0.146f); // mV
|
||||
}
|
||||
|
||||
// 通道2数据 (116采样点)
|
||||
for (int i = 0; i < 116; ++i) {
|
||||
int8_t sample = static_cast<int8_t>(*payload++);
|
||||
channels[1].push_back(sample * 0.146f); // mV
|
||||
}
|
||||
|
||||
// 赋值原始二进制数据
|
||||
result.raw_data.assign(data, data + 6 + 116 * 2);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// 鼾声解析 (0x4212)
|
||||
SensorData parse_snore(const uint8_t* data) {
|
||||
SensorData result;
|
||||
result.data_type = DataType::SNORE;
|
||||
result.packet_sn = read_le<uint16_t>(data);
|
||||
|
||||
// 跳过 data_type(2) + data_len(2)
|
||||
const uint8_t* payload = data + 6;
|
||||
|
||||
// 解析鼾声数据 (232字节)
|
||||
auto& channels = result.channel_data.emplace<std::vector<float>>();
|
||||
|
||||
// 高效转换: 直接使用 reinterpret_cast
|
||||
const int8_t* snore_data = reinterpret_cast<const int8_t*>(payload);
|
||||
for (int i = 0; i < 232; ++i) {
|
||||
channels.push_back(static_cast<float>(snore_data[i]));
|
||||
}
|
||||
|
||||
// 赋值原始二进制数据
|
||||
result.raw_data.assign(data, data + 6 + 232);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// 呼吸/姿态/环境光解析(0x4213)
|
||||
SensorData parse_respiration(const uint8_t* data) {
|
||||
SensorData result;
|
||||
result.data_type = DataType::RESPIRATION;
|
||||
result.packet_sn = read_le<uint16_t>(data);
|
||||
|
||||
// 跳过 DataType(2) 和 data_len(2)
|
||||
const uint8_t* payload = data + 6;
|
||||
|
||||
// 解析呼吸气流数据 (114采样点)
|
||||
auto& channels = result.channel_data.emplace<std::vector<std::vector<float>>>();
|
||||
channels.push_back({});
|
||||
|
||||
for (int i = 0; i < 114; ++i) {
|
||||
int16_t adc_value = read_le<int16_t>(payload);
|
||||
payload += 2;
|
||||
channels.back().push_back(adc_value * 0.1f); // 实际单位
|
||||
}
|
||||
|
||||
// 解析附加数据
|
||||
result.additional.movement = read_le<uint16_t>(payload); // 运动强度
|
||||
payload += 2;
|
||||
|
||||
result.additional.posture = *payload++; // 姿态数据
|
||||
result.additional.ambient_light = *payload++; // 环境光数据
|
||||
|
||||
// 赋值原始二进制数据
|
||||
result.raw_data.assign(data, data + 6 + 114 * 2 + 2 + 1 + 1);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// 统一解析入口函数 - 支持多个包头和数据包格式
|
||||
std::vector<SensorData> parse_device_data(const std::vector<uint8_t>& file_data) {
|
||||
const size_t PACKET_SIZE = 238; // 每个数据包固定大小
|
||||
const size_t RESPONSE_HEADER_SIZE = 10; // 响应包头大小 (2功能码+ 2数据长度 + 4实际采集点数+crc校验)
|
||||
const uint16_t FUNCTION_CODE = 0x0010; // 获取数据功能码
|
||||
|
||||
const size_t file_size = file_data.size();
|
||||
const uint8_t* ptr = file_data.data();
|
||||
const uint8_t* end_ptr = ptr + file_size;
|
||||
std::map<DataType, std::vector<SensorData>> grouped_data;
|
||||
std::vector<SensorData> results;
|
||||
while (ptr < end_ptr) {
|
||||
// 检查是否有响应包头
|
||||
bool has_response_header = false;
|
||||
size_t remaining = end_ptr - ptr;
|
||||
|
||||
// 确保有足够的空间检查包头
|
||||
if (remaining >= RESPONSE_HEADER_SIZE) {
|
||||
// 读取功能码(小端)
|
||||
uint16_t func_code = read_le<uint16_t>(ptr);
|
||||
// 读取数据长度 (小端)
|
||||
uint16_t data_len = read_le<uint16_t>(ptr + 2);
|
||||
|
||||
// 检查是否符合0x0010响应包特征
|
||||
if (func_code == FUNCTION_CODE && data_len == 0x0004) {
|
||||
has_response_header = true;
|
||||
|
||||
// 读取实际数据点数 (小端)
|
||||
uint32_t actual_points = read_le<uint32_t>(ptr + 4);
|
||||
|
||||
// 计算期望的数据长度(实际点数 * 包大小)
|
||||
size_t expected_data_size = actual_points * PACKET_SIZE;
|
||||
size_t remaining_after_header = remaining - RESPONSE_HEADER_SIZE;
|
||||
|
||||
// 验证数据长度是否匹配
|
||||
if (remaining_after_header < expected_data_size) {
|
||||
std::string error = "包头数据不足: 预期 " +
|
||||
std::to_string(expected_data_size) +
|
||||
" 字节, 实际 " +
|
||||
std::to_string(remaining_after_header) +
|
||||
" 字节";
|
||||
throw std::runtime_error(error);
|
||||
}
|
||||
|
||||
// 跳过包头
|
||||
ptr += RESPONSE_HEADER_SIZE;
|
||||
|
||||
// 处理包头后的数据包
|
||||
for (uint32_t i = 0; i < actual_points; i++) {
|
||||
if (end_ptr - ptr < static_cast<ptrdiff_t>(PACKET_SIZE)) {
|
||||
throw std::runtime_error("数据包不完整");
|
||||
}
|
||||
|
||||
// 读取数据类型标识
|
||||
const uint16_t data_type = read_le<uint16_t>(ptr + 2);
|
||||
|
||||
switch (data_type) {
|
||||
case 0x4230:
|
||||
grouped_data[DataType::EEG].push_back(parse_eeg(ptr));
|
||||
break;
|
||||
case 0x4211:
|
||||
grouped_data[DataType::ECG_2LEAD].push_back(parse_ecg_emg(ptr));
|
||||
break;
|
||||
case 0x4212:
|
||||
grouped_data[DataType::SNORE].push_back(parse_snore(ptr));
|
||||
break;
|
||||
case 0x4213:
|
||||
grouped_data[DataType::RESPIRATION].push_back(parse_respiration(ptr));
|
||||
break;
|
||||
case 0x4402:
|
||||
grouped_data[DataType::ECG_12LEAD].push_back(parse_12lead_ecg(ptr));
|
||||
break;
|
||||
case 0x4302:
|
||||
grouped_data[DataType::PPG].push_back(parse_ppg(ptr));
|
||||
break;
|
||||
case 0x1102:
|
||||
grouped_data[DataType::STETHOSCOPE].push_back(parse_stethoscope(ptr));
|
||||
break;
|
||||
default:
|
||||
throw std::runtime_error("未知设备类型: 0x" +
|
||||
to_hex_string(data_type));
|
||||
}
|
||||
ptr += PACKET_SIZE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有检测到响应包头,尝试按线数据包处理
|
||||
if (!has_response_header) {
|
||||
// 检查是否有完整的数据包
|
||||
if (remaining >= PACKET_SIZE) {
|
||||
// 读取数据类型标识
|
||||
const uint16_t data_type = read_le<uint16_t>(ptr + 2);
|
||||
|
||||
switch (data_type) {
|
||||
case 0x4230:
|
||||
grouped_data[DataType::EEG].push_back(parse_eeg(ptr));
|
||||
break;
|
||||
case 0x4211:
|
||||
grouped_data[DataType::ECG_2LEAD].push_back(parse_ecg_emg(ptr));
|
||||
break;
|
||||
case 0x4212:
|
||||
grouped_data[DataType::SNORE].push_back(parse_snore(ptr));
|
||||
break;
|
||||
case 0x4213:
|
||||
grouped_data[DataType::RESPIRATION].push_back(parse_respiration(ptr));
|
||||
break;
|
||||
case 0x4402:
|
||||
grouped_data[DataType::ECG_12LEAD].push_back(parse_12lead_ecg(ptr));
|
||||
break;
|
||||
case 0x4302:
|
||||
grouped_data[DataType::PPG].push_back(parse_ppg(ptr));
|
||||
break;
|
||||
case 0x1102:
|
||||
grouped_data[DataType::STETHOSCOPE].push_back(parse_stethoscope(ptr));
|
||||
break;
|
||||
default:
|
||||
// 如果不是已知类型,跳过这个包继续处理
|
||||
ptr += PACKET_SIZE;
|
||||
continue;
|
||||
}
|
||||
ptr += PACKET_SIZE;
|
||||
} else {
|
||||
// 剩余数据不足一个包,但大于0,发出警告
|
||||
break; // 结束处理
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 直接构建最终结果,避免中间存储
|
||||
std::vector<SensorData> final_results;
|
||||
|
||||
// 按数据类型分组处理,合并通道数据
|
||||
for (auto& [data_type, packets] : grouped_data) {
|
||||
if (packets.empty()) continue;
|
||||
|
||||
// 检查第一个数据包的通道数据类型
|
||||
bool isMultiChannel = std::holds_alternative<std::vector<std::vector<float>>>(packets[0].channel_data);
|
||||
bool isSingleChannel = std::holds_alternative<std::vector<float>>(packets[0].channel_data);
|
||||
|
||||
if (isMultiChannel) {
|
||||
// 处理多通道数据 (vector<vector<float>>)
|
||||
SensorData full_data = packets[0];
|
||||
full_data.channel_data = std::vector<std::vector<float>>();
|
||||
|
||||
// 获取通道数量
|
||||
size_t num_channels = 0;
|
||||
if (auto* channels = std::get_if<std::vector<std::vector<float>>>(&packets[0].channel_data)) {
|
||||
num_channels = channels->size();
|
||||
full_data.channel_data.emplace<std::vector<std::vector<float>>>(num_channels);
|
||||
}
|
||||
|
||||
// 合并所有数据包中的通道数据
|
||||
auto& full_channels = std::get<std::vector<std::vector<float>>>(full_data.channel_data);
|
||||
for (size_t ch = 0; ch < num_channels; ch++) {
|
||||
for (auto& packet : packets) {
|
||||
if (auto* channels = std::get_if<std::vector<std::vector<float>>>(&packet.channel_data)) {
|
||||
if (ch < channels->size()) {
|
||||
full_channels[ch].insert(full_channels[ch].end(),
|
||||
(*channels)[ch].begin(),
|
||||
(*channels)[ch].end());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final_results.push_back(full_data);
|
||||
|
||||
} else if (isSingleChannel) {
|
||||
// 处理单通道数据 (vector<float>) - 合并所有数据包的单通道数据
|
||||
SensorData full_data = packets[0];
|
||||
full_data.channel_data = std::vector<float>();
|
||||
|
||||
auto& full_channel = full_data.channel_data.emplace<std::vector<float>>();
|
||||
|
||||
// 合并所有数据包的单通道数据
|
||||
for (auto& packet : packets) {
|
||||
if (auto* channel = std::get_if<std::vector<float>>(&packet.channel_data)) {
|
||||
full_channel.insert(full_channel.end(), channel->begin(), channel->end());
|
||||
}
|
||||
}
|
||||
|
||||
final_results.push_back(full_data);
|
||||
|
||||
} else {
|
||||
// 没有通道数据,直接添加第一个数据包
|
||||
final_results.push_back(packets[0]);
|
||||
}
|
||||
}
|
||||
|
||||
return final_results;
|
||||
}
|
||||
|
||||
// 工具函数:将数值转换为十六进制字符串
|
||||
std::string to_hex_string(uint16_t value) {
|
||||
std::stringstream ss;
|
||||
ss << "0x" << std::hex << std::uppercase << std::setfill('0') << std::setw(4) << value;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
|
||||
// ============================================================================
|
||||
// 新的流式解析版本 - 用于蓝牙设备数据包
|
||||
// ============================================================================
|
||||
|
||||
// 流式解析器构造函数
|
||||
StreamParser::StreamParser() : packet_callback_(nullptr), buffer_size_(0), total_packets_(0) {
|
||||
// 预分配缓冲区空间,避免频繁重新分配
|
||||
buffer_.reserve(1024); // 预分配1KB空间
|
||||
}
|
||||
|
||||
// 流式解析器析构函数
|
||||
StreamParser::~StreamParser() {
|
||||
// 清理资源
|
||||
clear();
|
||||
}
|
||||
|
||||
// 添加新接收到的蓝牙数据
|
||||
void StreamParser::appendData(const uint8_t* data, size_t length) {
|
||||
if (data == nullptr || length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 将新数据添加到缓冲区
|
||||
buffer_.insert(buffer_.end(), data, data + length);
|
||||
buffer_size_ += length;
|
||||
|
||||
// 尝试解析缓冲区中的数据
|
||||
parseBuffer();
|
||||
}
|
||||
|
||||
// 检查是否有完整的数据包可用
|
||||
bool StreamParser::hasCompletePacket() const {
|
||||
return !complete_packets_.empty();
|
||||
}
|
||||
|
||||
// 获取下一个完整的数据包
|
||||
SensorData StreamParser::getNextPacket() {
|
||||
if (complete_packets_.empty()) {
|
||||
throw std::runtime_error("没有可用的完整数据包");
|
||||
}
|
||||
|
||||
SensorData packet = complete_packets_.front();
|
||||
complete_packets_.pop();
|
||||
return packet;
|
||||
}
|
||||
|
||||
// 获取所有可用数据包
|
||||
std::vector<SensorData> StreamParser::getAllPackets() {
|
||||
std::vector<SensorData> result;
|
||||
while (!complete_packets_.empty()) {
|
||||
result.push_back(complete_packets_.front());
|
||||
complete_packets_.pop();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// 设置数据包处理回调函数
|
||||
void StreamParser::setPacketCallback(std::function<void(const SensorData&)> callback) {
|
||||
packet_callback_ = callback;
|
||||
}
|
||||
|
||||
// 清空缓冲区和队列
|
||||
void StreamParser::clear() {
|
||||
buffer_.clear();
|
||||
buffer_size_ = 0;
|
||||
while (!complete_packets_.empty()) {
|
||||
complete_packets_.pop();
|
||||
}
|
||||
}
|
||||
|
||||
// 获取缓冲区状态信息
|
||||
size_t StreamParser::getBufferSize() const {
|
||||
return buffer_.size();
|
||||
}
|
||||
|
||||
size_t StreamParser::getTotalPackets() const {
|
||||
return total_packets_;
|
||||
}
|
||||
|
||||
// 内部解析方法 - 持续处理缓冲区中的数据
|
||||
void StreamParser::parseBuffer() {
|
||||
// 持续处理直到缓冲区中没有完整的数据包
|
||||
while (buffer_.size() >= PACKET_SIZE) {
|
||||
// 验证数据包的有效性
|
||||
if (!isValidPacket(buffer_.data())) {
|
||||
// 无效数据包,跳过第一个字节继续寻找
|
||||
buffer_.erase(buffer_.begin());
|
||||
continue;
|
||||
}
|
||||
|
||||
// 提取数据包类型
|
||||
uint16_t data_type = read_le<uint16_t>(buffer_.data() + 2);
|
||||
SensorData packet;
|
||||
|
||||
try {
|
||||
// 根据数据类型调用相应的解析函数
|
||||
switch (data_type) {
|
||||
case 0x4230:
|
||||
packet = parse_eeg(buffer_.data());
|
||||
break;
|
||||
case 0x4211:
|
||||
packet = parse_ecg_emg(buffer_.data());
|
||||
break;
|
||||
case 0x4212:
|
||||
packet = parse_snore(buffer_.data());
|
||||
break;
|
||||
case 0x4213:
|
||||
packet = parse_respiration(buffer_.data());
|
||||
break;
|
||||
case 0x4402:
|
||||
packet = parse_12lead_ecg(buffer_.data());
|
||||
break;
|
||||
case 0x4302:
|
||||
packet = parse_ppg(buffer_.data());
|
||||
break;
|
||||
case 0x1102:
|
||||
packet = parse_stethoscope(buffer_.data());
|
||||
break;
|
||||
default:
|
||||
// 未知类型,跳过这个包
|
||||
buffer_.erase(buffer_.begin(), buffer_.begin() + PACKET_SIZE);
|
||||
continue; // 继续处理下一个包
|
||||
}
|
||||
|
||||
// 成功解析,添加到完成包队列
|
||||
complete_packets_.push(packet);
|
||||
total_packets_++;
|
||||
|
||||
// 如果有回调函数,调用它
|
||||
if (packet_callback_) {
|
||||
packet_callback_(packet);
|
||||
}
|
||||
|
||||
// 从缓冲区移除已处理的数据包
|
||||
buffer_.erase(buffer_.begin(), buffer_.begin() + PACKET_SIZE);
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
// 解析错误,跳过这个包
|
||||
buffer_.erase(buffer_.begin(), buffer_.begin() + PACKET_SIZE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 验证数据包的有效性
|
||||
bool StreamParser::isValidPacket(const uint8_t* data) {
|
||||
if (data == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查数据包长度
|
||||
uint16_t data_len = read_le<uint16_t>(data + 4);
|
||||
if (data_len != PACKET_SIZE - 6) { // 6字节是头部长度
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查数据类型是否在有效范围内
|
||||
uint16_t data_type = read_le<uint16_t>(data + 2);
|
||||
switch (data_type) {
|
||||
case 0x4230: // EEG
|
||||
case 0x4211: // ECG_2LEAD
|
||||
case 0x4212: // SNORE
|
||||
case 0x4213: // RESPIRATION
|
||||
case 0x4402: // ECG_12LEAD
|
||||
case 0x4302: // PPG
|
||||
case 0x1102: // STETHOSCOPE
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 获取缓冲区中等待处理的数据量
|
||||
size_t StreamParser::getPendingDataSize() const {
|
||||
return buffer_.size();
|
||||
}
|
||||
|
||||
// 检查缓冲区是否为空
|
||||
bool StreamParser::isBufferEmpty() const {
|
||||
return buffer_.empty();
|
||||
}
|
||||
|
||||
// 获取队列中等待处理的数据包数量
|
||||
size_t StreamParser::getQueuedPacketCount() const {
|
||||
return complete_packets_.size();
|
||||
}
|
||||
|
||||
// JNI辅助函数:将Java的byte数组转换为C++的vector
|
||||
std::vector<uint8_t> convertJavaByteArrayToVector(JNIEnv* env, jbyteArray byteArray) {
|
||||
std::vector<uint8_t> result;
|
||||
|
||||
if (byteArray == nullptr) {
|
||||
return result;
|
||||
}
|
||||
|
||||
jsize length = env->GetArrayLength(byteArray);
|
||||
if (length <= 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
jbyte* elements = env->GetByteArrayElements(byteArray, nullptr);
|
||||
if (elements == nullptr) {
|
||||
return result;
|
||||
}
|
||||
|
||||
result.resize(length);
|
||||
for (jsize i = 0; i < length; ++i) {
|
||||
result[i] = static_cast<uint8_t>(elements[i]);
|
||||
}
|
||||
|
||||
env->ReleaseByteArrayElements(byteArray, elements, JNI_ABORT);
|
||||
return result;
|
||||
}
|
||||
|
||||
// JNI辅助函数:将C++的SensorData转换为Java对象
|
||||
jobject convertSensorDataToJava(JNIEnv* env, const SensorData& sensorData) {
|
||||
// 获取Java类的引用
|
||||
jclass sensorDataClass = env->FindClass("type/SensorData");
|
||||
if (sensorDataClass == nullptr) {
|
||||
__android_log_print(ANDROID_LOG_ERROR, "DataParser", "Cannot find SensorData class");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// 创建Java对象
|
||||
jobject javaSensorData = env->AllocObject(sensorDataClass);
|
||||
if (javaSensorData == nullptr) {
|
||||
__android_log_print(ANDROID_LOG_ERROR, "DataParser", "Cannot create SensorData object");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// 获取字段ID
|
||||
jfieldID dataTypeField = env->GetFieldID(sensorDataClass, "dataType", "Ltype/SensorData$DataType;");
|
||||
jfieldID packetSnField = env->GetFieldID(sensorDataClass, "packetSn", "I");
|
||||
jfieldID leadStatusField = env->GetFieldID(sensorDataClass, "leadStatus", "[B");
|
||||
jfieldID timestampField = env->GetFieldID(sensorDataClass, "timestamp", "J");
|
||||
jfieldID channelDataField = env->GetFieldID(sensorDataClass, "channelData", "Ljava/util/List;");
|
||||
jfieldID additionalDataField = env->GetFieldID(sensorDataClass, "additionalData", "Ltype/SensorData$AdditionalData;");
|
||||
jfieldID rawDataField = env->GetFieldID(sensorDataClass, "rawData", "[B");
|
||||
|
||||
if (dataTypeField == nullptr || packetSnField == nullptr || leadStatusField == nullptr ||
|
||||
timestampField == nullptr || channelDataField == nullptr || additionalDataField == nullptr || rawDataField == nullptr) {
|
||||
__android_log_print(ANDROID_LOG_ERROR, "DataParser", "Cannot find SensorData fields");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// 设置基本字段
|
||||
env->SetIntField(javaSensorData, packetSnField, static_cast<jint>(sensorData.packet_sn));
|
||||
env->SetLongField(javaSensorData, timestampField, static_cast<jlong>(sensorData.timestamp));
|
||||
|
||||
// 设置数据类型
|
||||
jclass dataTypeClass = env->FindClass("type/SensorData$DataType");
|
||||
if (dataTypeClass != nullptr) {
|
||||
// 根据C++的DataType枚举值找到对应的Java枚举值
|
||||
const char* enumName = nullptr;
|
||||
switch (sensorData.data_type) {
|
||||
case DataType::EEG: enumName = "EEG"; break;
|
||||
case DataType::ECG_2LEAD: enumName = "ECG_2LEAD"; break;
|
||||
case DataType::PPG: enumName = "PPG"; break;
|
||||
case DataType::ECG_12LEAD: enumName = "ECG_12LEAD"; break;
|
||||
case DataType::MIT_BIH: enumName = "MIT_BIH"; break;
|
||||
case DataType::STETHOSCOPE: enumName = "STETHOSCOPE"; break;
|
||||
case DataType::SNORE: enumName = "SNORE"; break;
|
||||
case DataType::RESPIRATION: enumName = "RESPIRATION"; break;
|
||||
default: enumName = "EEG"; break; // 默认值
|
||||
}
|
||||
|
||||
jfieldID enumField = env->GetStaticFieldID(dataTypeClass, enumName, "Ltype/SensorData$DataType;");
|
||||
if (enumField != nullptr) {
|
||||
jobject enumValue = env->GetStaticObjectField(dataTypeClass, enumField);
|
||||
if (enumValue != nullptr) {
|
||||
env->SetObjectField(javaSensorData, dataTypeField, enumValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 设置导联状态
|
||||
jbyteArray leadStatusArray = env->NewByteArray(2);
|
||||
env->SetByteArrayRegion(leadStatusArray, 0, 2, reinterpret_cast<const jbyte*>(sensorData.lead_status.status));
|
||||
env->SetObjectField(javaSensorData, leadStatusField, leadStatusArray);
|
||||
|
||||
// 设置原始数据
|
||||
jbyteArray rawDataArray = env->NewByteArray(static_cast<jsize>(sensorData.raw_data.size()));
|
||||
env->SetByteArrayRegion(rawDataArray, 0, static_cast<jsize>(sensorData.raw_data.size()),
|
||||
reinterpret_cast<const jbyte*>(sensorData.raw_data.data()));
|
||||
env->SetObjectField(javaSensorData, rawDataField, rawDataArray);
|
||||
|
||||
// 设置通道数据(处理两种类型:vector<vector<float>> 和 vector<float>)
|
||||
if (auto* multiChannels = std::get_if<std::vector<std::vector<float>>>(&sensorData.channel_data)) {
|
||||
// 处理多通道数据 (vector<vector<float>>)
|
||||
jclass arrayListClass = env->FindClass("java/util/ArrayList");
|
||||
jmethodID arrayListConstructor = env->GetMethodID(arrayListClass, "<init>", "()V");
|
||||
jmethodID addMethod = env->GetMethodID(arrayListClass, "add", "(Ljava/lang/Object;)Z");
|
||||
|
||||
// 创建外层列表 (List<List<Float>>)
|
||||
jobject outerList = env->NewObject(arrayListClass, arrayListConstructor);
|
||||
|
||||
for (const auto& channel : *multiChannels) {
|
||||
// 创建内层列表 (List<Float>)
|
||||
jobject innerList = env->NewObject(arrayListClass, arrayListConstructor);
|
||||
|
||||
// 将浮点数添加到内层列表
|
||||
jclass floatClass = env->FindClass("java/lang/Float");
|
||||
jmethodID floatConstructor = env->GetMethodID(floatClass, "<init>", "(F)V");
|
||||
|
||||
for (float value : channel) {
|
||||
jobject floatObj = env->NewObject(floatClass, floatConstructor, value);
|
||||
env->CallBooleanMethod(innerList, addMethod, floatObj);
|
||||
env->DeleteLocalRef(floatObj); // 释放本地引用
|
||||
}
|
||||
|
||||
// 将内层列表添加到外层列表
|
||||
env->CallBooleanMethod(outerList, addMethod, innerList);
|
||||
env->DeleteLocalRef(innerList); // 释放本地引用
|
||||
}
|
||||
|
||||
env->SetObjectField(javaSensorData, channelDataField, outerList);
|
||||
|
||||
} else if (auto* singleChannel = std::get_if<std::vector<float>>(&sensorData.channel_data)) {
|
||||
// 处理单通道数据 (vector<float>) - 将其包装为单通道的多通道格式
|
||||
jclass arrayListClass = env->FindClass("java/util/ArrayList");
|
||||
jmethodID arrayListConstructor = env->GetMethodID(arrayListClass, "<init>", "()V");
|
||||
jmethodID addMethod = env->GetMethodID(arrayListClass, "add", "(Ljava/lang/Object;)Z");
|
||||
|
||||
// 创建外层列表 (List<List<Float>>)
|
||||
jobject outerList = env->NewObject(arrayListClass, arrayListConstructor);
|
||||
|
||||
// 创建内层列表 (List<Float>)
|
||||
jobject innerList = env->NewObject(arrayListClass, arrayListConstructor);
|
||||
|
||||
// 将浮点数添加到内层列表
|
||||
jclass floatClass = env->FindClass("java/lang/Float");
|
||||
jmethodID floatConstructor = env->GetMethodID(floatClass, "<init>", "(F)V");
|
||||
|
||||
for (float value : *singleChannel) {
|
||||
jobject floatObj = env->NewObject(floatClass, floatConstructor, value);
|
||||
env->CallBooleanMethod(innerList, addMethod, floatObj);
|
||||
env->DeleteLocalRef(floatObj); // 释放本地引用
|
||||
}
|
||||
|
||||
// 将内层列表添加到外层列表
|
||||
env->CallBooleanMethod(outerList, addMethod, innerList);
|
||||
env->DeleteLocalRef(innerList); // 释放本地引用
|
||||
|
||||
env->SetObjectField(javaSensorData, channelDataField, outerList);
|
||||
|
||||
} else {
|
||||
// 没有通道数据,设置为空列表
|
||||
jclass arrayListClass = env->FindClass("java/util/ArrayList");
|
||||
jmethodID arrayListConstructor = env->GetMethodID(arrayListClass, "<init>", "()V");
|
||||
jobject emptyList = env->NewObject(arrayListClass, arrayListConstructor);
|
||||
env->SetObjectField(javaSensorData, channelDataField, emptyList);
|
||||
}
|
||||
|
||||
// 设置附加数据(简化处理)
|
||||
jclass additionalDataClass = env->FindClass("type/SensorData$AdditionalData");
|
||||
if (additionalDataClass != nullptr) {
|
||||
jobject additionalData = env->AllocObject(additionalDataClass);
|
||||
if (additionalData != nullptr) {
|
||||
// 设置基本字段
|
||||
jfieldID hrField = env->GetFieldID(additionalDataClass, "hr", "I");
|
||||
jfieldID spo2Field = env->GetFieldID(additionalDataClass, "spo2", "I");
|
||||
jfieldID tempField = env->GetFieldID(additionalDataClass, "temperature", "F");
|
||||
|
||||
if (hrField != nullptr) env->SetIntField(additionalData, hrField, sensorData.additional.hr);
|
||||
if (spo2Field != nullptr) env->SetIntField(additionalData, spo2Field, sensorData.additional.spo2);
|
||||
if (tempField != nullptr) env->SetFloatField(additionalData, tempField, sensorData.additional.temperature);
|
||||
|
||||
env->SetObjectField(javaSensorData, additionalDataField, additionalData);
|
||||
}
|
||||
}
|
||||
|
||||
return javaSensorData;
|
||||
}
|
||||
|
||||
// JNI辅助函数:将C++的vector<SensorData>转换为Java的List
|
||||
jobject convertSensorDataVectorToJavaList(JNIEnv* env, const std::vector<SensorData>& sensorDataVector) {
|
||||
// 创建ArrayList
|
||||
jclass arrayListClass = env->FindClass("java/util/ArrayList");
|
||||
if (arrayListClass == nullptr) {
|
||||
__android_log_print(ANDROID_LOG_ERROR, "DataParser", "Cannot find ArrayList class");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
jmethodID arrayListConstructor = env->GetMethodID(arrayListClass, "<init>", "()V");
|
||||
jmethodID addMethod = env->GetMethodID(arrayListClass, "add", "(Ljava/lang/Object;)Z");
|
||||
|
||||
if (arrayListConstructor == nullptr || addMethod == nullptr) {
|
||||
__android_log_print(ANDROID_LOG_ERROR, "DataParser", "Cannot find ArrayList methods");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
jobject resultList = env->NewObject(arrayListClass, arrayListConstructor);
|
||||
|
||||
// 添加每个SensorData对象
|
||||
for (const auto& sensorData : sensorDataVector) {
|
||||
jobject javaSensorData = convertSensorDataToJava(env, sensorData);
|
||||
if (javaSensorData != nullptr) {
|
||||
env->CallBooleanMethod(resultList, addMethod, javaSensorData);
|
||||
}
|
||||
}
|
||||
|
||||
return resultList;
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,335 @@
|
|||
package com.example.cmake_project_test
|
||||
|
||||
import type.SensorData
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import com.example.cmake_project_test.databinding.ActivityMainBinding
|
||||
import java.io.InputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.IOException
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var binding: ActivityMainBinding
|
||||
private var parserHandle: Long = 0L
|
||||
private val rawStream = ByteArrayOutputStream(4096)
|
||||
private val uiUpdatePending = AtomicBoolean(false)
|
||||
private val packetBuffer = mutableListOf<SensorData>()
|
||||
private var lastUpdateTime = 0L
|
||||
private val UPDATE_INTERVAL = 500L // 每500毫秒更新一次UI
|
||||
private var totalPacketsParsed = 0L // 总共解析的数据包数量
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
binding = ActivityMainBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
||||
// 初始化UI
|
||||
binding.sampleText.text = "正在初始化..."
|
||||
|
||||
// 设置按钮点击事件
|
||||
binding.btnReset.setOnClickListener {
|
||||
resetData()
|
||||
}
|
||||
|
||||
binding.btnReload.setOnClickListener {
|
||||
reloadData()
|
||||
}
|
||||
|
||||
// 在后台线程处理数据加载和解析
|
||||
Thread {
|
||||
try {
|
||||
// 从 assets 文件夹读取文件
|
||||
val fileData = readAssetFile("data1.dat")
|
||||
|
||||
if (fileData != null) {
|
||||
// 使用真实流式解析路径:按块喂数据 -> drain -> 展示
|
||||
ensureParser()
|
||||
rawStream.reset()
|
||||
val chunkSize = 64
|
||||
var offset = 0
|
||||
|
||||
while (offset < fileData.size) {
|
||||
val n = minOf(chunkSize, fileData.size - offset)
|
||||
val chunk = fileData.copyOfRange(offset, offset + n)
|
||||
onBleNotify(chunk) // 复用同一流式路径
|
||||
offset += n
|
||||
|
||||
// 添加小延迟,模拟真实蓝牙数据传输
|
||||
Thread.sleep(10)
|
||||
}
|
||||
|
||||
// 最后拉取所有剩余包
|
||||
val packets = streamParserDrainPackets(parserHandle)
|
||||
if (!packets.isNullOrEmpty()) {
|
||||
totalPacketsParsed += packets.size // 更新总计数
|
||||
packetBuffer.addAll(packets)
|
||||
scheduleUiUpdate()
|
||||
}
|
||||
} else {
|
||||
runOnUiThread {
|
||||
binding.sampleText.text = "读取文件失败"
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("MainActivity", "Error processing data", e)
|
||||
runOnUiThread {
|
||||
binding.sampleText.text = "错误: ${e.message}"
|
||||
}
|
||||
}
|
||||
}.start()
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
ensureParser()
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
if (parserHandle != 0L) {
|
||||
destroyStreamParser(parserHandle)
|
||||
parserHandle = 0L
|
||||
}
|
||||
}
|
||||
|
||||
private fun ensureParser() {
|
||||
if (parserHandle == 0L) {
|
||||
parserHandle = createStreamParser()
|
||||
}
|
||||
}
|
||||
|
||||
// 蓝牙通知回调时调用:将 chunk 追加到解析器并拉取新包
|
||||
private fun onBleNotify(chunk: ByteArray) {
|
||||
if (chunk.isEmpty()) return
|
||||
ensureParser()
|
||||
rawStream.write(chunk)
|
||||
streamParserAppend(parserHandle, chunk)
|
||||
|
||||
// 拉取解析出的数据包,但不立即更新UI
|
||||
val packets = streamParserDrainPackets(parserHandle)
|
||||
if (!packets.isNullOrEmpty()) {
|
||||
totalPacketsParsed += packets.size // 更新总计数
|
||||
packetBuffer.addAll(packets)
|
||||
scheduleUiUpdate()
|
||||
}
|
||||
}
|
||||
|
||||
// 计划UI更新,避免频繁刷新
|
||||
private fun scheduleUiUpdate() {
|
||||
val currentTime = System.currentTimeMillis()
|
||||
if (currentTime - lastUpdateTime >= UPDATE_INTERVAL && uiUpdatePending.compareAndSet(false, true)) {
|
||||
lastUpdateTime = currentTime
|
||||
runOnUiThread {
|
||||
updateUiWithBufferedPackets()
|
||||
uiUpdatePending.set(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 使用缓冲的数据包更新UI
|
||||
private fun updateUiWithBufferedPackets() {
|
||||
if (packetBuffer.isEmpty()) return
|
||||
|
||||
// 获取设备类型信息
|
||||
val deviceTypes = packetBuffer.mapNotNull { it.dataType }.distinct()
|
||||
val deviceInfo = deviceTypes.joinToString(", ") { getDeviceName(it) }
|
||||
|
||||
// 构建统计信息
|
||||
val stats = buildString {
|
||||
append("=== 设备数据概览 ===\n")
|
||||
append("当前缓冲区: ${packetBuffer.size} 个数据包\n")
|
||||
append("总共解析: ${totalPacketsParsed} 个数据包\n")
|
||||
append("设备类型: $deviceInfo\n")
|
||||
append("总数据量: ${rawStream.size()} 字节\n\n")
|
||||
}
|
||||
|
||||
// 只显示最新的一些数据包详情
|
||||
val recentPackets = packetBuffer.takeLast(5) // 只显示最后5个包
|
||||
val has12Lead = recentPackets.any { it.dataType == SensorData.DataType.ECG_12LEAD }
|
||||
val channelDetails = if (has12Lead) {
|
||||
buildChannelDetails(recentPackets, maxPackets = 3, maxChannels = 6, maxSamples = 10)
|
||||
} else {
|
||||
buildChannelDetails(recentPackets, maxPackets = 3, maxChannels = 4, maxSamples = 10)
|
||||
}
|
||||
|
||||
// 更新UI
|
||||
binding.sampleText.text = stats + channelDetails
|
||||
|
||||
// 智能清理缓冲区:如果数据包数量过多,保留更多用于统计
|
||||
if (packetBuffer.size > 50) {
|
||||
val keepCount = minOf(30, packetBuffer.size / 2) // 保留30个或一半,取较小值
|
||||
packetBuffer.subList(0, packetBuffer.size - keepCount).clear()
|
||||
Log.d("MainActivity", "缓冲区已清理,保留 $keepCount 个数据包")
|
||||
}
|
||||
|
||||
// 添加调试信息:显示实际的数据包数量变化
|
||||
Log.d("MainActivity", "当前缓冲区数据包数量: ${packetBuffer.size}, 总共解析: $totalPacketsParsed")
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据数据类型获取设备名称
|
||||
*/
|
||||
private fun getDeviceName(dataType: SensorData.DataType): String {
|
||||
return when (dataType) {
|
||||
SensorData.DataType.EEG -> "脑电设备"
|
||||
SensorData.DataType.ECG_2LEAD -> "胸腹设备"
|
||||
SensorData.DataType.PPG -> "血氧设备"
|
||||
SensorData.DataType.ECG_12LEAD -> "12导联心电"
|
||||
SensorData.DataType.STETHOSCOPE -> "数字听诊"
|
||||
SensorData.DataType.SNORE -> "鼾声设备"
|
||||
SensorData.DataType.RESPIRATION -> "呼吸/姿态"
|
||||
SensorData.DataType.MIT_BIH -> "MIT-BIH"
|
||||
else -> "未知设备"
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 assets 文件夹读取文件到字节数组
|
||||
*/
|
||||
private fun readAssetFile(fileName: String): ByteArray? {
|
||||
return try {
|
||||
assets.open(fileName).use { inputStream ->
|
||||
ByteArray(inputStream.available()).also {
|
||||
inputStream.read(it)
|
||||
}
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
Log.e("MainActivity", "Error reading asset file", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建通道数据详情字符串
|
||||
*/
|
||||
private fun buildChannelDetails(data: List<SensorData>, maxPackets: Int = 3, maxChannels: Int = 2, maxSamples: Int = 10): String {
|
||||
if (data.isEmpty()) {
|
||||
return "无通道数据"
|
||||
}
|
||||
|
||||
val details = mutableListOf<String>()
|
||||
|
||||
data.take(maxPackets).forEachIndexed { packetIndex, sensorData ->
|
||||
if (sensorData.channelData.isNullOrEmpty()) {
|
||||
details.add("数据包 ${packetIndex + 1}: 无通道数据")
|
||||
return@forEachIndexed
|
||||
}
|
||||
|
||||
details.add("数据包 ${packetIndex + 1} (${getDeviceName(sensorData.dataType ?: SensorData.DataType.EEG)}):")
|
||||
|
||||
sensorData.channelData.take(maxChannels).forEachIndexed { channelIndex, channel ->
|
||||
// 只显示前几个采样点
|
||||
val sampleCount = minOf(maxSamples, channel.size)
|
||||
val channelDataStr = channel.take(sampleCount).joinToString(", ") { "%.1f".format(it) }
|
||||
|
||||
details.add(" 通道 ${channelIndex + 1}: ${sampleCount}/${channel.size} 采样点")
|
||||
details.add(" 示例: $channelDataStr${if (channel.size > sampleCount) "..." else ""}")
|
||||
}
|
||||
|
||||
if (sensorData.channelData.size > maxChannels) {
|
||||
details.add(" ... 还有 ${sensorData.channelData.size - maxChannels} 个通道")
|
||||
}
|
||||
|
||||
// 添加分隔线
|
||||
details.add("")
|
||||
}
|
||||
|
||||
if (data.size > maxPackets) {
|
||||
details.add("... 还有 ${data.size - maxPackets} 个数据包")
|
||||
}
|
||||
|
||||
return details.joinToString("\n")
|
||||
}
|
||||
|
||||
/**
|
||||
* A native method that is implemented by the 'cmake_project_test' native library,
|
||||
* which is packaged with this application.
|
||||
*/
|
||||
external fun stringFromJNI(): String
|
||||
external fun addFromJNI(a: Int, b: Int): Int
|
||||
external fun parseDeviceDataFromJNI(fileData: ByteArray): List<SensorData>?
|
||||
external fun createStreamParser(): Long
|
||||
external fun destroyStreamParser(handle: Long)
|
||||
external fun streamParserAppend(handle: Long, chunk: ByteArray)
|
||||
external fun streamParserDrainPackets(handle: Long): List<SensorData>?
|
||||
external fun parseStreamFromBytes(data: ByteArray, chunkSize: Int): List<SensorData>?
|
||||
|
||||
/**
|
||||
* 重置所有数据
|
||||
*/
|
||||
private fun resetData() {
|
||||
packetBuffer.clear()
|
||||
rawStream.reset()
|
||||
totalPacketsParsed = 0L // 重置总计数
|
||||
binding.sampleText.text = "数据已重置"
|
||||
Log.d("MainActivity", "数据已重置,当前数据包数量: ${packetBuffer.size}, 总计数: $totalPacketsParsed")
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新加载数据
|
||||
*/
|
||||
private fun reloadData() {
|
||||
binding.sampleText.text = "正在重新加载数据..."
|
||||
|
||||
// 重置解析器
|
||||
if (parserHandle != 0L) {
|
||||
destroyStreamParser(parserHandle)
|
||||
parserHandle = 0L
|
||||
}
|
||||
|
||||
// 清空缓冲区和重置计数
|
||||
packetBuffer.clear()
|
||||
rawStream.reset()
|
||||
totalPacketsParsed = 0L
|
||||
|
||||
// 在后台线程重新加载数据
|
||||
Thread {
|
||||
try {
|
||||
val fileData = readAssetFile("data1.dat")
|
||||
|
||||
if (fileData != null) {
|
||||
ensureParser()
|
||||
val chunkSize = 64
|
||||
var offset = 0
|
||||
|
||||
while (offset < fileData.size) {
|
||||
val n = minOf(chunkSize, fileData.size - offset)
|
||||
val chunk = fileData.copyOfRange(offset, offset + n)
|
||||
onBleNotify(chunk)
|
||||
offset += n
|
||||
Thread.sleep(10)
|
||||
}
|
||||
|
||||
// 最后拉取所有剩余包
|
||||
val packets = streamParserDrainPackets(parserHandle)
|
||||
if (!packets.isNullOrEmpty()) {
|
||||
packetBuffer.addAll(packets)
|
||||
scheduleUiUpdate()
|
||||
}
|
||||
|
||||
Log.d("MainActivity", "数据重新加载完成,当前数据包数量: ${packetBuffer.size}")
|
||||
} else {
|
||||
runOnUiThread {
|
||||
binding.sampleText.text = "重新加载失败:文件读取失败"
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("MainActivity", "Error reloading data", e)
|
||||
runOnUiThread {
|
||||
binding.sampleText.text = "重新加载错误: ${e.message}"
|
||||
}
|
||||
}
|
||||
}.start()
|
||||
}
|
||||
|
||||
companion object {
|
||||
// Used to load the 'cmake_project_test' library on application startup.
|
||||
init {
|
||||
System.loadLibrary("cmake_project_test")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
package type;
|
||||
import java.util.List;
|
||||
|
||||
public class SensorData {
|
||||
public enum DataType {
|
||||
EEG, ECG_2LEAD, PPG, ECG_12LEAD, MIT_BIH, STETHOSCOPE, SNORE, RESPIRATION
|
||||
}
|
||||
|
||||
public DataType dataType;
|
||||
public int packetSn;
|
||||
public byte[] leadStatus = new byte[2];
|
||||
public long timestamp;
|
||||
public List<List<Float>> channelData;
|
||||
public AdditionalData additionalData;
|
||||
public byte[] rawData;
|
||||
|
||||
// 构造函数
|
||||
public SensorData() {
|
||||
this.additionalData = new AdditionalData();
|
||||
}
|
||||
|
||||
// Getter和Setter方法
|
||||
public DataType getDataType() { return dataType; }
|
||||
public void setDataType(DataType dataType) { this.dataType = dataType; }
|
||||
|
||||
public int getPacketSn() { return packetSn; }
|
||||
public void setPacketSn(int packetSn) { this.packetSn = packetSn; }
|
||||
|
||||
public byte[] getLeadStatus() { return leadStatus; }
|
||||
public void setLeadStatus(byte[] leadStatus) { this.leadStatus = leadStatus; }
|
||||
|
||||
public long getTimestamp() { return timestamp; }
|
||||
public void setTimestamp(long timestamp) { this.timestamp = timestamp; }
|
||||
|
||||
public List<List<Float>> getChannelData() { return channelData; }
|
||||
public void setChannelData(List<List<Float>> channelData) { this.channelData = channelData; }
|
||||
|
||||
public AdditionalData getAdditionalData() { return additionalData; }
|
||||
public void setAdditionalData(AdditionalData additionalData) { this.additionalData = additionalData; }
|
||||
|
||||
public byte[] getRawData() { return rawData; }
|
||||
public void setRawData(byte[] rawData) { this.rawData = rawData; }
|
||||
|
||||
public static class AdditionalData {
|
||||
public int hr;
|
||||
public int spo2;
|
||||
public float temperature;
|
||||
public int gpioState;
|
||||
public int movement;
|
||||
public int posture;
|
||||
public int ambientLight;
|
||||
|
||||
// 构造函数
|
||||
public AdditionalData() {}
|
||||
|
||||
// Getter和Setter方法
|
||||
public int getHr() { return hr; }
|
||||
public void setHr(int hr) { this.hr = hr; }
|
||||
|
||||
public int getSpo2() { return spo2; }
|
||||
public void setSpo2(int spo2) { this.spo2 = spo2; }
|
||||
|
||||
public float getTemperature() { return temperature; }
|
||||
public void setTemperature(float temperature) { this.temperature = temperature; }
|
||||
|
||||
public int getGpioState() { return gpioState; }
|
||||
public void setGpioState(int gpioState) { this.gpioState = gpioState; }
|
||||
|
||||
public int getMovement() { return movement; }
|
||||
public void setMovement(int movement) { this.movement = movement; }
|
||||
|
||||
public int getPosture() { return posture; }
|
||||
public void setPosture(int posture) { this.posture = posture; }
|
||||
|
||||
public int getAmbientLight() { return ambientLight; }
|
||||
public void setAmbientLight(int ambientLight) { this.ambientLight = ambientLight; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,170 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
<path
|
||||
android:fillColor="#3DDC84"
|
||||
android:pathData="M0,0h108v108h-108z" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M9,0L9,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,0L19,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M29,0L29,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M39,0L39,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M49,0L49,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M59,0L59,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M69,0L69,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M79,0L79,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M89,0L89,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M99,0L99,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,9L108,9"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,19L108,19"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,29L108,29"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,39L108,39"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,49L108,49"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,59L108,59"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,69L108,69"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,79L108,79"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,89L108,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,99L108,99"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,29L89,29"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,39L89,39"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,49L89,49"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,59L89,59"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,69L89,69"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,79L89,79"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M29,19L29,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M39,19L39,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M49,19L49,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M59,19L59,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M69,19L69,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M79,19L79,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
</vector>
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:aapt="http://schemas.android.com/aapt"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
<path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z">
|
||||
<aapt:attr name="android:fillColor">
|
||||
<gradient
|
||||
android:endX="85.84757"
|
||||
android:endY="92.4963"
|
||||
android:startX="42.9492"
|
||||
android:startY="49.59793"
|
||||
android:type="linear">
|
||||
<item
|
||||
android:color="#44000000"
|
||||
android:offset="0.0" />
|
||||
<item
|
||||
android:color="#00000000"
|
||||
android:offset="1.0" />
|
||||
</gradient>
|
||||
</aapt:attr>
|
||||
</path>
|
||||
<path
|
||||
android:fillColor="#FFFFFF"
|
||||
android:fillType="nonZero"
|
||||
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
|
||||
android:strokeWidth="1"
|
||||
android:strokeColor="#00000000" />
|
||||
</vector>
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:padding="16dp"
|
||||
android:gravity="center">
|
||||
|
||||
<Button
|
||||
android:id="@+id/btn_reset"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="重置数据"
|
||||
android:layout_marginEnd="8dp" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btn_reload"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="重新加载"
|
||||
android:layout_marginStart="8dp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<ScrollView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/sample_text"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="13sp"
|
||||
android:padding="16dp"
|
||||
android:textColor="#000000"
|
||||
android:lineSpacingExtra="2dp"
|
||||
android:textIsSelectable="true"
|
||||
android:text="" />
|
||||
|
||||
</ScrollView>
|
||||
|
||||
</LinearLayout>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@drawable/ic_launcher_background" />
|
||||
<foreground android:drawable="@drawable/ic_launcher_foreground" />
|
||||
<monochrome android:drawable="@drawable/ic_launcher_foreground" />
|
||||
</adaptive-icon>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@drawable/ic_launcher_background" />
|
||||
<foreground android:drawable="@drawable/ic_launcher_foreground" />
|
||||
<monochrome android:drawable="@drawable/ic_launcher_foreground" />
|
||||
</adaptive-icon>
|
||||
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 2.8 KiB |
|
After Width: | Height: | Size: 982 B |
|
After Width: | Height: | Size: 1.7 KiB |
|
After Width: | Height: | Size: 1.9 KiB |
|
After Width: | Height: | Size: 3.8 KiB |
|
After Width: | Height: | Size: 2.8 KiB |
|
After Width: | Height: | Size: 5.8 KiB |
|
After Width: | Height: | Size: 3.8 KiB |
|
After Width: | Height: | Size: 7.6 KiB |
|
|
@ -0,0 +1,16 @@
|
|||
<resources xmlns:tools="http://schemas.android.com/tools">
|
||||
<!-- Base application theme. -->
|
||||
<style name="Theme.Cmake_project_test" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
|
||||
<!-- Primary brand color. -->
|
||||
<item name="colorPrimary">@color/purple_200</item>
|
||||
<item name="colorPrimaryVariant">@color/purple_700</item>
|
||||
<item name="colorOnPrimary">@color/black</item>
|
||||
<!-- Secondary brand color. -->
|
||||
<item name="colorSecondary">@color/teal_200</item>
|
||||
<item name="colorSecondaryVariant">@color/teal_200</item>
|
||||
<item name="colorOnSecondary">@color/black</item>
|
||||
<!-- Status bar color. -->
|
||||
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
|
||||
<!-- Customize your theme here. -->
|
||||
</style>
|
||||
</resources>
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="purple_200">#FFBB86FC</color>
|
||||
<color name="purple_500">#FF6200EE</color>
|
||||
<color name="purple_700">#FF3700B3</color>
|
||||
<color name="teal_200">#FF03DAC5</color>
|
||||
<color name="teal_700">#FF018786</color>
|
||||
<color name="black">#FF000000</color>
|
||||
<color name="white">#FFFFFFFF</color>
|
||||
</resources>
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
<resources>
|
||||
<string name="app_name">Cmake_project_test</string>
|
||||
</resources>
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
<resources xmlns:tools="http://schemas.android.com/tools">
|
||||
<!-- Base application theme. -->
|
||||
<style name="Theme.Cmake_project_test" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
|
||||
<!-- Primary brand color. -->
|
||||
<item name="colorPrimary">@color/purple_500</item>
|
||||
<item name="colorPrimaryVariant">@color/purple_700</item>
|
||||
<item name="colorOnPrimary">@color/white</item>
|
||||
<!-- Secondary brand color. -->
|
||||
<item name="colorSecondary">@color/teal_200</item>
|
||||
<item name="colorSecondaryVariant">@color/teal_700</item>
|
||||
<item name="colorOnSecondary">@color/black</item>
|
||||
<!-- Status bar color. -->
|
||||
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
|
||||
<!-- Customize your theme here. -->
|
||||
</style>
|
||||
</resources>
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="utf-8"?><!--
|
||||
Sample backup rules file; uncomment and customize as necessary.
|
||||
See https://developer.android.com/guide/topics/data/autobackup
|
||||
for details.
|
||||
Note: This file is ignored for devices older than API 31
|
||||
See https://developer.android.com/about/versions/12/backup-restore
|
||||
-->
|
||||
<full-backup-content>
|
||||
<!--
|
||||
<include domain="sharedpref" path="."/>
|
||||
<exclude domain="sharedpref" path="device.xml"/>
|
||||
-->
|
||||
</full-backup-content>
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="utf-8"?><!--
|
||||
Sample data extraction rules file; uncomment and customize as necessary.
|
||||
See https://developer.android.com/about/versions/12/backup-restore#xml-changes
|
||||
for details.
|
||||
-->
|
||||
<data-extraction-rules>
|
||||
<cloud-backup>
|
||||
<!-- TODO: Use <include> and <exclude> to control what is backed up.
|
||||
<include .../>
|
||||
<exclude .../>
|
||||
-->
|
||||
</cloud-backup>
|
||||
<!--
|
||||
<device-transfer>
|
||||
<include .../>
|
||||
<exclude .../>
|
||||
</device-transfer>
|
||||
-->
|
||||
</data-extraction-rules>
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package com.example.cmake_project_test
|
||||
|
||||
import org.junit.Test
|
||||
|
||||
import org.junit.Assert.*
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* See [testing documentation](http://d.android.com/tools/testing).
|
||||
*/
|
||||
class ExampleUnitTest {
|
||||
@Test
|
||||
fun addition_isCorrect() {
|
||||
assertEquals(4, 2 + 2)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
plugins {
|
||||
alias(libs.plugins.android.application) apply false
|
||||
alias(libs.plugins.kotlin.android) apply false
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
# Project-wide Gradle settings.
|
||||
# IDE (e.g. Android Studio) users:
|
||||
# Gradle settings configured through the IDE *will override*
|
||||
# any settings specified in this file.
|
||||
# For more details on how to configure your build environment visit
|
||||
# http://www.gradle.org/docs/current/userguide/build_environment.html
|
||||
# Specifies the JVM arguments used for the daemon process.
|
||||
# The setting is particularly useful for tweaking memory settings.
|
||||
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
|
||||
# When configured, Gradle will run in incubating parallel mode.
|
||||
# This option should only be used with decoupled projects. For more details, visit
|
||||
# https://developer.android.com/r/tools/gradle-multi-project-decoupled-projects
|
||||
# org.gradle.parallel=true
|
||||
# AndroidX package structure to make it clearer which packages are bundled with the
|
||||
# Android operating system, and which are packaged with your app's APK
|
||||
# https://developer.android.com/topic/libraries/support-library/androidx-rn
|
||||
android.useAndroidX=true
|
||||
# Kotlin code style for this project: "official" or "obsolete":
|
||||
kotlin.code.style=official
|
||||
# Enables namespacing of each library's R class so that its R class includes only the
|
||||
# resources declared in the library itself and none from the library's dependencies,
|
||||
# thereby reducing the size of the R class for that library
|
||||
android.nonTransitiveRClass=true
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
[versions]
|
||||
agp = "8.12.1"
|
||||
kotlin = "2.0.21"
|
||||
coreKtx = "1.10.1"
|
||||
junit = "4.13.2"
|
||||
junitVersion = "1.1.5"
|
||||
espressoCore = "3.5.1"
|
||||
appcompat = "1.6.1"
|
||||
material = "1.10.0"
|
||||
constraintlayout = "2.1.4"
|
||||
|
||||
[libraries]
|
||||
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
|
||||
junit = { group = "junit", name = "junit", version.ref = "junit" }
|
||||
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
|
||||
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
|
||||
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
|
||||
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
|
||||
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
|
||||
|
||||
[plugins]
|
||||
android-application = { id = "com.android.application", version.ref = "agp" }
|
||||
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
|
||||
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#Tue Aug 26 16:11:26 CST 2025
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
|
|
@ -0,0 +1,185 @@
|
|||
#!/usr/bin/env sh
|
||||
|
||||
#
|
||||
# Copyright 2015 the original author or authors.
|
||||
#
|
||||
# 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
|
||||
#
|
||||
# https://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.
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
##
|
||||
## Gradle start up script for UN*X
|
||||
##
|
||||
##############################################################################
|
||||
|
||||
# Attempt to set APP_HOME
|
||||
# Resolve links: $0 may be a link
|
||||
PRG="$0"
|
||||
# Need this for relative symlinks.
|
||||
while [ -h "$PRG" ] ; do
|
||||
ls=`ls -ld "$PRG"`
|
||||
link=`expr "$ls" : '.*-> \(.*\)$'`
|
||||
if expr "$link" : '/.*' > /dev/null; then
|
||||
PRG="$link"
|
||||
else
|
||||
PRG=`dirname "$PRG"`"/$link"
|
||||
fi
|
||||
done
|
||||
SAVED="`pwd`"
|
||||
cd "`dirname \"$PRG\"`/" >/dev/null
|
||||
APP_HOME="`pwd -P`"
|
||||
cd "$SAVED" >/dev/null
|
||||
|
||||
APP_NAME="Gradle"
|
||||
APP_BASE_NAME=`basename "$0"`
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD="maximum"
|
||||
|
||||
warn () {
|
||||
echo "$*"
|
||||
}
|
||||
|
||||
die () {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
}
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
nonstop=false
|
||||
case "`uname`" in
|
||||
CYGWIN* )
|
||||
cygwin=true
|
||||
;;
|
||||
Darwin* )
|
||||
darwin=true
|
||||
;;
|
||||
MINGW* )
|
||||
msys=true
|
||||
;;
|
||||
NONSTOP* )
|
||||
nonstop=true
|
||||
;;
|
||||
esac
|
||||
|
||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||
|
||||
|
||||
# Determine the Java command to use to start the JVM.
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||
else
|
||||
JAVACMD="$JAVA_HOME/bin/java"
|
||||
fi
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD="java"
|
||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
|
||||
MAX_FD_LIMIT=`ulimit -H -n`
|
||||
if [ $? -eq 0 ] ; then
|
||||
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
||||
MAX_FD="$MAX_FD_LIMIT"
|
||||
fi
|
||||
ulimit -n $MAX_FD
|
||||
if [ $? -ne 0 ] ; then
|
||||
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
||||
fi
|
||||
else
|
||||
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
||||
fi
|
||||
fi
|
||||
|
||||
# For Darwin, add options to specify how the application appears in the dock
|
||||
if $darwin; then
|
||||
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
||||
fi
|
||||
|
||||
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
|
||||
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
||||
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
||||
|
||||
JAVACMD=`cygpath --unix "$JAVACMD"`
|
||||
|
||||
# We build the pattern for arguments to be converted via cygpath
|
||||
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
||||
SEP=""
|
||||
for dir in $ROOTDIRSRAW ; do
|
||||
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
||||
SEP="|"
|
||||
done
|
||||
OURCYGPATTERN="(^($ROOTDIRS))"
|
||||
# Add a user-defined pattern to the cygpath arguments
|
||||
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
||||
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
||||
fi
|
||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||
i=0
|
||||
for arg in "$@" ; do
|
||||
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
||||
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
||||
|
||||
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
||||
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
||||
else
|
||||
eval `echo args$i`="\"$arg\""
|
||||
fi
|
||||
i=`expr $i + 1`
|
||||
done
|
||||
case $i in
|
||||
0) set -- ;;
|
||||
1) set -- "$args0" ;;
|
||||
2) set -- "$args0" "$args1" ;;
|
||||
3) set -- "$args0" "$args1" "$args2" ;;
|
||||
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
||||
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
||||
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
||||
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
||||
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
||||
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# Escape application args
|
||||
save () {
|
||||
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
|
||||
echo " "
|
||||
}
|
||||
APP_ARGS=`save "$@"`
|
||||
|
||||
# Collect all arguments for the java command, following the shell quoting and substitution rules
|
||||
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
|
||||
|
||||
exec "$JAVACMD" "$@"
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
@rem
|
||||
@rem Copyright 2015 the original author or authors.
|
||||
@rem
|
||||
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@rem you may not use this file except in compliance with the License.
|
||||
@rem You may obtain a copy of the License at
|
||||
@rem
|
||||
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||
@rem
|
||||
@rem Unless required by applicable law or agreed to in writing, software
|
||||
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@rem See the License for the specific language governing permissions and
|
||||
@rem limitations under the License.
|
||||
@rem
|
||||
|
||||
@if "%DEBUG%" == "" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Gradle startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%" == "" set DIRNAME=.
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
||||
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if "%ERRORLEVEL%" == "0" goto execute
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto execute
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
|
||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||
|
||||
|
||||
@rem Execute Gradle
|
||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
||||
|
||||
:end
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||
|
||||
:fail
|
||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||
rem the _cmd.exe /c_ return code!
|
||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||
exit /b 1
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
||||
:omega
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
pluginManagement {
|
||||
repositories {
|
||||
google {
|
||||
content {
|
||||
includeGroupByRegex("com\\.android.*")
|
||||
includeGroupByRegex("com\\.google.*")
|
||||
includeGroupByRegex("androidx.*")
|
||||
}
|
||||
}
|
||||
mavenCentral()
|
||||
gradlePluginPortal()
|
||||
}
|
||||
}
|
||||
dependencyResolutionManagement {
|
||||
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
}
|
||||
}
|
||||
|
||||
rootProject.name = "Cmake_project_test"
|
||||
include(":app")
|
||||
|
||||