{"id":34636747,"url":"https://github.com/entertech/entertechexternaldevicecommunication","last_synced_at":"2026-05-28T19:31:15.326Z","repository":{"id":206385516,"uuid":"716395935","full_name":"Entertech/EntertechExternalDeviceCommunication","owner":"Entertech","description":null,"archived":false,"fork":false,"pushed_at":"2024-06-18T09:11:53.000Z","size":4436,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2024-06-18T10:08:46.155Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Entertech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-11-09T03:33:33.000Z","updated_at":"2024-06-18T09:12:02.000Z","dependencies_parsed_at":"2023-11-27T10:01:51.227Z","dependency_job_id":"bac47470-58fa-4c9f-a5cd-a67e58ee0950","html_url":"https://github.com/Entertech/EntertechExternalDeviceCommunication","commit_stats":null,"previous_names":["entertech/entertechexternaldevicecommunication"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/Entertech/EntertechExternalDeviceCommunication","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Entertech%2FEntertechExternalDeviceCommunication","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Entertech%2FEntertechExternalDeviceCommunication/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Entertech%2FEntertechExternalDeviceCommunication/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Entertech%2FEntertechExternalDeviceCommunication/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Entertech","download_url":"https://codeload.github.com/Entertech/EntertechExternalDeviceCommunication/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Entertech%2FEntertechExternalDeviceCommunication/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28005408,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-24T02:00:07.193Z","response_time":83,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-12-24T17:02:31.056Z","updated_at":"2025-12-24T17:03:54.624Z","avatar_url":"https://github.com/Entertech.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 详细API说明\n\n### 集成\n\n#### 本地依赖\n\n将Demo中app/libs目录下的device\\_communicate\\_serialport-0.0.4.aar文件\n\n#### gradle自动依赖\n\n在项目根目录的build.gradle文件下添加以下依赖：\n\n```groovy\nrepositories {\n    mavenCentral()\n}\n```\n\n在所需的module中的build.gradle文件下添加以下依赖：\n\n    implementation 'cn.entertech.android:device_communicate_serialport:0.0.4'\n\n### 外设-串口管理类\n\n**方法说明**\n\n该类集成了外设-串口的所有操作\n\n**示例代码**\n\n```kotlin\nval manage = BaseExternalDeviceCommunicationManage.getManage(ExternalDeviceType.SERIAL_PORT)\n```\n\n### 设备初始化\n\n    manage?.initDevice(context)\n\n### 设备连接\n\n**方法说明**\n\n连接串口设备\n\n**示例代码**\n\n```kotlin\n  manage?.connectDevice(this, {\n    Log.d(TAG, \"connectDevice success\")\n}) { errorCode, errorMsg -\u003e\n    Log.e(TAG, \"errorCode: $errorCode  errorMsg: $errorMsg\")\n}\n\n```\n\n**参数说明**\n\n| 参数             | 类型                       | 说明            |\n|----------------|--------------------------|---------------|\n| context        | Context                  | 上下文，启动服务，注册广播 |\n| connectSuccess | (() -\u003e Unit)?            | 连接成功回调        |\n| connectFail    | ((Int, String) -\u003e Unit)? | 连接失败：错误码，错误信息 |\n\n### 设备断开\n\n**方法说明**\n\n断开与设备的连接\n\n**示例代码**\n\n```kotlin\nmanage?.disConnectDevice()\n```\n\n### 获取设备连接状态\n\n**方法说明**\n\n获取当前设备连接状态\n\n**示例代码**\n\n```kotlin\nval isConnected = manage?.isConnected()\n```\n\n**返回值说明**\n\n| 参数          | 类型      | 说明                    |\n|-------------|---------|-----------------------|\n| isConnected | Boolean | 设备已连接为true，未连接为false。 |\n\n### 添加|移除 断开连接成功监听\n\n**方法说明**\n\n添加|移除 断开连接成功监听，断开连接成功回调\n\n**示例代码**\n\n```kotlin\nprivate val disconnectListener: (String) -\u003e Unit by lazy {\n    {\n//todo\n    }\n}\n\nmanage?.addDisConnectListener(disconnectListener)\n\n```\n\n监听接口生命周期需要管理，不需要监听了，请调用remove\n\n```kotlin\nmanage?.removeDisConnectListener(disconnectListener)\n```\n\n### 添加|移除 连接成功监听\n\n**方法说明**\n\n添加连接|移除成功监听，连接成功回调\n\n**示例代码**\n\n```kotlin\n private val connectListener by lazy {\n    {\n//todo\n    }\n}\n\nmanage?.addConnectListener(connectListener)\n\n```\n\n监听接口生命周期需要管理，不需要监听了，请调用remove\n\n```kotlin\nmanage?.removeConnectListener(connectListener)\n```\n\n### 添加原始数据监听\n\n**方法说明**\n\n添加原始数据监听，通过该监听可从硬件中获取原始数据\n\n**示例代码**\n\n      var rawDataListener = fun(data:ByteArray){\n            Logger.d(Arrays.toString(data))\n      }\n      manage?.addRawDataListener(rawDataListener)\n\n**原始数据说明**\n\n| 包头             | 包长度  | 心率数据         | 脱落检测数据             | 第一个数据（左通道） | 第二个数据（右通道） | 第三个数据（左通道） | 第四个数据（右通道） | ........ | 第9个数据    | 第10个数据   | 校验位（单字节对比校验） | 包尾             |\n|:---------------|:-----|:-------------|:-------------------|:-----------|:-----------|:-----------|:-----------|:---------|:---------|:---------|:-------------|:---------------|\n| 3字节            | 1字节  | 1字节          | 1字节                | 3个字节       | 3个字节       | 3个字节       | 3字节        | ........ | 3个字节     | 3个字节     | 1字节          | 3字节            |\n| 0xBB-0xBB-0xBB | 0x28 | 0x00(心率数据为0) | 0x00(0为佩戴正常，非0为脱落) | 00-01-02   | 03-04-05   | 06-07-08   | 09-0A-0B   | ........ | 00-01-02 | 00-01-02 | 0x77         | 0xEE-0xEE-0xEE |\n\n### 移除原始数据监听\n\n**方法说明**\n\n如果不想受到原始数据，移除监听即可\n\n**示例代码**\n\n```kotlin\nmanage?.removeRawDataListener(rawDataListener)\n```\n\n### 添加脑波数据监听\n\n**方法说明**\n\n添加原始脑波监听，通过该监听可从硬件中获取原始脑波数据\n\n**示例代码**\n\n```kotlin\n  var bioAndAffectDataListeners = fun(data: ByteArray) {\n    Logger.d(Arrays.toString(data))\n}\nmanage?.addBioAndAffectDataListener(bioAndAffectDataListeners)\n```\n\n**参数说明**\n\n| 参数                       | 类型                | 说明     |\n|--------------------------|-------------------|--------|\n| bioAndAffectDataListener | （ByteArray）-\u003eUnit | 原始脑波回调 |\n\n\u003e **脑波数据说明**\n\u003e\n\u003e 从脑波回调中返回的原始脑波数据是一个长度为20的字节数组，其中脑波数据分左右两个通道，\n\u003e 依次为：包序号、包序号、左通道、左通道、左通道、右通道、右通道、右通道、左通道、左通道、左通道、右通道、右通道、右通道、左通道、左通道、左通道、右通道、右通道、右通道。。。。\n\u003e\n\u003e **正常数据示例**\n\u003e\n\u003e \\[0, 94, 21, -36, 125, 21, -12, -75, 22, 8, 61, 22, 10, -72, 22, 15, -19,20,10,8]\n\u003e\n\u003e **异常数据示例（未检测到脑波数据）**\n\u003e\n\u003e \\[0, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,-1,-1,-1]\n\n### 移除原始脑波监听\n\n**方法说明**\n\n如果不想受到脑波数据，移除监听即可\n\n**示例代码**\n\n```kotlin\nmanage?.removeBioAndAffectDataListener(bioAndAffectDataListeners)\n```\n\n**参数说明**\n\n| 参数                        | 类型                | 说明     |\n|---------------------------|-------------------|--------|\n| bioAndAffectDataListeners | （ByteArray）-\u003eUnit | 脑波数据回调 |\n\n### 添加心率监听\n\n**方法说明**\n\n添加心率监听，通过该监听可从硬件中获取心率数据\n\n**示例代码**\n\n```kotlin\nvar heartRateListener = fun(heartRate: Int) {\n    Logger.d(\"heart rate data is \" + heartRate)\n}\nmanage?.addHeartRateListener(heartRateListener)\n```\n\n**参数说明**\n\n| 参数                | 类型          | 说明       |\n|-------------------|-------------|----------|\n| heartRateListener | （Int）-\u003eUnit | 心率数据获取回调 |\n\n### 移除心率监听\n\n**方法说明**\n\n如果不想收到心率，移除监听即可\n\n**示例代码**\n\n```kotlin\nmanage?.removeHeartRateListener(heartRateListener)\n```\n\n**参数说明**\n\n| 参数                | 类型          | 说明     |\n|-------------------|-------------|--------|\n| heartRateListener | （Int）-\u003eUnit | 心率数据回调 |\n\n### 添加佩戴信号监听\n\n**方法说明**\n\n添加该监听，可实时获取设备佩戴质量\n\n**代码示例**\n\n```kotlin\ncontactListener = fun(state: Int) {\n    Logger.d(\"Whether the wearing contact is good:\" + state == 0);\n}\nmanage?.addContactListener(contactListener)\n\n```\n\n**参数说明**\n\n| 参数              | 类型          | 说明                      |\n|-----------------|-------------|-------------------------|\n| contactListener | （Int）-\u003eUnit | 佩戴信号回调。0:接触良好，其他值：未正常佩戴 |\n\n### 移除佩戴信号监听\n\n**方法说明**\n\n移除该监听，则不会受到佩戴信号\n\n**代码示例**\n\n```kotlin\nmanage?.removeContactListener(contactListener)\n```\n\n**参数说明**\n\n| 参数              | 类型          | 说明     |\n|-----------------|-------------|--------|\n| contactListener | （Int）-\u003eUnit | 佩戴信号回调 |\n\n**参数说明**\n\n| 参数                     | 类型              | 说明     |\n|------------------------|-----------------|--------|\n| batteryVoltageListener | （Double）-\u003e Unit | 电池电压回调 |\n\n### 开始脑波和心率数据同时采集\n\n**方法说明**\n\n开始心率数据采集，调用这个接口开始同时采集脑波和心率数据\n\n**示例代码**\n\n```kotlin\nmanage?.startHeartAndBrainCollection()\n```\n\n### 停止脑波和心率数据采集\n\n**方法说明**\n\n停止采集，调用该方法停止采集脑波和心率数据\n\n**示例代码**\n\n```kotlin\nmanage?.stopHeartAndBrainCollection()\n```\n\n### 流程图\n\n```mermaid\ngraph LR\n\n断开连接--\u003e移除监听\n获取外部设备通讯服务-.-\u003e设置监听\n初始化设备-.-\u003e设置监听\n连接设备-.-\u003e设置监听\n获取外部设备通讯服务--\u003e\n初始化设备--\u003e\n连接设备--\u003e\n下发接收指令--\u003e\n下发停止接收指令--\u003e\n断开连接\n\n\n```\n\n### 辅助功能\n\n##### **调式日志**\n\n如果调试阶段需要打印日志调用如下方法：\n\n```kotlin\nExternalDeviceCommunicateLog.printer = object : ILogPrinter {\n    override fun d(tag: String, msg: String) {\n    }\n\n    override fun i(tag: String, msg: String) {\n    }\n\n    override fun e(tag: String, msg: String) {\n    }\n}\n```\n\n~~内部默认使用DefaultLogPrinter~~，业务可以使用默认，sdk内部不使用\n\n    object DefaultLogPrinter:ILogPrinter {\n        override fun d(tag: String, msg: String) {\n            Log.d(tag, msg)\n        }\n\n        override fun i(tag: String, msg: String) {\n            Log.i(tag, msg)\n        }\n\n        override fun e(tag: String, msg: String) {\n            Log.e(tag, msg)\n        }\n    }\n\n##### **数据校验接口IProcessDataHelper**\n\n```kotlin\ninterface IProcessDataHelper {\n\n    /**\n     * @param byteInt 读取出来的字节\n     * @param contactListeners 佩戴监听 is [BaseExternalDeviceCommunicationManage.contactListeners]\n     * @param bioAndAffectDataListeners 生物基础数据\u0026情感数据监听 is [BaseExternalDeviceCommunicationManage.bioAndAffectDataListeners]\n     * @param heartRateListeners 心率监听 is [BaseExternalDeviceCommunicationManage.heartRateListeners]\n     * @param finish 拿到完整的数据包结构时候的回调\n     * */\n    fun process(\n        byteInt: Byte,\n        contactListeners: List\u003c((Int) -\u003e Unit)?\u003e,\n        bioAndAffectDataListeners: List\u003c((ByteArray) -\u003e Unit)?\u003e,\n        heartRateListeners: List\u003c((Int) -\u003e Unit)\u003e?,\n        finish: (() -\u003e Unit)? = null\n    )\n}\n\n```\n\n默认为ProcessDataTools\n该类的作用：从串口读出的数据，整合成一个以40个字节的数据包（rawListener[40字节]\n），从这个40字节数据包中获取心率（heartRateListeners[1字节]\n），佩戴状态（contactListeners1字节），脑波数据（bioAndAffectDataListeners[30字节]）\n\n若需要自定义校验规则 获取到BaseExternalDeviceCommunicationManage时就应该设置\n\n```kotlin\nBaseExternalDeviceCommunicationManage.mIProcessDataHelper = YouProcessDataHelper()\n\n```\n\n##### **数据适配器接口IDataAdapter**\n\n仅适用于ProcessDataTools中\n\n```kotlin\n\n\ninterface IDataAdapter\u003cT\u003e {\n\n    fun dataAdapter(originData: T, newDataCallback: (T) -\u003e Unit)\n}\n\n\n```\n\n**参数说明**\n\n| 参数 | 类型 | 说明 |\n| --------------- | -- | ------------------------------------ |\n| originData | T | 源数据，即 从ProcessDataTools中 获取到的一个完整数据包 |\n| newDataCallback | T | 新数据，经过处理后的新的完整数据包 |\n\n该接口用途：从串口获取到的源数据转化为所使用的算法所支持数据包，\n若需要自定义数据适配 则可以这么设置\n\n```kotlin\nval helper = BaseExternalDeviceCommunicationManage.mIProcessDataHelper\n\nif (helper is ProcessDataTools) {\n    helper.mIDataAdapter = MyDataAdapter\n}\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fentertech%2Fentertechexternaldevicecommunication","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fentertech%2Fentertechexternaldevicecommunication","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fentertech%2Fentertechexternaldevicecommunication/lists"}