{"id":21659711,"url":"https://github.com/Mase9527/react-native-SerialPort","last_synced_at":"2025-07-17T22:32:48.249Z","repository":{"id":40635738,"uuid":"207199456","full_name":"Marcello168/react-native-SerialPort","owner":"Marcello168","description":"React native Android 串口通讯 DLC","archived":false,"fork":false,"pushed_at":"2020-07-02T00:33:23.000Z","size":1541,"stargazers_count":14,"open_issues_count":5,"forks_count":9,"subscribers_count":1,"default_branch":"master","last_synced_at":"2023-03-02T22:16:56.311Z","etag":null,"topics":["androidserialport","reactnative","serial-port"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Marcello168.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-09T01:32:39.000Z","updated_at":"2022-06-20T05:54:16.000Z","dependencies_parsed_at":"2022-09-14T10:01:10.447Z","dependency_job_id":null,"html_url":"https://github.com/Marcello168/react-native-SerialPort","commit_stats":null,"previous_names":[],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marcello168%2Freact-native-SerialPort","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marcello168%2Freact-native-SerialPort/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marcello168%2Freact-native-SerialPort/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marcello168%2Freact-native-SerialPort/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Marcello168","download_url":"https://codeload.github.com/Marcello168/react-native-SerialPort/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226305336,"owners_count":17603794,"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","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":["androidserialport","reactnative","serial-port"],"created_at":"2024-11-25T09:31:24.738Z","updated_at":"2025-07-17T22:32:42.921Z","avatar_url":"https://github.com/Marcello168.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Android和硬件通信使用的串口库 react-native-serial-port\n\n## 原生库 使用的是 志勇大神写的串口工具库 [项目地址]:[https://github.com/licheedev/Android-SerialPort-API]\n\n## 开始使用\n1. 在`package.json`里面的 `dependencies` 添加 `\"react-native-serial-port\": \"github:Marcello168/react-native-SerialPort\"`\n2. 添加后 \b执行 `$ npm install ` 就会下载\n3. 执行 如下命令 自动链接到原生库  `$ react-native link react-native-serial-port`\n4. 在Android 目录下 的`build.gradle`文件里面的 `repositories`\n   增加 `maven { url 'https://jitpack.io' }` 如下\n\n   ```\n    allprojects {\n    repositories {\n        mavenLocal()\n        google()\n        jcenter()\n        maven {\n            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm\n            url \"$rootDir/../node_modules/react-native/android\"\n            }\n      maven { url 'https://jitpack.io' }\n      }\n\t}\n   ```\n\n   5. 在AndraidMainifest.xml 文件中 将  ` android:allowBackup=\"false\"` 改成`android:allowBackup=\"true\"`\n\n\n\n### 手动安装\n\n#### Android\n\n1. Open up `android/app/src/main/java/[...]/MainActivity.java`\n  - Add `import com.reactlibrary.RNSerialPortPackage;` to the imports at the top of the file\n  - Add `new RNSerialPortPackage()` to the list returned by the `getPackages()` method\n2. Append the following lines to `android/settings.gradle`:\n  \t```\n  \tinclude ':react-native-serial-port'\n  \tproject(':react-native-serial-port').projectDir = new File(rootProject.projectDir, \t'../node_modules/react-native-serial-port/android')\n  \t```\n3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:\n  \t```\n      compile project(':react-native-serial-port')\n  \t```\n\n\n## 使用方法\n\n1. 获取设备的路径列表\n```javascript\nimport RNSerialPort from 'react-native-serial-port';\n\n// TODO: 获取设备的路径列表\n RNSerialPort.getAllDevicesPath((result) =\u003e {\n                console.log(result); \n            });\n```\n\n2. 打开串口\n```javascript\nimport RNSerialPort from 'react-native-serial-port';\n// TODO: 打开串口\nRNSerialPort.openSerialPort('/dev/ttySO', 9600);\n```\n3. 发送数据\n```javascript\nimport RNSerialPort from 'react-native-serial-port';\nlet byteData = [0x00,0x01,0x02,0x03,0x05]\n// TODO: 发送数据\nRNSerialPort.sendByteData(byteData);\n```\n\n3. 监听串口的状态 和 监听串口回传数据\n```javascript\nimport RNSerialPort from 'react-native-serial-port';\n     DeviceEventEmitter.addListener('onSerialPortRecevieData', this.onSerialPortRecevieData, this)\n    //监听接收串口开关的状态\n\tDeviceEventEmitter.addListener('onSerialPortOpenStatus'，this.onSerialPortOpenStatus, this)\n\n\n\t    //监听串口的状态\n    onSerialPortOpenStatus(status) {\n        alert(status)\n        console.log(\"onSerialPortOpenStatus\");\n        //处理逻辑\n\t}\n\t\n    // 监听串口回传数据\n    onSerialPortRecevieData(receiveData) {\n        console.log(receiveData);\n        console.log(\"onSerialPortRecevieData\");\n        // 处理接收的数据\n    }\n\n```\n### 具体使用可以参考 `example` 里面的 `RNSerialPortManager.js`文件","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMase9527%2Freact-native-SerialPort","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMase9527%2Freact-native-SerialPort","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMase9527%2Freact-native-SerialPort/lists"}