├── .gitignore ├── .idea ├── .gitignore ├── codeStyles │ ├── Project.xml │ └── codeStyleConfig.xml ├── compiler.xml ├── gradle.xml ├── inspectionProfiles │ └── Project_Default.xml ├── jarRepositories.xml ├── misc.xml └── vcs.xml ├── LICENSE ├── README.md ├── README_zh-CN.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro ├── release │ ├── app-release.apk │ └── output.json └── src │ ├── androidTest │ └── java │ │ └── world │ │ └── shanya │ │ └── serialportsample │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── ic_launcher-playstore.png │ ├── ic_launcher_logo-playstore.png │ ├── java │ │ └── world │ │ │ └── shanya │ │ │ └── serialportsample │ │ │ ├── AboutActivity.kt │ │ │ ├── DebugActivity.kt │ │ │ ├── MainActivity.kt │ │ │ ├── MyViewModel.kt │ │ │ ├── SettingActivity.kt │ │ │ ├── pagers │ │ │ ├── KeyFragment.kt │ │ │ └── MessageFragment.kt │ │ │ └── utils │ │ │ ├── CheckUpdate.kt │ │ │ ├── KeySpTools.kt │ │ │ └── SettingSPTools.kt │ └── res │ │ ├── drawable │ │ ├── ic_baseline_send_24.xml │ │ ├── ic_baseline_settings_24.xml │ │ ├── ic_launcher_foreground.xml │ │ └── ic_launcher_logo_foreground.xml │ │ ├── layout │ │ ├── activity_about.xml │ │ ├── activity_debug.xml │ │ ├── activity_main.xml │ │ ├── activity_setting.xml │ │ ├── fragment_key.xml │ │ ├── fragment_message.xml │ │ └── key_button_info_dialog.xml │ │ ├── menu │ │ └── menu.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ ├── ic_launcher_logo.xml │ │ ├── ic_launcher_logo_round.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher_logo.png │ │ └── ic_launcher_logo_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher_logo.png │ │ └── ic_launcher_logo_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher_logo.png │ │ └── ic_launcher_logo_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher_logo.png │ │ └── ic_launcher_logo_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher_logo.png │ │ └── ic_launcher_logo_round.png │ │ ├── values-night │ │ └── themes.xml │ │ ├── values-zh-rCN │ │ └── strings.xml │ │ └── values │ │ ├── colors.xml │ │ ├── ic_launcher_background.xml │ │ ├── ic_launcher_logo_background.xml │ │ ├── strings.xml │ │ └── themes.xml │ └── test │ └── java │ └── world │ └── shanya │ └── serialportsample │ └── ExampleUnitTest.kt ├── build.gradle ├── docs ├── en │ ├── Makefile │ ├── _static │ │ ├── css │ │ │ └── readthedocs.css │ │ └── image │ │ │ ├── baidu.png │ │ │ ├── google.png │ │ │ ├── logo.png │ │ │ ├── logo192.png │ │ │ ├── qq.png │ │ │ ├── qq_wx.png │ │ │ └── wx.png │ ├── changelog.md │ ├── conf.py │ ├── index.rst │ ├── make.bat │ ├── questions.md │ ├── sponsor.md │ ├── switch_language.md │ └── tutorials │ │ ├── configuration_java.md │ │ ├── configuration_kotlin.md │ │ ├── discovery_connect_java.md │ │ ├── discovery_connect_kotlin.md │ │ ├── getting_started_java.md │ │ ├── getting_started_kotlin.md │ │ ├── install.md │ │ ├── received_send_java.md │ │ ├── received_send_kotlin.md │ │ ├── server.md │ │ ├── toast_java.md │ │ ├── toast_kotlin.md │ │ ├── tools_java.md │ │ └── tools_kotlin.md ├── requirements.txt └── zh_CN │ ├── Makefile │ ├── _static │ ├── css │ │ └── readthedocs.css │ └── image │ │ ├── baidu.png │ │ ├── google.png │ │ ├── logo.png │ │ ├── logo192.png │ │ ├── qq.png │ │ ├── qq_wx.png │ │ └── wx.png │ ├── changelog.md │ ├── conf.py │ ├── index.rst │ ├── make.bat │ ├── questions.md │ ├── sponsor.md │ ├── switch_language.md │ └── tutorials │ ├── configuration_java.md │ ├── configuration_kotlin.md │ ├── discovery_connect_java.md │ ├── discovery_connect_kotlin.md │ ├── getting_started_java.md │ ├── getting_started_kotlin.md │ ├── install.md │ ├── received_send_java.md │ ├── received_send_kotlin.md │ ├── server.md │ ├── toast_java.md │ ├── toast_kotlin.md │ ├── tools_java.md │ └── tools_kotlin.md ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── javademo ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── cn │ │ └── shanyaliux │ │ └── javademo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── cn │ │ │ └── shanyaliux │ │ │ └── javademo │ │ │ └── MainActivity.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── values-night │ │ └── themes.xml │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── themes.xml │ └── test │ └── java │ └── cn │ └── shanyaliux │ └── javademo │ └── ExampleUnitTest.java ├── kotlindemo ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── cn │ │ └── shanyaliux │ │ └── kotlindemo │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── cn │ │ │ └── shanyaliux │ │ │ └── kotlindemo │ │ │ └── MainActivity.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── values-night │ │ └── themes.xml │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── themes.xml │ └── test │ └── java │ └── cn │ └── shanyaliux │ └── kotlindemo │ └── ExampleUnitTest.kt ├── serialport ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── world │ │ └── shanya │ │ └── serialport │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── device_logo-playstore.png │ ├── device_logo_b-playstore.png │ ├── device_logo_ble-playstore.png │ ├── device_logo_l-playstore.png │ ├── device_logo_l_b-playstore.png │ ├── java │ │ └── world │ │ │ └── shanya │ │ │ └── serialport │ │ │ ├── SerialPort.kt │ │ │ ├── SerialPortBuilder.kt │ │ │ ├── SerialPortConfig.java │ │ │ ├── SerialPortServerBuilder.kt │ │ │ ├── connect │ │ │ ├── BluetoothStatusBroadcastReceiver.kt │ │ │ └── SerialPortConnect.kt │ │ │ ├── discovery │ │ │ ├── Device.kt │ │ │ ├── DiscoveryActivity.kt │ │ │ ├── DiscoveryBroadcastReceiver.kt │ │ │ └── SerialPortDiscovery.kt │ │ │ ├── server │ │ │ ├── SerialPortServer.kt │ │ │ ├── SerialPortServerService.kt │ │ │ └── SerialPortServerThread.kt │ │ │ ├── service │ │ │ └── SerialPortService.kt │ │ │ ├── strings │ │ │ ├── SerialPortToast.kt │ │ │ └── SerialPortToastBean.kt │ │ │ └── tools │ │ │ ├── SerialPortTools.java │ │ │ └── SerialPortToolsByKotlin.kt │ └── res │ │ ├── drawable-anydpi │ │ ├── ic_action_refresh.xml │ │ └── ic_action_search.xml │ │ ├── drawable-hdpi │ │ ├── ic_action_refresh.png │ │ └── ic_action_search.png │ │ ├── drawable-mdpi │ │ ├── ic_action_refresh.png │ │ └── ic_action_search.png │ │ ├── drawable-xhdpi │ │ ├── ic_action_refresh.png │ │ └── ic_action_search.png │ │ ├── drawable-xxhdpi │ │ ├── ic_action_refresh.png │ │ └── ic_action_search.png │ │ ├── drawable │ │ ├── device_logo_b_foreground.xml │ │ ├── device_logo_ble_foreground.xml │ │ ├── device_logo_foreground.xml │ │ ├── device_logo_l_b_foreground.xml │ │ └── device_logo_l_foreground.xml │ │ ├── layout │ │ ├── activity_discovery.xml │ │ ├── device_cell.xml │ │ └── progress_dialog_layout.xml │ │ ├── menu │ │ └── discovery_menu.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── device_logo.xml │ │ ├── device_logo_b.xml │ │ ├── device_logo_b_round.xml │ │ ├── device_logo_ble.xml │ │ ├── device_logo_ble_round.xml │ │ ├── device_logo_l.xml │ │ ├── device_logo_l_b.xml │ │ ├── device_logo_l_b_round.xml │ │ ├── device_logo_l_round.xml │ │ └── device_logo_round.xml │ │ ├── mipmap-hdpi │ │ ├── device_logo.png │ │ ├── device_logo_b.png │ │ ├── device_logo_b_round.png │ │ ├── device_logo_ble.png │ │ ├── device_logo_ble_round.png │ │ ├── device_logo_l.png │ │ ├── device_logo_l_b.png │ │ ├── device_logo_l_b_round.png │ │ ├── device_logo_l_round.png │ │ └── device_logo_round.png │ │ ├── mipmap-mdpi │ │ ├── device_logo.png │ │ ├── device_logo_b.png │ │ ├── device_logo_b_round.png │ │ ├── device_logo_ble.png │ │ ├── device_logo_ble_round.png │ │ ├── device_logo_l.png │ │ ├── device_logo_l_b.png │ │ ├── device_logo_l_b_round.png │ │ ├── device_logo_l_round.png │ │ └── device_logo_round.png │ │ ├── mipmap-xhdpi │ │ ├── device_logo.png │ │ ├── device_logo_b.png │ │ ├── device_logo_b_round.png │ │ ├── device_logo_ble.png │ │ ├── device_logo_ble_round.png │ │ ├── device_logo_l.png │ │ ├── device_logo_l_b.png │ │ ├── device_logo_l_b_round.png │ │ ├── device_logo_l_round.png │ │ └── device_logo_round.png │ │ ├── mipmap-xxhdpi │ │ ├── device_logo.png │ │ ├── device_logo_b.png │ │ ├── device_logo_b_round.png │ │ ├── device_logo_ble.png │ │ ├── device_logo_ble_round.png │ │ ├── device_logo_l.png │ │ ├── device_logo_l_b.png │ │ ├── device_logo_l_b_round.png │ │ ├── device_logo_l_round.png │ │ └── device_logo_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── device_logo.png │ │ ├── device_logo_b.png │ │ ├── device_logo_b_round.png │ │ ├── device_logo_ble.png │ │ ├── device_logo_ble_round.png │ │ ├── device_logo_l.png │ │ ├── device_logo_l_b.png │ │ ├── device_logo_l_b_round.png │ │ ├── device_logo_l_round.png │ │ └── device_logo_round.png │ │ ├── values-zh-rCN │ │ └── strings.xml │ │ └── values │ │ ├── arrays.xml │ │ ├── device_logo_b_background.xml │ │ ├── device_logo_background.xml │ │ ├── device_logo_ble_background.xml │ │ ├── device_logo_l_b_background.xml │ │ ├── device_logo_l_background.xml │ │ └── strings.xml │ └── test │ └── java │ └── world │ └── shanya │ └── serialport │ └── ExampleUnitTest.kt └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | local.properties 16 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 22 | 23 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 36 | -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | 29 | 30 | 34 | 35 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 |

3 |

4 | 5 | SerialPort 6 | 7 |

8 |

9 |

10 | 11 | An android bluetooth kit library 12 | 13 |

14 |

15 | 16 | [![docs](https://img.shields.io/badge/docs-latest-blue)](https://serialportsample.readthedocs.io/en/latest/) 17 | [![license](https://img.shields.io/github/license/open-mmlab/mmdetection.svg)](https://github.com/Shanyaliux/SerialPortSample/blob/master/LICENSE) 18 | [![open issues](https://isitmaintained.com/badge/open/open-mmlab/mmdetection.svg)](https://github.com/Shanyaliux/SerialPortSample/issues) 19 |
20 |

21 | 22 | ## Introduction 23 | English | [简体中文](README_zh-CN.md) 24 | 25 | SerialPort is an open source android bluetooth lightweight package library that easily solves the complexity of building your own bluetooth application, allowing people to focus on their own designs without considering the underlying configuration of android bluetooth. 26 | 27 | - Chain call 28 | - Legacy and BLE 29 | - Built-in must-have search interface 30 | - Search and connection status monitoring 31 | - Automatic reconnection mechanism 32 | - Toast prompt content modification 33 | 34 | ## License 35 | This project is released under the [Apache 2.0 license](LICENSE). 36 | 37 | ## Changelog 38 | **4.2.0** was released in 7/6/2022: 39 | - [Fix] Some compilation warnings about bluetooth permissions 40 | - [Modify] Upgrade `Kotlin` and `Gradle` versions 41 | - [Modify] Mark `ConnectionResultCallback` obsolete 42 | - [Feature] Added Ble device to send byte array 43 | - [Feature] Added Ble device can work callback 44 | - [Feature] Added Server 45 | 46 | For more changelogs see: [Changelog](docs/en/changelog.md) 47 | 48 | ## Installation 49 | Please refer to [install.md](docs/en/tutorials/install.md) for installation. 50 | 51 | ## Getting Started 52 | Please see [Basic usage Kotlin.md](docs/en/tutorials/getting_started_kotlin.md) or [Basic usage Java.md](docs/en/tutorials/getting_started_java.md) for the basic usage of SerialPort. 53 | 54 | We provide complete documentation. [documentation](https://serialport.readthedocs.io/en/latest/) 55 | 56 | ## Demo sample source code 57 | The following is the simplest Demo source code of Java and Kotlin based on SerialPort to implement Bluetooth App 58 | [Java](javademo) 59 | [Kotlin](kotlindemo) 60 | -------------------------------------------------------------------------------- /README_zh-CN.md: -------------------------------------------------------------------------------- 1 |
2 |

3 |

4 | 5 | SerialPort 6 | 7 |

8 |

9 |

10 | 11 | 一个安卓蓝牙工具库 12 | 13 |

14 |

15 | 16 | [![docs](https://img.shields.io/badge/docs-latest-blue)](https://serialportsample.readthedocs.io/zh_CN/latest/) 17 | [![license](https://img.shields.io/github/license/open-mmlab/mmdetection.svg)](https://github.com/Shanyaliux/SerialPortSample/blob/master/LICENSE) 18 | [![open issues](https://isitmaintained.com/badge/open/open-mmlab/mmdetection.svg)](https://github.com/Shanyaliux/SerialPortSample/issues) 19 |
20 |

21 | 22 | ## 简介 23 | [English](README.md) | 简体中文 24 | 25 | SerialPort 是一个开源的对 Android 蓝牙串口通信的轻量封装库,轻松解决了构建自己的串口调试APP的复杂程度,让人可以专注追求自己设计,不用考虑蓝牙串口底层的配置。 26 | 27 | - 链式调用,一次到位 28 | - 传统与BLE兼顾 29 | - 内置必备搜索界面 30 | - 搜索、连接状态监听 31 | - 自动重连机制 32 | - Toast 提示内容修改 33 | 34 | ## 开源许可证 35 | 该项目采用 [Apache 2.0 开源许可证](LICENSE)。 36 | 37 | ## 更新日志 38 | 最新的 **4.2.0** 版本已经在 7/6/2022 发布: 39 | - 修复一些关于蓝牙权限的编译告警 40 | - 升级`Kotlin`和`Gradle`版本 41 | - 标记`ConnectionResultCallback`过时 42 | - 新增Ble设备发送字节数组 43 | - 新增Ble设备可以工作回调 44 | - 新增服务端配置 45 | 46 | 更多更新日志见: [更新日志](docs/zh_CN/changelog.md) 47 | 48 | ## 安装 49 | 请参考 [安装文档](docs/zh_CN/tutorials/install.md) 进行安装。 50 | 51 | ## 快速入门 52 | 请参考 [快速入门文档_Kotlin版本](docs/zh_CN/tutorials/getting_started_kotlin.md) 或 [快速入门文档_Java版本](docs/zh_CN/tutorials/getting_started_java.md) 学习 SerialPort 的基本使用。 53 | 54 | 我们提供完整的文档。 [文档地址](https://serialport.readthedocs.io/zh_CN/latest/) 55 | 56 | ## 最简Demo源码 57 | 以下是基于SerialPort实现蓝牙App的Java和Kotlin最简Demo源码 58 | [Java](javademo) 59 | [Kotlin](kotlindemo) 60 | 61 | ## 欢迎加入 SerialPort 交流群 62 |
63 | 64 |
65 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'kotlin-android' 4 | id 'kotlin-android-extensions' 5 | } 6 | 7 | android { 8 | compileSdkVersion 32 9 | buildToolsVersion "30.0.3" 10 | 11 | defaultConfig { 12 | applicationId "world.shanya.serialportsample" 13 | minSdkVersion 21 14 | targetSdkVersion 32 15 | versionCode 5 16 | versionName "1.1.1" 17 | 18 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 19 | } 20 | 21 | buildTypes { 22 | release { 23 | minifyEnabled false 24 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 25 | } 26 | } 27 | compileOptions { 28 | sourceCompatibility JavaVersion.VERSION_1_8 29 | targetCompatibility JavaVersion.VERSION_1_8 30 | } 31 | kotlinOptions { 32 | jvmTarget = '1.8' 33 | } 34 | } 35 | 36 | dependencies { 37 | 38 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 39 | implementation 'androidx.core:core-ktx:1.3.2' 40 | implementation 'androidx.appcompat:appcompat:1.2.0' 41 | implementation 'com.google.android.material:material:1.3.0' 42 | implementation 'androidx.constraintlayout:constraintlayout:2.0.4' 43 | implementation 'androidx.legacy:legacy-support-v4:1.0.0' 44 | testImplementation 'junit:junit:4.+' 45 | androidTestImplementation 'androidx.test.ext:junit:1.1.2' 46 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' 47 | implementation "androidx.viewpager2:viewpager2:1.0.0" 48 | //ViewModel 49 | implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0' 50 | implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.0' 51 | implementation "androidx.activity:activity-ktx:1.2.0" 52 | implementation "androidx.fragment:fragment-ktx:1.3.0" 53 | 54 | 55 | implementation project(':serialport') 56 | 57 | implementation 'io.github.medyo:android-about-page:2.0.0' //About Page 58 | implementation 'com.guolindev.permissionx:permissionx:1.6.1' //Get Permissions 59 | implementation 'com.ejlchina:okhttps-gson:3.4.2' //Check Update 60 | implementation 'com.github.azhon:AppUpdate:3.0.7' //Check Update 61 | 62 | implementation 'com.github.markushi:circlebutton:1.1' //CircleButton 63 | 64 | implementation 'com.afollestad.material-dialogs:bottomsheets:3.2.1' //bottomDialog 65 | 66 | 67 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/release/app-release.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/app/release/app-release.apk -------------------------------------------------------------------------------- /app/release/output.json: -------------------------------------------------------------------------------- 1 | [{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":5,"versionName":"1.1.1","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release","dirName":""},"path":"app-release.apk","properties":{}}] -------------------------------------------------------------------------------- /app/src/androidTest/java/world/shanya/serialportsample/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package world.shanya.serialportsample 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("world.shanya.serilportsample", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 17 | 18 | 21 | 24 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /app/src/main/ic_launcher-playstore.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/app/src/main/ic_launcher-playstore.png -------------------------------------------------------------------------------- /app/src/main/ic_launcher_logo-playstore.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/app/src/main/ic_launcher_logo-playstore.png -------------------------------------------------------------------------------- /app/src/main/java/world/shanya/serialportsample/AboutActivity.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * 项目名:SerialPortSample 3 | * 模块名:SerialPortSample.app 4 | * 类名:AboutActivity.kt 5 | * 作者:Shanya 6 | * 日期:2022/2/23 下午2:49 7 | * Copyright (c) 2021 https://shanyaliux.cn 8 | */ 9 | 10 | package world.shanya.serialportsample 11 | 12 | import androidx.appcompat.app.AppCompatActivity 13 | import android.os.Bundle 14 | import android.view.Gravity 15 | import android.view.View 16 | import mehdi.sakout.aboutpage.AboutPage 17 | import mehdi.sakout.aboutpage.Element 18 | import world.shanya.serialportsample.utils.CheckUpdate 19 | import java.lang.Exception 20 | 21 | 22 | class AboutActivity : AppCompatActivity() { 23 | 24 | private val checkUpdate = CheckUpdate(this) 25 | 26 | override fun onCreate(savedInstanceState: Bundle?) { 27 | super.onCreate(savedInstanceState) 28 | setContentView(R.layout.activity_about) 29 | 30 | val versionElement = Element() 31 | versionElement.gravity = Gravity.CENTER 32 | versionElement.title = "版本号: " + packageManager.getPackageInfo(packageName, 0).versionName 33 | versionElement.setOnClickListener { 34 | checkUpdate.check(true) 35 | } 36 | 37 | val authorElement = Element() 38 | authorElement.gravity = Gravity.CENTER 39 | authorElement.title = "作者: Shanya" 40 | 41 | val aboutPage: View = AboutPage(this) 42 | .isRTL(false) 43 | .setDescription("SerialPort封装库的全功能示例APP") 44 | // .setCustomFont(String) // or Typeface 45 | .setImage(R.mipmap.ic_launcher_logo) 46 | // .addItem(Element().setTitle(packageManager.getPackageInfo(packageName, 0).versionName)) 47 | // .addItem(adsElement) 48 | .addGroup("Connect with us") 49 | .addEmail("shanyaliux@qq.com") 50 | .addWebsite("https://shanyaliux.cn/serialport/") 51 | // .addFacebook("the.medy") 52 | // .addTwitter("medyo80") 53 | // .addYoutube("UCdPQtdWIsg7_pi4mrRu46vA") 54 | // .addPlayStore("com.ideashower.readitlater.pro") 55 | .addGitHub("Shanyaliux\\SerialPortSample") 56 | // .addInstagram("medyo80") 57 | .addItem(authorElement) 58 | .addItem(versionElement) 59 | .create() 60 | setContentView(aboutPage) 61 | } 62 | } -------------------------------------------------------------------------------- /app/src/main/java/world/shanya/serialportsample/DebugActivity.kt: -------------------------------------------------------------------------------- 1 | package world.shanya.serialportsample 2 | 3 | import android.annotation.SuppressLint 4 | import androidx.appcompat.app.AppCompatActivity 5 | import android.os.Bundle 6 | import kotlinx.android.synthetic.main.activity_debug.* 7 | import kotlinx.coroutines.MainScope 8 | import kotlinx.coroutines.launch 9 | import world.shanya.serialport.SerialPortBuilder 10 | import world.shanya.serialport.SerialPortServerBuilder 11 | 12 | class DebugActivity : AppCompatActivity() { 13 | @SuppressLint("MissingPermission", "SetTextI18n") 14 | override fun onCreate(savedInstanceState: Bundle?) { 15 | super.onCreate(savedInstanceState) 16 | setContentView(R.layout.activity_debug) 17 | 18 | val stringBuilder = StringBuilder() 19 | 20 | val serialPort = SerialPortBuilder 21 | .isDebug(true) 22 | .setReceivedDataCallback { 23 | MainScope().launch { 24 | stringBuilder.append(it) 25 | textViewReceiced.text = stringBuilder.toString() 26 | } 27 | } 28 | .setConnectionStatusCallback { status, bluetoothDevice -> 29 | MainScope().launch { 30 | if (status) { 31 | textViewConnectInfo.text = 32 | "设备名称:\t${bluetoothDevice?.name}\n" + 33 | "设备地址:\t${bluetoothDevice?.address}\n" + 34 | "设备类型:\t${bluetoothDevice?.type}" 35 | 36 | }else 37 | textViewConnectInfo.text = "" 38 | } 39 | } 40 | .build(this) 41 | 42 | val serialPortServer = SerialPortServerBuilder 43 | .setServerReceivedDataCallback { 44 | MainScope().launch { 45 | stringBuilder.append(it) 46 | textViewReceiced.text = stringBuilder.toString() 47 | } 48 | } 49 | .setServerConnectStatusCallback { status, bluetoothDevice -> 50 | MainScope().launch { 51 | if (status) { 52 | textViewConnectInfo.text = 53 | "设备名称:\t${bluetoothDevice?.name}\n" + 54 | "设备地址:\t${bluetoothDevice?.address}\n" + 55 | "设备类型:\t${bluetoothDevice?.type}" 56 | 57 | }else 58 | textViewConnectInfo.text = "" 59 | } 60 | } 61 | .build(this) 62 | 63 | 64 | buttonConnect.setOnClickListener { 65 | serialPort.openDiscoveryActivity() 66 | } 67 | 68 | 69 | buttonDisconnect.setOnClickListener { 70 | // serialPort.disconnect() 71 | serialPortServer.disconnect() 72 | } 73 | 74 | buttonSend.setOnClickListener { 75 | // serialPort.sendData(editTextTextSend.text.toString()) 76 | serialPortServer.sendData(editTextTextSend.text.toString()) 77 | } 78 | 79 | buttonOpenServer.setOnClickListener { 80 | serialPortServer.openServer() 81 | } 82 | 83 | buttonCloseServer.setOnClickListener { 84 | serialPortServer.closeServer() 85 | } 86 | 87 | } 88 | } -------------------------------------------------------------------------------- /app/src/main/java/world/shanya/serialportsample/MyViewModel.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * 项目名:SerialPortSample 3 | * 模块名:SerialPortSample.app 4 | * 类名:MyViewModel.kt 5 | * 作者:Shanya 6 | * 日期:2022/3/13 下午10:43 7 | * Copyright (c) 2021 https://shanyaliux.cn 8 | */ 9 | 10 | package world.shanya.serialportsample 11 | 12 | import androidx.lifecycle.MutableLiveData 13 | import androidx.lifecycle.ViewModel 14 | import java.lang.StringBuilder 15 | 16 | class MyViewModel: ViewModel() { 17 | val deviceInfoLiveData = MutableLiveData() 18 | val receivedStringBuilder = StringBuilder() 19 | val receivedLiveData = MutableLiveData() 20 | } 21 | 22 | data class DeviceInfo( 23 | val isConnected: Boolean, 24 | val deviceName:String, 25 | val deviceAddr:String, 26 | val deviceType:String 27 | ) -------------------------------------------------------------------------------- /app/src/main/java/world/shanya/serialportsample/pagers/MessageFragment.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * 项目名:SerialPortSample 3 | * 模块名:SerialPortSample.app 4 | * 类名:MessageFragment.kt 5 | * 作者:Shanya 6 | * 日期:2022/3/13 下午9:07 7 | * Copyright (c) 2021 https://shanyaliux.cn 8 | */ 9 | 10 | package world.shanya.serialportsample.pagers 11 | 12 | import android.annotation.SuppressLint 13 | import android.os.Bundle 14 | import android.view.Gravity 15 | import android.view.LayoutInflater 16 | import android.view.View 17 | import android.view.ViewGroup 18 | import android.widget.Toast 19 | import androidx.fragment.app.Fragment 20 | import androidx.fragment.app.activityViewModels 21 | import kotlinx.android.synthetic.main.fragment_message.* 22 | import kotlinx.android.synthetic.main.fragment_message.view.* 23 | import world.shanya.serialport.SerialPort 24 | import world.shanya.serialport.SerialPortBuilder 25 | import world.shanya.serialportsample.MyViewModel 26 | import world.shanya.serialportsample.R 27 | 28 | 29 | class MessageFragment : Fragment() { 30 | 31 | private val myViewModel: MyViewModel by activityViewModels() 32 | 33 | @SuppressLint("SetTextI18n", "RtlHardcoded") 34 | override fun onCreate(savedInstanceState: Bundle?) { 35 | super.onCreate(savedInstanceState) 36 | 37 | myViewModel.deviceInfoLiveData.observe(this, { 38 | if (it.isConnected) { 39 | textViewInfo.gravity = Gravity.LEFT 40 | textViewInfo.text = 41 | "设备名称:\t${it.deviceName}\n设备地址:\t${it.deviceAddr}\n设备类型:\t${it.deviceType}" 42 | } else { 43 | textViewInfo.gravity = Gravity.CENTER 44 | textViewInfo.text = "当前未连接设备" 45 | } 46 | }) 47 | 48 | myViewModel.receivedLiveData.observe(this, { 49 | textViewReceiced.text = it.toString() 50 | }) 51 | } 52 | 53 | override fun onCreateView( 54 | inflater: LayoutInflater, container: ViewGroup?, 55 | savedInstanceState: Bundle? 56 | ): View? { 57 | // Inflate the layout for this fragment 58 | val root = inflater.inflate(R.layout.fragment_message, container, false) 59 | 60 | root.buttonSend.setOnClickListener { 61 | SerialPortBuilder.sendData(root.editTextTextSend.text.toString()) 62 | } 63 | 64 | root.switchReadType.setOnCheckedChangeListener { _, b -> 65 | if (b) { 66 | SerialPortBuilder.setReadDataType(SerialPort.READ_HEX) 67 | Toast.makeText(requireActivity(), "接收数据格式为十六进制", Toast.LENGTH_SHORT).show() 68 | } else { 69 | SerialPortBuilder.setReadDataType(SerialPort.READ_STRING) 70 | Toast.makeText(requireActivity(), "接收数据格式为字符串", Toast.LENGTH_SHORT).show() 71 | } 72 | } 73 | 74 | root.switchSendType.setOnCheckedChangeListener { _, b -> 75 | if (b) { 76 | SerialPortBuilder.setSendDataType(SerialPort.SEND_HEX) 77 | Toast.makeText(requireActivity(), "发送数据格式为十六进制", Toast.LENGTH_SHORT).show() 78 | } else { 79 | SerialPortBuilder.setSendDataType(SerialPort.SEND_STRING) 80 | Toast.makeText(requireActivity(), "发送数据格式为字符串", Toast.LENGTH_SHORT).show() 81 | } 82 | } 83 | 84 | return root 85 | } 86 | } -------------------------------------------------------------------------------- /app/src/main/java/world/shanya/serialportsample/utils/KeySpTools.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * 项目名:SerialPortSample 3 | * 模块名:SerialPortSample.app 4 | * 类名:KeySpTools.kt 5 | * 作者:Shanya 6 | * 日期:2022/3/15 下午11:58 7 | * Copyright (c) 2021 https://shanyaliux.cn 8 | */ 9 | 10 | package world.shanya.serialportsample.utils 11 | 12 | import android.content.Context 13 | 14 | data class KeyButtonInfo( 15 | val buttonName: String, 16 | val buttonData: String 17 | ) 18 | 19 | object KeySpTools { 20 | private const val SP_KeyInfo = "SerialPortKey" 21 | 22 | fun putButtonInfo(context: Context, id:Int, keyButtonInfo: KeyButtonInfo) { 23 | val sp = context.getSharedPreferences(SP_KeyInfo, Context.MODE_PRIVATE) 24 | val editor = sp.edit() 25 | editor.putString(id.toString() + "name", keyButtonInfo.buttonName) 26 | editor.putString(id.toString() + "data", keyButtonInfo.buttonData) 27 | editor.apply() 28 | } 29 | 30 | fun getButtonInfo(context: Context, id: Int): KeyButtonInfo { 31 | val sp = context.getSharedPreferences(SP_KeyInfo, Context.MODE_PRIVATE) 32 | return KeyButtonInfo(sp.getString(id.toString() + "name", "")?:"", 33 | sp.getString(id.toString() + "data", "")?:"") 34 | } 35 | } -------------------------------------------------------------------------------- /app/src/main/java/world/shanya/serialportsample/utils/SettingSPTools.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * 项目名:SerialPortSample 3 | * 模块名:SerialPortSample.app 4 | * 类名:SPTools.kt 5 | * 作者:Shanya 6 | * 日期:2022/3/13 下午8:42 7 | * Copyright (c) 2021 https://shanyaliux.cn 8 | */ 9 | 10 | package world.shanya.serialportsample.utils 11 | 12 | import android.content.Context 13 | import world.shanya.serialport.SerialPortConfig 14 | 15 | /** 16 | * 设置偏好存储类 17 | */ 18 | object SettingSPTools { 19 | private const val SP_SettingName = "SerialPortSetting" 20 | const val SPP_UUID = "SPP_UUID" 21 | const val BLE_R_UUID = "BLE_R_UUID" 22 | const val BLE_S_UUID = "BLE_S_UUID" 23 | const val AUTO_CONNECT = "AUTO_CONNECT" 24 | const val RECONNECT = "RECONNECT" 25 | const val INTERVALS_TIME = "INTERVALS_TIME" 26 | const val IGNORE_NO_NAME = "IGNORE_NO_NAME" 27 | const val CONNECTION_SELECT = "CONNECTION_SELECT" 28 | const val OPEN_DISCOVERY = "OPEN_DISCOVERY" 29 | const val HEX_TO_STRING = "HEX_TO_STRING" 30 | 31 | fun putData(context: Context, name:String, data: String) { 32 | val sp = context.getSharedPreferences(SP_SettingName, Context.MODE_PRIVATE) 33 | val editor = sp.edit() 34 | editor.putString(name, data) 35 | editor.apply() 36 | } 37 | 38 | fun getString(context: Context, key: String): String? { 39 | val sp = context.getSharedPreferences(SP_SettingName, Context.MODE_PRIVATE) 40 | return sp.getString(key, "") 41 | } 42 | 43 | fun getSerialPortConfig(context: Context): SerialPortConfig { 44 | val config = SerialPortConfig() 45 | if (getString(context, SPP_UUID) == "") { 46 | config.UUID_LEGACY = "00001101-0000-1000-8000-00805F9B34FB" 47 | } else { 48 | config.UUID_LEGACY = getString(context, SPP_UUID) 49 | } 50 | 51 | if (getString(context, BLE_R_UUID) == "") { 52 | config.UUID_BLE_READ = "0000ffe1-0000-1000-8000-00805f9b34fb" 53 | } else { 54 | config.UUID_BLE_READ = getString(context, BLE_R_UUID) 55 | } 56 | 57 | if (getString(context, BLE_S_UUID) == "") { 58 | config.UUID_BLE_SEND = "0000ffe1-0000-1000-8000-00805f9b34fb" 59 | } else { 60 | config.UUID_BLE_SEND = getString(context, BLE_S_UUID) 61 | } 62 | 63 | config.autoConnect = getSwitchSetting(context, AUTO_CONNECT) 64 | 65 | config.autoReconnect = getSwitchSetting(context, RECONNECT) 66 | 67 | if (getString(context, INTERVALS_TIME) == "") { 68 | config.reconnectAtIntervals = 10000 69 | } else { 70 | config.reconnectAtIntervals = getString(context, INTERVALS_TIME)?.toInt() ?: 10000 71 | } 72 | 73 | config.ignoreNoNameDevice = getSwitchSetting(context, IGNORE_NO_NAME) 74 | 75 | config.openConnectionTypeDialogFlag = getSwitchSetting(context, CONNECTION_SELECT) 76 | 77 | config.autoOpenDiscoveryActivity = getSwitchSetting(context, OPEN_DISCOVERY) 78 | 79 | config.autoHexStringToString = getSwitchSetting(context, HEX_TO_STRING) 80 | return config 81 | } 82 | 83 | fun getSwitchSetting(context: Context, switchLabel: String): Boolean { 84 | return if (getString(context, switchLabel) == "") { 85 | false 86 | } else { 87 | getString(context, switchLabel) != "false" 88 | } 89 | } 90 | 91 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_baseline_send_24.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_baseline_settings_24.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 11 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_logo_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 11 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_about.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 17 | 18 | 21 | 22 | 25 | 26 | 27 | 28 | 37 | -------------------------------------------------------------------------------- /app/src/main/res/layout/key_button_info_dialog.xml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 15 | 16 | 28 | 29 | 42 | 43 | 57 | 58 | 73 | -------------------------------------------------------------------------------- /app/src/main/res/menu/menu.xml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 12 | 13 | 17 | 20 | 23 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_logo.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_logo_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/app/src/main/res/mipmap-hdpi/ic_launcher_logo.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_logo_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/app/src/main/res/mipmap-hdpi/ic_launcher_logo_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/app/src/main/res/mipmap-mdpi/ic_launcher_logo.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_logo_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/app/src/main/res/mipmap-mdpi/ic_launcher_logo_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/app/src/main/res/mipmap-xhdpi/ic_launcher_logo.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_logo_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/app/src/main/res/mipmap-xhdpi/ic_launcher_logo_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/app/src/main/res/mipmap-xxhdpi/ic_launcher_logo.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_logo_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/app/src/main/res/mipmap-xxhdpi/ic_launcher_logo_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/app/src/main/res/mipmap-xxxhdpi/ic_launcher_logo.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_logo_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/app/src/main/res/mipmap-xxxhdpi/ic_launcher_logo_round.png -------------------------------------------------------------------------------- /app/src/main/res/values-night/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/values-zh-rCN/strings.xml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | 蓝牙串口助手 12 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFBB86FC 4 | #FF6200EE 5 | #FF3700B3 6 | #FF03DAC5 7 | #FF018786 8 | #FF000000 9 | #FFFFFFFF 10 | -------------------------------------------------------------------------------- /app/src/main/res/values/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3D65DC 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/ic_launcher_logo_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3D6ADC 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | SerialPort 3 | 4 | Hello blank fragment 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16 | -------------------------------------------------------------------------------- /app/src/test/java/world/shanya/serialportsample/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package world.shanya.serialportsample 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | buildscript { 3 | ext.kotlin_version = "1.3.72" 4 | repositories { 5 | google() 6 | jcenter() 7 | mavenCentral() 8 | 9 | } 10 | dependencies { 11 | classpath 'com.android.tools.build:gradle:7.2.1' 12 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10" 13 | // NOTE: Do not place your application dependencies here; they belong 14 | // in the individual module build.gradle files 15 | classpath 'com.vanniktech:gradle-maven-publish-plugin:0.18.0' 16 | } 17 | } 18 | 19 | allprojects { 20 | repositories { 21 | google() 22 | maven { url "https://jitpack.io" } 23 | mavenCentral() 24 | } 25 | tasks.withType(Javadoc) { 26 | options.addStringOption('Xdoclint:none', '-quiet') 27 | options.addStringOption('encoding', 'UTF-8') 28 | } 29 | 30 | plugins.withId("com.vanniktech.maven.publish") { 31 | mavenPublish { 32 | sonatypeHost = "S01" 33 | } 34 | } 35 | } 36 | 37 | task clean(type: Delete) { 38 | delete rootProject.buildDir 39 | } -------------------------------------------------------------------------------- /docs/en/Makefile: -------------------------------------------------------------------------------- 1 | # Minimal makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line, and also 5 | # from the environment for the first two. 6 | SPHINXOPTS ?= 7 | SPHINXBUILD ?= sphinx-build 8 | SOURCEDIR = . 9 | BUILDDIR = _build 10 | 11 | # Put it first so that "make" without argument is like "make help". 12 | help: 13 | @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 14 | 15 | .PHONY: help Makefile 16 | 17 | # Catch-all target: route all unknown targets to Sphinx using the new 18 | # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). 19 | %: Makefile 20 | @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 21 | -------------------------------------------------------------------------------- /docs/en/_static/css/readthedocs.css: -------------------------------------------------------------------------------- 1 | .header-logo { 2 | background-image: url("../image/logo.png"); 3 | background-size: 156px 40px; 4 | height: 40px; 5 | width: 156px; 6 | } 7 | -------------------------------------------------------------------------------- /docs/en/_static/image/baidu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/docs/en/_static/image/baidu.png -------------------------------------------------------------------------------- /docs/en/_static/image/google.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/docs/en/_static/image/google.png -------------------------------------------------------------------------------- /docs/en/_static/image/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/docs/en/_static/image/logo.png -------------------------------------------------------------------------------- /docs/en/_static/image/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/docs/en/_static/image/logo192.png -------------------------------------------------------------------------------- /docs/en/_static/image/qq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/docs/en/_static/image/qq.png -------------------------------------------------------------------------------- /docs/en/_static/image/qq_wx.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/docs/en/_static/image/qq_wx.png -------------------------------------------------------------------------------- /docs/en/_static/image/wx.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/docs/en/_static/image/wx.png -------------------------------------------------------------------------------- /docs/en/changelog.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ### **4.2.0** was released in 7/6/2022: 4 | - [Fix] Some compilation warnings about bluetooth permissions 5 | - [Modify] Upgrade `Kotlin` and `Gradle` versions 6 | - [Modify] Mark `ConnectionResultCallback` obsolete 7 | - [Feature] Added Ble device to send byte array 8 | - [Feature] Added Ble device can work callback 9 | - [Feature] Added Server 10 | 11 | ### **4.1.9** was released in 9/5/2022: 12 | - [Fix] When the sendUUID is incorrect, it crashes 13 | 14 | ### **4.1.8** was released in 14/4/2022: 15 | - fix autoOpenDiscoveryActivity is always true 16 | - DiscoveryActivity add English 17 | - add setDiscoveryTime -------------------------------------------------------------------------------- /docs/en/conf.py: -------------------------------------------------------------------------------- 1 | # Configuration file for the Sphinx documentation builder. 2 | # 3 | # This file only contains a selection of the most common options. For a full 4 | # list see the documentation: 5 | # https://www.sphinx-doc.org/en/master/usage/configuration.html 6 | 7 | # -- Path setup -------------------------------------------------------------- 8 | 9 | # If extensions (or modules to document with autodoc) are in another directory, 10 | # add these directories to sys.path here. If the directory is relative to the 11 | # documentation root, use os.path.abspath to make it absolute, like shown here. 12 | # 13 | import os 14 | import subprocess 15 | import sys 16 | 17 | import pytorch_sphinx_theme 18 | 19 | sys.path.insert(0, os.path.abspath('../..')) 20 | 21 | # -- Project information ----------------------------------------------------- 22 | 23 | project = 'SerialPort' 24 | copyright = '2021-2022, Shanya' 25 | author = 'Shanya' 26 | language = 'en' 27 | 28 | # def get_version(): 29 | # with open(version_file, 'r') as f: 30 | # exec(compile(f.read(), version_file, 'exec')) 31 | # return locals()['__version__'] 32 | 33 | 34 | # The full version, including alpha/beta/rc tags 35 | # release = get_version() 36 | 37 | # -- General configuration --------------------------------------------------- 38 | 39 | # Add any Sphinx extension module names here, as strings. They can be 40 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 41 | # ones. 42 | extensions = [ 43 | 'sphinx.ext.autodoc', 44 | 'sphinx.ext.napoleon', 45 | 'sphinx.ext.viewcode', 46 | 'recommonmark', 47 | 'sphinx_markdown_tables', 48 | 'sphinx_copybutton', 49 | ] 50 | 51 | # Add any paths that contain templates here, relative to this directory. 52 | templates_path = ['_templates'] 53 | 54 | # The suffix(es) of source filenames. 55 | # You can specify multiple suffix as a list of string: 56 | # 57 | source_suffix = { 58 | '.rst': 'restructuredtext', 59 | '.md': 'markdown', 60 | } 61 | 62 | # The master toctree document. 63 | master_doc = 'index' 64 | 65 | # List of patterns, relative to source directory, that match files and 66 | # directories to ignore when looking for source files. 67 | # This pattern also affects html_static_path and html_extra_path. 68 | exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] 69 | 70 | # -- Options for HTML output ------------------------------------------------- 71 | 72 | # The theme to use for HTML and HTML Help pages. See the documentation for 73 | # a list of builtin themes. 74 | # 75 | # html_theme = 'sphinx_rtd_theme' 76 | html_theme = 'pytorch_sphinx_theme' 77 | html_theme_path = [pytorch_sphinx_theme.get_html_theme_path()] 78 | 79 | html_theme_options = { 80 | 'menu': [ 81 | { 82 | 'name': 'GitHub', 83 | 'url': 'https://github.com/Shanyaliux/SerialPortSample' 84 | }, 85 | ], 86 | # Specify the language of shared menu 87 | 'menu_lang': 'en' 88 | } 89 | 90 | # Add any paths that contain custom static files (such as style sheets) here, 91 | # relative to this directory. They are copied after the builtin static files, 92 | # so a file named "default.css" will overwrite the builtin "default.css". 93 | html_static_path = ['_static'] 94 | html_css_files = ['css/readthedocs.css'] 95 | 96 | # -- Extension configuration ------------------------------------------------- 97 | # Ignore >>> when copying code 98 | copybutton_prompt_text = r'>>> |\.\.\. ' 99 | copybutton_prompt_is_regexp = True 100 | 101 | 102 | # def builder_inited_handler(app): 103 | # subprocess.run(['./stat.py']) 104 | 105 | 106 | # def setup(app): 107 | # app.connect('builder-inited', builder_inited_handler) 108 | -------------------------------------------------------------------------------- /docs/en/index.rst: -------------------------------------------------------------------------------- 1 | Welcome to SerialPort's documentation! 2 | ======================================= 3 | 4 | .. toctree:: 5 | :maxdepth: 2 6 | :caption: Get Started 7 | 8 | tutorials/install 9 | tutorials/getting_started_kotlin 10 | tutorials/getting_started_java 11 | tutorials/server 12 | 13 | .. toctree:: 14 | :maxdepth: 2 15 | :caption: Tutorials (Kotlin) 16 | 17 | tutorials/configuration_kotlin 18 | tutorials/discovery_connect_kotlin 19 | tutorials/received_send_kotlin 20 | tutorials/toast_kotlin 21 | tutorials/tools_kotlin 22 | 23 | .. toctree:: 24 | :maxdepth: 2 25 | :caption: Tutorials (Java) 26 | 27 | tutorials/configuration_java 28 | tutorials/discovery_connect_java 29 | tutorials/received_send_java 30 | tutorials/toast_java 31 | tutorials/tools_java 32 | 33 | 34 | .. toctree:: 35 | :maxdepth: 2 36 | :caption: Notes 37 | 38 | questions 39 | changelog 40 | sponsor 41 | 42 | .. toctree:: 43 | :caption: Switch Language 44 | 45 | switch_language.md 46 | 47 | 48 | .. Indices and tables 49 | .. ================== 50 | 51 | .. * :ref:`genindex` 52 | .. * :ref:`search` 53 | -------------------------------------------------------------------------------- /docs/en/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | pushd %~dp0 4 | 5 | REM Command file for Sphinx documentation 6 | 7 | if "%SPHINXBUILD%" == "" ( 8 | set SPHINXBUILD=sphinx-build 9 | ) 10 | set SOURCEDIR=. 11 | set BUILDDIR=_build 12 | 13 | if "%1" == "" goto help 14 | 15 | %SPHINXBUILD% >NUL 2>NUL 16 | if errorlevel 9009 ( 17 | echo. 18 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 19 | echo.installed, then set the SPHINXBUILD environment variable to point 20 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 21 | echo.may add the Sphinx directory to PATH. 22 | echo. 23 | echo.If you don't have Sphinx installed, grab it from 24 | echo.http://sphinx-doc.org/ 25 | exit /b 1 26 | ) 27 | 28 | %SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% 29 | goto end 30 | 31 | :help 32 | %SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% 33 | 34 | :end 35 | popd 36 | -------------------------------------------------------------------------------- /docs/en/questions.md: -------------------------------------------------------------------------------- 1 | # FREQUENTLY ASKED QUESTIONS 2 | 3 | ## Feature support 4 | 5 | ### Does it support BLE devices? 6 | 7 | Certainly,`SerialPort` fully supports BLE devices since version `4.0.0`. 8 | 9 | ### Is there an automatic reconnection mechanism? 10 | 11 | Yes, it can be set to reconnect at startup, or it can be automatically reconnected at intervals.See [auto reconnect](./tutorials/configuration_kotlin.html#id3) 12 | 13 | ## Common problem 14 | 15 | ### Why is the list of available devices for a custom page empty? 16 | 17 | After Android 6.0, you must give the positioning permission to search for available devices. 18 | 19 | ### Why does the BLE device connect successfully, but cannot send and receive messages, and sometimes an exception occurs? 20 | 21 | After the BLE device is successfully connected, it is necessary to set the correct UUID for normal communication.For the specific setting method, see [Set BLE device UUID](./tutorials/discovery_connect_kotlin.html#ble) 22 | 23 | ## How to solve other problems? 24 | 25 | ### Enable debug mode 26 | 27 | Turn on debug mode to view the printed log information. See [debug mode](./tutorials/configuration_kotlin.html#id2) 28 | 29 | ### Take advantage of a powerful search engine 30 | 31 | ![](./_static/image/google.png) 32 | 33 | ![](./_static/image/baidu.png) 34 | 35 | ### Issues 36 | 37 | If the above methods still do not solve the problem, Please open [issues](https://github.com/Shanyaliux/SerialPortSample/issues). 38 | 39 | 40 | -------------------------------------------------------------------------------- /docs/en/sponsor.md: -------------------------------------------------------------------------------- 1 | # Sponsor 2 | 3 | SerialPort is an open source project licensed under the Apache-2.0 license. It is completely free to use, but your sponsorship can make SerialPort more healthy and stable. 4 | 5 | ------ 6 | 7 | If you recognize the author's efforts or SerialPort really helped you, please go to [Gitee](https://gitee.com/Shanya/SerialPortSample) and [GitHub](https://github.com/Shanyaliux/SerialPortSample) to click star to encourage it. In addition, if you have enough pockets, you can scan the following sponsorship code to become a SerialPort sponsor: 8 | 9 | ![](./_static/image/wx.png) 10 | 11 | -------------------------------------------------------------------------------- /docs/en/switch_language.md: -------------------------------------------------------------------------------- 1 | ## English 2 | 3 | ## 简体中文 -------------------------------------------------------------------------------- /docs/en/tutorials/getting_started_java.md: -------------------------------------------------------------------------------- 1 | # Basic usage (Java) 2 | 3 | ### Build SerialPort instance 4 | 5 | ```java 6 | SerialPort serialPort = SerialPortBuilder.INSTANCE.build(this); 7 | ``` 8 | 9 | ### Search device 10 | 11 | Use the method `doDiscovery(context)` to search for devices: 12 | 13 | ```java 14 | serialPort.doDiscovery(this); 15 | ``` 16 | 17 | Use the methods `getPairedDevicesListBD()` and `getUnPairedDevicesListBD()` to get search results: 18 | 19 | ```java 20 | serialPort.getPairedDevicesListBD(); //Get a list of paired devices 21 | serialPort.getUnPairedDevicesListBD(); //Get a list of unpaired devices 22 | ``` 23 | 24 | **If the search is not over, the list of unpaired devices may be empty or incomplete.** 25 | 26 | 27 | ### Connect the device 28 | 29 | Setting the correct UUID is an essential step in order to successfully connect the device and complete the communication. 30 | 31 | #### Set legacy device UUID 32 | 33 | Use the static method `setLegacyUUID(uuid)` of `SerialPort` to set the UUID of the legacy device: 34 | 35 | ```java 36 | SerialPort.Companion.setLegacyUUID("00001101-0000-1000-8000-00805F9B34FB"); 37 | ``` 38 | 39 | For traditional devices **generally**, you can use the default UUID without setting UUID. 40 | 41 | 42 | #### Set BLE device UUID 43 | 44 | Use the static method `setLegacyUUID(uuid)` of `SerialPort` to set the UUID of the BLE device: 45 | 46 | ```java 47 | SerialPort.Companion.setBleUUID("0000ffe1-0000-1000-8000-00805f9b34fb"); 48 | ``` 49 | 50 | In most cases, BLE devices need to set UUID. For specific UUID, you can check the manual or consult the seller. 51 | 52 | In addition, you can also use the method `printPossibleBleUUID()` to print out a feasible UUID, and try it yourself: 53 | 54 | ```java 55 | serialPort.printPossibleBleUUID() 56 | ``` 57 | 58 | 59 | #### Establish connection 60 | 61 | Use the method `openDiscoveryActivity()` to open the built-in search page and select a device to connect to: 62 | 63 | ```java 64 | serialPort.openDiscoveryActivity(); 65 | ``` 66 | 67 | **What if you don't want to use the built-in search page? ** 68 | 69 | You can set up a custom search page or connect directly using the device address. See [Use a custom interface](/discovery_connect_java.html#id3) 70 | 71 | 72 | ### Receive message 73 | 74 | Use the method `setReceivedDataCallback(receivedDataCallback)` to set up a received message listener: 75 | 76 | 77 | ```java 78 | serialPort.setReceivedDataCallback( (data) -> { 79 | 80 | return null; 81 | }); 82 | ``` 83 | 84 | In addition to this, you can also configure the listener when building the instance: 85 | 86 | ```java 87 | SerialPort serialPort = SerialPortBuilder.INSTANCE 88 | .setReceivedDataCallback( (data) -> { 89 | 90 | return null; 91 | }) 92 | .build(this); 93 | ``` 94 | 95 | ### Send a message 96 | 97 | Send a message using the method `sendData(data)`: 98 | 99 | ```java 100 | serialPort.sendData("Hello World"); 101 | ``` 102 | 103 | **At this point, you can quickly develop a serial port application that can complete basic sending and receiving data. Of course, `SerialPort` has many more functions, please continue to read the documentation. ** 104 | 105 | -------------------------------------------------------------------------------- /docs/en/tutorials/getting_started_kotlin.md: -------------------------------------------------------------------------------- 1 | # Basic usage (Kotlin) 2 | 3 | ### Build SerialPort instance 4 | 5 | ```Kotlin 6 | val serialPort = SerialPortBuilder.build(this) 7 | ``` 8 | ### Search device 9 | 10 | Use the method `doDiscovery(context)` to search for devices: 11 | ```kotlin 12 | serialPort.doDiscovery(this) 13 | ``` 14 | 15 | Use the methods `getPairedDevicesListBD()` and `getUnPairedDevicesListBD()` to get search results: 16 | 17 | 18 | ```kotlin 19 | serialPort.getPairedDevicesListBD() //Get a list of paired devices 20 | serialPort.getUnPairedDevicesListBD() //Get a list of unpaired devices 21 | ``` 22 | **If the search is not over, the list of unpaired devices may be empty or incomplete.** 23 | 24 | ### Connect the device 25 | 26 | Setting the correct UUID is an essential step in order to successfully connect the device and complete the communication. 27 | 28 | #### Set legacy device UUID 29 | 30 | Use the static method `setLegacyUUID(uuid)` of `SerialPort` to set the UUID of the legacy device: 31 | 32 | 33 | ```kotlin 34 | SerialPort.setLegacyUUID("00001101-0000-1000-8000-00805F9B34FB") 35 | ``` 36 | 37 | For traditional devices **generally**, you can use the default UUID without setting UUID. 38 | 39 | 40 | #### Set BLE device UUID 41 | 42 | Use the static method `setLegacyUUID(uuid)` of `SerialPort` to set the UUID of the BLE device: 43 | 44 | 45 | ```kotlin 46 | SerialPort.setBleUUID("0000ffe1-0000-1000-8000-00805f9b34fb") 47 | ``` 48 | 49 | In most cases, BLE devices need to set UUID. For specific UUID, you can check the manual or consult the seller. 50 | 51 | In addition, you can also use the method `printPossibleBleUUID()` to print out a feasible UUID, and try it yourself: 52 | 53 | ```kotlin 54 | serialPort.printPossibleBleUUID() 55 | ``` 56 | 57 | #### Establish connection 58 | 59 | Use the method `openDiscoveryActivity()` to open the built-in search page and select a device to connect to: 60 | 61 | 62 | ```kotlin 63 | serialPort.openDiscoveryActivity() 64 | ``` 65 | 66 | **What if you don't want to use the built-in search page? ** 67 | 68 | You can set up a custom search page or connect directly using the device address. See [Use a custom interface](./discovery_connect_kotlin.html#id3) 69 | 70 | ### Receive message 71 | 72 | Use the method `setReceivedDataCallback(receivedDataCallback)` to set up a received message listener: 73 | 74 | ```kotlin 75 | serialPort.setReceivedDataCallback { data -> 76 | 77 | } 78 | ``` 79 | 80 | In addition to this, you can also configure the listener when building the instance: 81 | 82 | ```kotlin 83 | val serialPort = SerialPortBuilder 84 | .setReceivedDataCallback { data -> 85 | 86 | } 87 | .build(this) 88 | ``` 89 | 90 | ### Send a message 91 | 92 | Send a message using the method `sendData(data)`: 93 | 94 | ```kotlin 95 | serialPort.sendData("Hello World") 96 | ``` 97 | 98 | **At this point, you can quickly develop a serial port application that can complete basic sending and receiving data. Of course, `SerialPort` has many more functions, please continue to read the documentation. ** 99 | 100 | -------------------------------------------------------------------------------- /docs/en/tutorials/install.md: -------------------------------------------------------------------------------- 1 | # Install 2 | 3 | Edit the `Build.gradle` file and add the following dependencies. 4 | 5 | ```groovy 6 | dependencies { 7 | implementation 'cn.shanyaliux.serialport:serialport:4.2.0' 8 | } 9 | ``` 10 | 11 | If you need to use `4.1.6` and below, do as follows: 12 | 1. Add `JitPack` repository 13 | Add the `JitPack` repository to your build file 14 | ```groovy 15 | allprojects { 16 | repositories { 17 | ... 18 | maven { url 'https://jitpack.io' } 19 | } 20 | } 21 | ``` 22 | 2. add dependencies 23 | ```groovy 24 | dependencies { 25 | implementation 'com.gitee.Shanya:SerialPortSample:4.1.6' //Chinese warehouse 26 | implementation 'com.github.Shanyaliux:SerialPortSample:4.1.6' //Foreign warehouse 27 | } 28 | ``` -------------------------------------------------------------------------------- /docs/en/tutorials/received_send_kotlin.md: -------------------------------------------------------------------------------- 1 | # Received and send 2 | 3 | ## Set data format 4 | 5 | Use the methods `setReadDataType(type)` and `setSendDataType(type)` to format the technique data: 6 | 7 | ### Set the received message format 8 | 9 | ```kotlin 10 | //SerialPort.READ_HEX hex 11 | //SerialPort.READ_STRING string 12 | //If not set, the default string form 13 | serialPort.setReadDataType(SerialPort.READ_HEX) 14 | ``` 15 | 16 | In addition to this, you can also set the received data format when building the instance: 17 | 18 | ```kotlin 19 | //SerialPort.READ_HEX hex 20 | //SerialPort.READ_STRING string 21 | //If not set, the default string form 22 | val serialPort = SerialPortBuilder 23 | .setReadDataType(SerialPort.READ_HEX) 24 | .build(this) 25 | ``` 26 | 27 | ### Set the send data format 28 | 29 | ```kotlin 30 | //SerialPort.SEND_HEX hex 31 | //SerialPort.SEND_STRING string 32 | //If not set, the default string form 33 | serialPort.setSendDataType(SerialPort.SEND_HEX ) 34 | ``` 35 | 36 | In addition to this, you can also set the send data format when building the instance: 37 | 38 | ```kotlin 39 | //SerialPort.SEND_HEX hex 40 | //SerialPort.SEND_STRING string 41 | //If not set, the default string form 42 | val serialPort = SerialPortBuilder 43 | .setSendDataType(SerialPort.SEND_HEX) 44 | .build(this) 45 | ``` 46 | 47 | Currently, the data sending and receiving for BLE devices does not support the setting format, only the string format is supported. If you really need the hexadecimal data format, you can temporarily implement it by referring to the processing method of traditional equipment. 48 | 49 | Reference code link: [HexStringToString](https://gitee.com/Shanya/SerialPortSample/blob/master/serialport/src/main/java/world/shanya/serialport/tools/SerialPortToolsByKotlin.kt#L112)、[StringToHex](https://gitee.com/Shanya/SerialPortSample/blob/master/serialport/src/main/java/world/shanya/serialport/tools/SerialPortToolsByKotlin.kt#L199) 50 | 51 | ## Receive message 52 | 53 | ### string and hex 54 | 55 | Use the method `setReceivedDataCallback(receivedDataCallback)` to set up a received message listener: 56 | 57 | ```kotlin 58 | serialPort.setReceivedDataCallback { data -> 59 | 60 | } 61 | ``` 62 | 63 | In addition to this, you can also configure the listener when building the instance: 64 | 65 | ```kotlin 66 | val serialPort = SerialPortBuilder 67 | .setReceivedDataCallback { data -> 68 | 69 | } 70 | .build(this) 71 | ``` 72 | 73 | ### Byte array 74 | 75 | When receiving a message, you can also choose to obtain a **byte array** as follows: 76 | 77 | ```kotlin 78 | serialPort.setReceivedBytesCallback { bytes -> 79 | 80 | } 81 | ``` 82 | 83 | In addition to this, you can also configure the listener when building the instance: 84 | 85 | ```kotlin 86 | val serialPort = SerialPortBuilder 87 | .setReceivedBytesCallback { bytes -> 88 | 89 | } 90 | .build(this) 91 | ``` 92 | 93 | ## Send a message 94 | 95 | Send a message using the method `sendData(data)`: 96 | 97 | ### String 98 | 99 | ```kotlin 100 | serialPort.sendData("Hello World") 101 | ``` 102 | 103 | ### hex 104 | 105 | ```kotlin 106 | serialPort.sendData("0C FF") 107 | ``` 108 | 109 | All hexadecimals should be **two digits**, with 0 in front of the less than two digits, case-insensitive. 110 | 111 | ### BLE device send bytes 112 | 113 | Now, BLE device support send bytes 114 | 115 | ```java 116 | serialPort.sendData(bytes) 117 | ``` -------------------------------------------------------------------------------- /docs/en/tutorials/server.md: -------------------------------------------------------------------------------- 1 | # Server 2 | 3 | Now, an Android server can be built to implement Bluetooth communication between two Androids. (Currently only supports single device connection) 4 | 5 | ## Build instance 6 | 7 | ```kotlin 8 | val serialPortServer = SerialPortServerBuilder 9 | .setServerName("SerialPortServer") 10 | .setServerUUID("00001101-0000-1000-8000-00805F9B34FB") 11 | .setServerReceivedDataCallback { 12 | 13 | } 14 | .setServerConnectStatusCallback { status, bluetoothDevice -> 15 | 16 | } 17 | .build(this) 18 | ``` 19 | 20 | - `setServerName` Set server name 21 | - `setServerUUID` Set the UUID of the server, the UUID of the traditional device needs to be set to the same as this when the client connects 22 | - `setServerReceivedDataCallback` The server receives message monitoring 23 | - `setServerConnectStatusCallback` Server connection status monitoring 24 | - `status` Connection Status 25 | - `bluetoothDevice` Connected device, null when `status` is false 26 | 27 | ## Open server 28 | 29 | Only after the service is opened, the client can connect to the server. 30 | 31 | ```kotlin 32 | serialPortServer.openServer() 33 | ``` 34 | 35 | ## Close server 36 | 37 | ```kotlin 38 | serialPortServer.closeServer() 39 | ``` 40 | 41 | ## Set the server discoverable state 42 | 43 | By default, if the server is turned on, it will be automatically set to be discoverable, and if the server is turned off, it will be set to be invisible. 44 | 45 | ```kotlin 46 | setServerDiscoverable(status) 47 | ``` 48 | - `status` is of type `Boolean`, indicating the discoverable status 49 | 50 | ## Disconnect 51 | 52 | Actively disconnect from the client 53 | 54 | ```kotlin 55 | serialPortServer.disconnect() 56 | ``` 57 | 58 | ## Send 59 | 60 | ```kotlin 61 | serialPortServer.sendData("Hello") 62 | ``` 63 | 64 | 65 | -------------------------------------------------------------------------------- /docs/en/tutorials/toast_java.md: -------------------------------------------------------------------------------- 1 | # Toast 2 | 3 | ## Configuration method 4 | 5 | ```java 6 | //whether to display 7 | SerialPortToast.INSTANCE.getConnectSucceeded().setStatus(true); 8 | //prompt content (content is a string id) 9 | SerialPortToast.INSTANCE.getConnectSucceeded().setContent(R.string.connectSucceededToast); 10 | //Display duration Toast.LENGTH_SHORT or Toast.LENGTH_LONG 11 | SerialPortToast.INSTANCE.getConnectSucceeded().setTime(Toast.LENGTH_SHORT); 12 | ``` 13 | 14 | ## Optional configuration items 15 | 16 | | item | describe | defaults | 17 | | :--------------------: | :---------------------------------------------------------: | :----------------------------------------------------------: | 18 | | connectSucceeded | When the connection is successful | Connection succeeded | 19 | | connectFailed | When the connection fails | Connection failed | 20 | | disconnect | When disconnected | Disconnect | 21 | | connectFirst | Send data when no device is connected | Please connect the device first | 22 | | disconnectFirst | Perform the connect operation after the device is connected | Please disconnect first | 23 | | permission | Ask whether to enable location permission | Please enable location permission first | 24 | | hexTip | When sending hexadecimal, the data format is incorrect | Please keep two digits for each hexadecimal data entered, and the insufficient is 0 in the front | 25 | | openBluetoothSucceeded | When the bluetooth is turned on successfully | Bluetooth turned on successfully | 26 | | openBluetoothFailed | When turning on bluetooth fails | Failed to turn on bluetooth | -------------------------------------------------------------------------------- /docs/en/tutorials/toast_kotlin.md: -------------------------------------------------------------------------------- 1 | # Toast 2 | 3 | ## Configuration method 4 | 5 | ```kotlin 6 | //whether to display 7 | SerialPortToast.connectSucceeded.status = true 8 | //prompt content (content is a string id) 9 | SerialPortToast.connectSucceeded.content = R.string.connectSucceededToast 10 | //Display duration Toast.LENGTH_SHORT or Toast.LENGTH_LONG 11 | SerialPortToast.connectSucceeded.time = Toast.LENGTH_SHORT 12 | ``` 13 | 14 | ## Optional configuration items 15 | 16 | | item | describe | defaults | 17 | | :--------------------: | :---------------------------------------------------------: | :----------------------------------------------------------: | 18 | | connectSucceeded | When the connection is successful | Connection succeeded | 19 | | connectFailed | When the connection fails | Connection failed | 20 | | disconnect | When disconnected | Disconnect | 21 | | connectFirst | Send data when no device is connected | Please connect the device first | 22 | | disconnectFirst | Perform the connect operation after the device is connected | Please disconnect first | 23 | | permission | Ask whether to enable location permission | Please enable location permission first | 24 | | hexTip | When sending hexadecimal, the data format is incorrect | Please keep two digits for each hexadecimal data entered, and the insufficient is 0 in the front | 25 | | openBluetoothSucceeded | When the bluetooth is turned on successfully | Bluetooth turned on successfully | 26 | | openBluetoothFailed | When turning on bluetooth fails | Failed to turn on bluetooth | -------------------------------------------------------------------------------- /docs/en/tutorials/tools_java.md: -------------------------------------------------------------------------------- 1 | # Tools 2 | 3 | ## Print UUID and its attributes 4 | 5 | If we don't know the UUID of the current BLE device, we can call the function `printPossibleBleUUID` to print out the optional UUID of the currently connected device 6 | 7 | Where `Properties` is a binary number, and the meaning of each bit is shown in the following table: 8 | 9 | | value (in hexadecimal) | mean | 10 | | :--------------------: | :-----------------------------: | 11 | | 0x01 | broadcastable | 12 | | 0x02 | readable | 13 | | 0x04 | can be written without response | 14 | | 0x08 | can be written | 15 | | 0x10 | supports notification | 16 | | 0x20 | supports indication | 17 | | 0x40 | supports write with signature | 18 | | 0x80 | has extended properties | 19 | 20 | 21 | 22 | ## String2hex 23 | 24 | ```java 25 | /** 26 | * Convert string to hexadecimal 27 | * @param str String to convert 28 | * @return hex array 29 | */ 30 | DataUtil.INSTANCE.string2hex("Hello"); 31 | ``` 32 | 33 | ## Bytes2string 34 | 35 | ```java 36 | /** 37 | * The byte array is converted into a string according to the required encoding format 38 | * @param bytes byte array to convert 39 | * @param charsetName Required encoding format 40 | * @return Converted string 41 | */ 42 | SerialPortTools.bytes2string(bytes, "GBK"); 43 | ``` 44 | 45 | ## String2bytes 46 | 47 | ```java 48 | /** 49 | * The string is converted into a byte array according to the required encoding format 50 | * @param string String to convert 51 | * @param charsetName Required encoding format 52 | * @return Converted byte array 53 | */ 54 | SerialPortTools.string2bytes("Hello", "GBK"); 55 | ``` 56 | -------------------------------------------------------------------------------- /docs/en/tutorials/tools_kotlin.md: -------------------------------------------------------------------------------- 1 | # Tools 2 | 3 | ## Print UUID and its attributes 4 | 5 | If we don't know the UUID of the current BLE device, we can call the function `printPossibleBleUUID` to print out the optional UUID of the currently connected device 6 | 7 | Where `Properties` is a binary number, and the meaning of each bit is shown in the following table: 8 | 9 | | value (in hexadecimal) | mean | 10 | | :--------------------: | :-----------------------------: | 11 | | 0x01 | broadcastable | 12 | | 0x02 | readable | 13 | | 0x04 | can be written without response | 14 | | 0x08 | can be written | 15 | | 0x10 | supports notification | 16 | | 0x20 | supports indication | 17 | | 0x40 | supports write with signature | 18 | | 0x80 | has extended properties | 19 | 20 | 21 | 22 | ## String2hex 23 | 24 | ```kotlin 25 | /** 26 | * Convert string to hexadecimal 27 | * @param str String to convert 28 | * @return hex array 29 | */ 30 | DataUtil.string2hex("Hello") 31 | ``` 32 | 33 | ## Bytes2string 34 | 35 | ```kotlin 36 | /** 37 | * The byte array is converted into a string according to the required encoding format 38 | * @param bytes byte array to convert 39 | * @param charsetName Required encoding format 40 | * @return Converted string 41 | */ 42 | SerialPortTools.bytes2string(bytes, "GBK") 43 | ``` 44 | 45 | ## String2bytes 46 | 47 | ```kotlin 48 | /** 49 | * The string is converted into a byte array according to the required encoding format 50 | * @param string String to convert 51 | * @param charsetName Required encoding format 52 | * @return Converted byte array 53 | */ 54 | SerialPortTools.bytes2string("Hello", "GBK") 55 | ``` 56 | -------------------------------------------------------------------------------- /docs/requirements.txt: -------------------------------------------------------------------------------- 1 | docutils==0.16.0 2 | myst-parser 3 | -e git+https://github.com/Shanyaliux/pytorch_sphinx_theme.git#egg=pytorch_sphinx_theme 4 | sphinx==4.0.2 5 | sphinx-copybutton 6 | sphinx_markdown_tables 7 | sphinx_rtd_theme==0.5.2 -------------------------------------------------------------------------------- /docs/zh_CN/Makefile: -------------------------------------------------------------------------------- 1 | # Minimal makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line, and also 5 | # from the environment for the first two. 6 | SPHINXOPTS ?= 7 | SPHINXBUILD ?= sphinx-build 8 | SOURCEDIR = . 9 | BUILDDIR = _build 10 | 11 | # Put it first so that "make" without argument is like "make help". 12 | help: 13 | @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 14 | 15 | .PHONY: help Makefile 16 | 17 | # Catch-all target: route all unknown targets to Sphinx using the new 18 | # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). 19 | %: Makefile 20 | @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 21 | -------------------------------------------------------------------------------- /docs/zh_CN/_static/css/readthedocs.css: -------------------------------------------------------------------------------- 1 | .header-logo { 2 | background-image: url("../image/logo.png"); 3 | background-size: 156px 40px; 4 | height: 40px; 5 | width: 156px; 6 | } 7 | -------------------------------------------------------------------------------- /docs/zh_CN/_static/image/baidu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/docs/zh_CN/_static/image/baidu.png -------------------------------------------------------------------------------- /docs/zh_CN/_static/image/google.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/docs/zh_CN/_static/image/google.png -------------------------------------------------------------------------------- /docs/zh_CN/_static/image/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/docs/zh_CN/_static/image/logo.png -------------------------------------------------------------------------------- /docs/zh_CN/_static/image/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/docs/zh_CN/_static/image/logo192.png -------------------------------------------------------------------------------- /docs/zh_CN/_static/image/qq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/docs/zh_CN/_static/image/qq.png -------------------------------------------------------------------------------- /docs/zh_CN/_static/image/qq_wx.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/docs/zh_CN/_static/image/qq_wx.png -------------------------------------------------------------------------------- /docs/zh_CN/_static/image/wx.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/docs/zh_CN/_static/image/wx.png -------------------------------------------------------------------------------- /docs/zh_CN/changelog.md: -------------------------------------------------------------------------------- 1 | # 更新日志 2 | 3 | ### **4.2.0** 版本已经在 7/6/2022 发布: 4 | - 修复一些关于蓝牙权限的编译告警 5 | - 升级`Kotlin`和`Gradle`版本 6 | - 标记`ConnectionResultCallback`过时 7 | - 新增Ble设备发送字节数组 8 | - 新增Ble设备可以工作回调 9 | - 新增服务端配置 10 | 11 | ### **4.1.9** 版本已经在 9/5/2022 发布: 12 | - 修复当发送UUID设置错误时应用闪退 13 | 14 | ### **4.1.8** 版本已经在 14/4/2022 发布: 15 | - 修复 autoOpenDiscoveryActivity 始终为 true 16 | - DiscoveryActivity 添加英文 17 | - 添加 setDiscoveryTime -------------------------------------------------------------------------------- /docs/zh_CN/index.rst: -------------------------------------------------------------------------------- 1 | Welcome to SerialPort's documentation! 2 | ======================================= 3 | 4 | .. toctree:: 5 | :maxdepth: 2 6 | :caption: 开始你的第一步 7 | 8 | tutorials/install 9 | tutorials/getting_started_kotlin 10 | tutorials/getting_started_java 11 | tutorials/server 12 | 13 | .. toctree:: 14 | :maxdepth: 2 15 | :caption: 教程 (Kotlin) 16 | 17 | tutorials/configuration_kotlin 18 | tutorials/discovery_connect_kotlin 19 | tutorials/received_send_kotlin 20 | tutorials/toast_kotlin 21 | tutorials/tools_kotlin 22 | 23 | .. toctree:: 24 | :maxdepth: 2 25 | :caption: 教程 (Java) 26 | 27 | tutorials/configuration_java 28 | tutorials/discovery_connect_java 29 | tutorials/received_send_java 30 | tutorials/toast_java 31 | tutorials/tools_java 32 | 33 | 34 | .. toctree:: 35 | :maxdepth: 2 36 | :caption: 说明 37 | 38 | questions 39 | changelog 40 | sponsor 41 | 42 | .. toctree:: 43 | :caption: 语言切换 44 | 45 | switch_language.md 46 | 47 | 48 | .. Indices and tables 49 | .. ================== 50 | 51 | .. * :ref:`genindex` 52 | .. * :ref:`search` 53 | -------------------------------------------------------------------------------- /docs/zh_CN/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | pushd %~dp0 4 | 5 | REM Command file for Sphinx documentation 6 | 7 | if "%SPHINXBUILD%" == "" ( 8 | set SPHINXBUILD=sphinx-build 9 | ) 10 | set SOURCEDIR=. 11 | set BUILDDIR=_build 12 | 13 | if "%1" == "" goto help 14 | 15 | %SPHINXBUILD% >NUL 2>NUL 16 | if errorlevel 9009 ( 17 | echo. 18 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 19 | echo.installed, then set the SPHINXBUILD environment variable to point 20 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 21 | echo.may add the Sphinx directory to PATH. 22 | echo. 23 | echo.If you don't have Sphinx installed, grab it from 24 | echo.http://sphinx-doc.org/ 25 | exit /b 1 26 | ) 27 | 28 | %SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% 29 | goto end 30 | 31 | :help 32 | %SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% 33 | 34 | :end 35 | popd 36 | -------------------------------------------------------------------------------- /docs/zh_CN/questions.md: -------------------------------------------------------------------------------- 1 | # 有疑必看 2 | 3 | ## 功能支持 4 | 5 | ### 支持BLE设备吗? 6 | 7 | 支持,`SerialPort` 从 `4.0.0` 版本开始全面支持BLE设备。 8 | 9 | ### 有自动重连机制吗? 10 | 11 | 有,可以设置在启动时重连,也可以间隔时间自动重连。详见[自动重连](./tutorials/configuration_kotlin.html#id3) 12 | 13 | ## 常见问题 14 | 15 | ### 为什么自定义页面的可用设备列表为空? 16 | 17 | 在Android6.0之后一定要给上定位权限才可以搜索到可用设备。 18 | 19 | ### 为什么BLE设备连接成功了,不能收发消息,有时还是发出异常? 20 | 21 | BLE设备连接成功后,还需要设置正确的UUID才可以正常通信。具体设置方法见[设置BLE设备UUID](./tutorials/discovery_connect_kotlin.html#ble) 22 | 23 | ## 还有其他问题怎么解决? 24 | 25 | ### 开启调试模式 26 | 27 | 打开调试模式查看打印的日志信息。详见[调试模式](./tutorials/configuration_kotlin.html#id2) 28 | 29 | ### 利用强大的搜索引擎 30 | 31 | ![](./_static/image/google.png) 32 | 33 | ![](./_static/image/baidu.png) 34 | 35 | ### 加群 36 | 37 | 若通过以上方法仍未解决问题,请加入QQ技术交流群。 38 | 39 | ![](./_static/image/qq.png) 40 | 41 | -------------------------------------------------------------------------------- /docs/zh_CN/sponsor.md: -------------------------------------------------------------------------------- 1 | # 赞助 2 | 3 | SerialPort 是采用 Apache-2.0 许可的开源项目,使用完全免费,但您的赞助可以使 SerialPort 获得更健康稳定的发展。 4 | 5 | ------ 6 | 7 | 如果您认可作者的努力或者 SerialPort 确实帮助到了您,请到 [Gitee](https://gitee.com/Shanya/SerialPortSample)和 [GitHub](https://github.com/Shanyaliux/SerialPortSample)上点个 Star 鼓励一下吧。另外如果您口袋充裕,可以扫以下的赞助码,成为 SerialPort 的赞助人: 8 | 9 | ![](./_static/image/wx.png) 10 | 11 | -------------------------------------------------------------------------------- /docs/zh_CN/switch_language.md: -------------------------------------------------------------------------------- 1 | ## English 2 | 3 | ## 简体中文 -------------------------------------------------------------------------------- /docs/zh_CN/tutorials/getting_started_java.md: -------------------------------------------------------------------------------- 1 | # 快速上手 (Java) 2 | 3 | ### 构建SerialPort实例 4 | 5 | ```java 6 | SerialPort serialPort = SerialPortBuilder.INSTANCE.build(this); 7 | ``` 8 | 9 | ### 搜索设备 10 | 11 | 使用方法 `doDiscovery(context)` 搜索设备: 12 | 13 | ```java 14 | serialPort.doDiscovery(this); 15 | ``` 16 | 17 | 使用方法 `getPairedDevicesListBD()` 和 `getUnPairedDevicesListBD()` 获取搜索结果: 18 | 19 | ```java 20 | serialPort.getPairedDevicesListBD(); //获取已配对设备列表 21 | serialPort.getUnPairedDevicesListBD(); //获取未配对设备列表 22 | ``` 23 | 24 | **如果搜索未结束,则可能获取的未配对设备列表为空或者不全.** 25 | 26 | 27 | ### 连接设备 28 | 29 | 想要成功的连接设备,并且完成通信,设置正确的UUID是必不可少的一步。 30 | 31 | #### 设置传统设备UUID 32 | 33 | 使用 `SerialPort` 的静态方法 `setLegacyUUID(uuid)` 设置传统设备的UUID: 34 | 35 | ```java 36 | SerialPort.Companion.setLegacyUUID("00001101-0000-1000-8000-00805F9B34FB"); 37 | ``` 38 | 39 | 传统设备**一般**情况下,可以不用设置UUID,使用默认的即可。 40 | 41 | 42 | #### 设置BLE设备UUID 43 | 44 | 使用 `SerialPort` 的静态方法 `setLegacyUUID(uuid)` 设置BLE设备的UUID: 45 | 46 | ```java 47 | SerialPort.Companion.setBleUUID("0000ffe1-0000-1000-8000-00805f9b34fb"); 48 | ``` 49 | 50 | BLE设备大多数情况下都需要设置UUID,具体的UUID可以查询手册或咨询卖家。 51 | 52 | 除此之外,也可以使用方法 `printPossibleBleUUID()` 打印出可行的UUID,自行选择尝试: 53 | 54 | ```java 55 | serialPort.printPossibleBleUUID() 56 | ``` 57 | 58 | 59 | #### 建立连接 60 | 61 | 使用方法 `openDiscoveryActivity()` 打开内置的搜索页面选择设备进行连接: 62 | 63 | ```java 64 | serialPort.openDiscoveryActivity(); 65 | ``` 66 | 67 | **不想使用内置的搜索页面怎么办?** 68 | 69 | 可以设置自定义的搜索页面或者直接使用设备地址进行连接。详见[使用自定义的界面](/discovery_connect_java.html#id3) 70 | 71 | 72 | ### 接收消息 73 | 74 | 使用方法 `setReceivedDataCallback(receivedDataCallback)` 设置一个接收消息监听器: 75 | 76 | 77 | ```java 78 | serialPort.setReceivedDataCallback( (data) -> { 79 | 80 | return null; 81 | }); 82 | ``` 83 | 84 | 除此之外,你还可以在构建实例时配置监听器: 85 | 86 | ```java 87 | SerialPort serialPort = SerialPortBuilder.INSTANCE 88 | .setReceivedDataCallback( (data) -> { 89 | 90 | return null; 91 | }) 92 | .build(this); 93 | ``` 94 | 95 | ### 发送消息 96 | 97 | 使用方法 `sendData(data)` 发送消息: 98 | 99 | ```java 100 | serialPort.sendData("Hello World"); 101 | ``` 102 | 103 | **至此,你已经可以快速的开发一款能够完成基本收发数据的串口应用了。当然,`SerialPort` 还有着更多的功能,请继续阅读说明文档。** 104 | 105 | 106 | -------------------------------------------------------------------------------- /docs/zh_CN/tutorials/getting_started_kotlin.md: -------------------------------------------------------------------------------- 1 | # 快速上手 (Kotlin) 2 | 3 | ### 构建SerialPort实例 4 | 5 | ```Kotlin 6 | val serialPort = SerialPortBuilder.build(this) 7 | ``` 8 | ### 搜索设备 9 | 10 | 使用方法 `doDiscovery(context)` 搜索设备: 11 | ```kotlin 12 | serialPort.doDiscovery(this) 13 | ``` 14 | 15 | 使用方法 `getPairedDevicesListBD()` 和 `getUnPairedDevicesListBD()` 获取搜索结果: 16 | 17 | 18 | ```kotlin 19 | serialPort.getPairedDevicesListBD() //获取已配对设备列表 20 | serialPort.getUnPairedDevicesListBD() //获取未配对设备列表 21 | ``` 22 | ### 连接设备 23 | 24 | 想要成功的连接设备,并且完成通信,设置正确的UUID是必不可少的一步。 25 | 26 | #### 设置传统设备UUID 27 | 28 | 使用 `SerialPort` 的静态方法 `setLegacyUUID(uuid)` 设置传统设备的UUID: 29 | 30 | 31 | ```kotlin 32 | SerialPort.setLegacyUUID("00001101-0000-1000-8000-00805F9B34FB") 33 | ``` 34 | 35 | 传统设备**一般**情况下,可以不用设置UUID,使用默认的即可。 36 | 37 | 38 | #### 设置BLE设备UUID 39 | 40 | 使用 `SerialPort` 的静态方法 `setBleUUID(uuid)` 设置BLE设备的UUID: 41 | 42 | 43 | ```kotlin 44 | SerialPort.setBleUUID("0000ffe1-0000-1000-8000-00805f9b34fb") 45 | ``` 46 | 47 | 48 | BLE设备大多数情况下都需要设置UUID,具体的UUID可以查询手册或咨询卖家。 49 | 50 | 除此之外,也可以使用方法 `printPossibleBleUUID()` 打印出可行的UUID,自行选择尝试: 51 | 52 | ```kotlin 53 | serialPort.printPossibleBleUUID() 54 | ``` 55 | 56 | 57 | #### 建立连接 58 | 59 | 使用方法 `openDiscoveryActivity()` 打开内置的搜索页面选择设备进行连接: 60 | 61 | 62 | ```kotlin 63 | serialPort.openDiscoveryActivity() 64 | ``` 65 | 66 | **不想使用内置的搜索页面怎么办?** 67 | 68 | 可以设置自定义的搜索页面或者直接使用设备地址进行连接。详见[使用自定义的界面](./discovery_connect_kotlin.html#id3) 69 | 70 | 71 | ### 接收消息 72 | 73 | 使用方法 `setReceivedDataCallback(receivedDataCallback)` 设置一个接收消息监听器: 74 | 75 | ```kotlin 76 | serialPort.setReceivedDataCallback { data -> 77 | 78 | } 79 | ``` 80 | 81 | 除此之外,你还可以在构建实例时配置监听器: 82 | 83 | ```kotlin 84 | val serialPort = SerialPortBuilder 85 | .setReceivedDataCallback { data -> 86 | 87 | } 88 | .build(this) 89 | ``` 90 | 91 | ### 发送消息 92 | 93 | 使用方法 `sendData(data)` 发送消息: 94 | 95 | ```kotlin 96 | serialPort.sendData("Hello World") 97 | ``` 98 | 99 | **至此,你已经可以快速的开发一款能够完成基本收发数据的串口应用了。当然,`SerialPort` 还有着更多的功能,请继续阅读说明文档。** 100 | 101 | 102 | -------------------------------------------------------------------------------- /docs/zh_CN/tutorials/install.md: -------------------------------------------------------------------------------- 1 | # 安装 2 | 3 | 编辑`Build.gradle`文件并添加以下依赖项。 4 | 5 | ```groovy 6 | dependencies { 7 | implementation 'cn.shanyaliux.serialport:serialport:4.2.0' 8 | } 9 | ``` 10 | 11 | 如果你需要使用`4.1.6`及其以下版本,则按如下操作: 12 | 1. 添加 `JitPack` 仓库 13 | 将 `JitPack` 存储库添加到您的构建文件中 14 | ```groovy 15 | allprojects { 16 | repositories { 17 | ... 18 | maven { url 'https://jitpack.io' } 19 | } 20 | } 21 | ``` 22 | 2. 添加依赖 23 | ```groovy 24 | dependencies { 25 | implementation 'com.gitee.Shanya:SerialPortSample:4.1.6' //国内仓库 26 | implementation 'com.github.Shanyaliux:SerialPortSample:4.1.6' //国外仓库 27 | } 28 | ``` -------------------------------------------------------------------------------- /docs/zh_CN/tutorials/received_send_java.md: -------------------------------------------------------------------------------- 1 | # 收发数据 2 | 3 | ## 设置数据格式 4 | 5 | 使用方法 `setReadDataType(type)` 和 `setSendDataType(type)` 来设置手法数据的格式: 6 | 7 | ### 设置接收消息格式 8 | 9 | ```java 10 | //SerialPort.READ_HEX 十六进制 11 | //SerialPort.READ_STRING 字符串 12 | //不设置则默认字符串形式 13 | serialPort.setReadDataType(SerialPort.READ_HEX); 14 | ``` 15 | 16 | 除此之外,你还可以在构建实例时设置接收数据格式: 17 | 18 | ```java 19 | //SerialPort.READ_HEX 十六进制 20 | //SerialPort.READ_STRING 字符串 21 | //不设置则默认字符串形式 22 | SerialPort serialPort = SerialPortBuilder.INSTANCE 23 | .setReadDataType(SerialPort.READ_HEX) 24 | .build(this); 25 | ``` 26 | 27 | ### 设置发送数据格式 28 | 29 | ```java 30 | //SerialPort.SEND_HEX 十六进制 31 | //SerialPort.SEND_STRING 字符串 32 | //不设置则默认字符串形式 33 | serialPort.setSendDataType(SerialPort.SEND_HEX); 34 | ``` 35 | 36 | 除此之外,你还可以在构建实例时设置接收数据格式: 37 | 38 | ```java 39 | //SerialPort.SEND_HEX 十六进制 40 | //SerialPort.SEND_STRING 字符串 41 | //不设置则默认字符串形式 42 | SerialPort serialPort = SerialPortBuilder.INSTANCE 43 | .setSendDataType(SerialPort.SEND_HEX) 44 | .build(this); 45 | ``` 46 | 47 | 目前针对于BLE设备的数据收发暂不支持设置格式,仅支持字符串格式。如果实在需要十六进制的数据格式,暂时可以参考传统设备的处理方式自行实现。 48 | 49 | 参考代码链接:[HexStringToString](https://gitee.com/Shanya/SerialPortSample/blob/master/serialport/src/main/java/world/shanya/serialport/tools/SerialPortToolsByKotlin.kt#L112)、[StringToHex](https://gitee.com/Shanya/SerialPortSample/blob/master/serialport/src/main/java/world/shanya/serialport/tools/SerialPortToolsByKotlin.kt#L199) 50 | 51 | ## 接收消息 52 | 53 | ### 字符串 和 十六进制 54 | 55 | 使用方法 `setReceivedDataCallback(receivedDataCallback)` 设置一个接收消息监听器: 56 | 57 | ```java 58 | serialPort.setReceivedDataCallback( (data) -> { 59 | 60 | return null; 61 | }); 62 | ``` 63 | 64 | 除此之外,你还可以在构建实例时配置监听器: 65 | 66 | ```java 67 | SerialPort serialPort = SerialPortBuilder.INSTANCE 68 | .setReceivedDataCallback( (data) -> { 69 | 70 | return null; 71 | }) 72 | .build(this); 73 | ``` 74 | 75 | ### 字节数组 76 | 77 | 在接收消息的时候,也可以选择获取**字节数组**,方法如下: 78 | 79 | ```java 80 | serialPort.setReceivedBytesCallback( (bytes) -> { 81 | 82 | return null; 83 | }); 84 | ``` 85 | 86 | 除此之外,你还可以在构建实例时配置监听器: 87 | 88 | ```java 89 | SerialPort serialPort = SerialPortBuilder.INSTANCE 90 | .setReceivedBytesCallback( (bytes) -> { 91 | 92 | return null; 93 | }) 94 | .build(this); 95 | ``` 96 | 97 | ## 发送消息 98 | 99 | 使用方法 `sendData(data)` 发送消息: 100 | 101 | ### 字符串 102 | 103 | ```java 104 | serialPort.sendData("Hello World"); 105 | ``` 106 | 107 | ### 十六进制 108 | 109 | ```java 110 | serialPort.sendData("0C FF"); 111 | ``` 112 | 113 | 所有的十六进制应为**两位**,不足两位的前方补0,不区分大小写。 114 | 115 | ### BLE设备发送字节数组 116 | 目前BLE设备支持直接发送字节数组 117 | 118 | ```java 119 | serialPort.sendData(bytes); 120 | ``` 121 | -------------------------------------------------------------------------------- /docs/zh_CN/tutorials/received_send_kotlin.md: -------------------------------------------------------------------------------- 1 | # 收发数据 2 | 3 | ## 设置数据格式 4 | 5 | 使用方法 `setReadDataType(type)` 和 `setSendDataType(type)` 来设置手法数据的格式: 6 | 7 | ### 设置接收消息格式 8 | 9 | ```kotlin 10 | //SerialPort.READ_HEX 十六进制 11 | //SerialPort.READ_STRING 字符串 12 | //不设置则默认字符串形式 13 | serialPort.setReadDataType(SerialPort.READ_HEX) 14 | ``` 15 | 16 | 除此之外,你还可以在构建实例时设置接收数据格式: 17 | 18 | ```kotlin 19 | //SerialPort.READ_HEX 十六进制 20 | //SerialPort.READ_STRING 字符串 21 | //不设置则默认字符串形式 22 | val serialPort = SerialPortBuilder 23 | .setReadDataType(SerialPort.READ_HEX) 24 | .build(this) 25 | ``` 26 | 27 | ### 设置发送数据格式 28 | 29 | ```kotlin 30 | //SerialPort.SEND_HEX 十六进制 31 | //SerialPort.SEND_STRING 字符串 32 | //不设置则默认字符串形式 33 | serialPort.setSendDataType(SerialPort.SEND_HEX ) 34 | ``` 35 | 36 | 除此之外,你还可以在构建实例时设置接收数据格式: 37 | 38 | ```kotlin 39 | //SerialPort.SEND_HEX 十六进制 40 | //SerialPort.SEND_STRING 字符串 41 | //不设置则默认字符串形式 42 | val serialPort = SerialPortBuilder 43 | .setSendDataType(SerialPort.SEND_HEX) 44 | .build(this) 45 | ``` 46 | 47 | 目前针对于BLE设备的数据收发暂不支持设置格式,仅支持字符串格式。如果实在需要十六进制的数据格式,暂时可以参考传统设备的处理方式自行实现。 48 | 49 | 参考代码链接:[HexStringToString](https://gitee.com/Shanya/SerialPortSample/blob/master/serialport/src/main/java/world/shanya/serialport/tools/SerialPortToolsByKotlin.kt#L112)、[StringToHex](https://gitee.com/Shanya/SerialPortSample/blob/master/serialport/src/main/java/world/shanya/serialport/tools/SerialPortToolsByKotlin.kt#L199) 50 | 51 | ## 接收消息 52 | 53 | ### 字符串 和 十六进制 54 | 55 | 使用方法 `setReceivedDataCallback(receivedDataCallback)` 设置一个接收消息监听器: 56 | 57 | ```kotlin 58 | serialPort.setReceivedDataCallback { data -> 59 | 60 | } 61 | ``` 62 | 63 | 除此之外,你还可以在构建实例时配置监听器: 64 | 65 | ```kotlin 66 | val serialPort = SerialPortBuilder 67 | .setReceivedDataCallback { data -> 68 | 69 | } 70 | .build(this) 71 | ``` 72 | 73 | ### 字节数组 74 | 75 | 在接收消息的时候,也可以选择获取**字节数组**,方法如下: 76 | 77 | ```kotlin 78 | serialPort.setReceivedBytesCallback { bytes -> 79 | 80 | } 81 | ``` 82 | 83 | 除此之外,你还可以在构建实例时配置监听器: 84 | 85 | ```kotlin 86 | val serialPort = SerialPortBuilder 87 | .setReceivedBytesCallback { bytes -> 88 | 89 | } 90 | .build(this) 91 | ``` 92 | 93 | ## 发送消息 94 | 95 | 使用方法 `sendData(data)` 发送消息: 96 | 97 | ### 字符串 98 | 99 | ```kotlin 100 | serialPort.sendData("Hello World") 101 | ``` 102 | 103 | ### 十六进制 104 | 105 | ```kotlin 106 | serialPort.sendData("0C FF") 107 | ``` 108 | 109 | 所有的十六进制应为**两位**,不足两位的前方补0,不区分大小写。 110 | 111 | ### BLE设备发送字节数组 112 | 目前BLE设备支持直接发送字节数组 113 | 114 | ```kotlin 115 | serialPort.sendData(bytes) 116 | ``` 117 | -------------------------------------------------------------------------------- /docs/zh_CN/tutorials/server.md: -------------------------------------------------------------------------------- 1 | # 服务端 2 | 3 | 现在,可以构建Android服务端,实现两台Android之间的蓝牙通信。(目前仅支持单设备连接) 4 | 5 | ## 构建实例 6 | 7 | ```kotlin 8 | val serialPortServer = SerialPortServerBuilder 9 | .setServerName("SerialPortServer") 10 | .setServerUUID("00001101-0000-1000-8000-00805F9B34FB") 11 | .setServerReceivedDataCallback { 12 | 13 | } 14 | .setServerConnectStatusCallback { status, bluetoothDevice -> 15 | 16 | } 17 | .build(this) 18 | ``` 19 | 20 | - `setServerName` 设置服务端名称 21 | - `setServerUUID` 设置服务端UUID,客户端连接时需设置传统设备的UUID与此相同 22 | - `setServerReceivedDataCallback` 服务端接收消息监听 23 | - `setServerConnectStatusCallback` 服务端连接状态监听 24 | - `status` 连接状态 25 | - `bluetoothDevice` 连接设备,当`status`为false则其为null 26 | 27 | ## 打开服务 28 | 29 | 只有打开服务后,客户端才可以连接到服务端。 30 | 31 | ```kotlin 32 | serialPortServer.openServer() 33 | ``` 34 | 35 | ## 关闭服务 36 | 37 | ```kotlin 38 | serialPortServer.closeServer() 39 | ``` 40 | 41 | ## 设置服务端可发现状态 42 | 43 | 默认打开服务则会自动设置为可发现,关闭服务则设置为不可见。 44 | 45 | ```kotlin 46 | setServerDiscoverable(status) 47 | ``` 48 | - `status` 为 `Boolean`类型,表示可发现状态 49 | 50 | ## 断开连接 51 | 52 | 主动断开与客户端的连接 53 | 54 | ```kotlin 55 | serialPortServer.disconnect() 56 | ``` 57 | 58 | ## 发送消息 59 | 60 | ```kotlin 61 | serialPortServer.sendData("Hello") 62 | ``` 63 | 64 | 65 | -------------------------------------------------------------------------------- /docs/zh_CN/tutorials/toast_java.md: -------------------------------------------------------------------------------- 1 | # Toast提示设置 2 | 3 | ## 配置方法 4 | 5 | ```java 6 | //是否显示 7 | SerialPortToast.INSTANCE.getConnectSucceeded().setStatus(true); 8 | //提示内容 content 是字符串id 9 | SerialPortToast.INSTANCE.getConnectSucceeded().setContent(R.string.connectSucceededToast); 10 | //显示时长 Toast.LENGTH_SHORT 或 Toast.LENGTH_LONG 11 | SerialPortToast.INSTANCE.getConnectSucceeded().setTime(Toast.LENGTH_SHORT); 12 | ``` 13 | 14 | ## 可选配置项 15 | 16 | | 项目 | 描述 | 默认值 | 17 | | :--------------------: | :------------------------------: | :---------------------------------------: | 18 | | connectSucceeded | 连接成功时 | 连接成功 | 19 | | connectFailed | 连接失败时 | 连接失败 | 20 | | disconnect | 断开连接时 | 断开连接 | 21 | | connectFirst | 未连接设备时执行发送数据 | 请先连接设备 | 22 | | disconnectFirst | 已连接设备后执行连接操作 | 请先断开连接 | 23 | | permission | 询问是否开启定位权限 | 请先开启位置权限 | 24 | | hexTip | 发送十六进制时,数据格式不对提示 | 请输入的十六进制数据保持两位,不足前面补0 | 25 | | openBluetoothSucceeded | 打开蓝牙成功时 | 蓝牙打开成功 | 26 | | openBluetoothFailed | 打开蓝牙失败时 | 蓝牙打开失败 | -------------------------------------------------------------------------------- /docs/zh_CN/tutorials/toast_kotlin.md: -------------------------------------------------------------------------------- 1 | # Toast提示设置 2 | 3 | ## 配置方法 4 | 5 | ```kotlin 6 | //是否显示 7 | SerialPortToast.connectSucceeded.status = true 8 | //提示内容 content 是字符串id 9 | SerialPortToast.connectSucceeded.content = R.string.connectSucceededToast 10 | //显示时长 Toast.LENGTH_SHORT 或 Toast.LENGTH_LONG 11 | SerialPortToast.connectSucceeded.time = Toast.LENGTH_SHORT 12 | ``` 13 | 14 | ## 可选配置项 15 | 16 | | 项目 | 描述 | 默认值 | 17 | | :--------------------: | :------------------------------: | :---------------------------------------: | 18 | | connectSucceeded | 连接成功时 | 连接成功 | 19 | | connectFailed | 连接失败时 | 连接失败 | 20 | | disconnect | 断开连接时 | 断开连接 | 21 | | connectFirst | 未连接设备时执行发送数据 | 请先连接设备 | 22 | | disconnectFirst | 已连接设备后执行连接操作 | 请先断开连接 | 23 | | permission | 询问是否开启定位权限 | 请先开启位置权限 | 24 | | hexTip | 发送十六进制时,数据格式不对提示 | 请输入的十六进制数据保持两位,不足前面补0 | 25 | | openBluetoothSucceeded | 打开蓝牙成功时 | 蓝牙打开成功 | 26 | | openBluetoothFailed | 打开蓝牙失败时 | 蓝牙打开失败 | -------------------------------------------------------------------------------- /docs/zh_CN/tutorials/tools_java.md: -------------------------------------------------------------------------------- 1 | # 工具 2 | 3 | ## 打印UUID及其属性 4 | 5 | 若我们不能知晓当前BLE设备的UUID可以调用函数`printPossibleBleUUID`来打印出当前连接设备的可选UUID 6 | 7 | 其中`Properties` 为二进制数,其每一位对应的意思见下表: 8 | 9 | | 数值(以十六进制表示) | 意思 | 10 | | :--------------------: | :-----------------------------: | 11 | | 0x01 | broadcastable | 12 | | 0x02 | readable | 13 | | 0x04 | can be written without response | 14 | | 0x08 | can be written | 15 | | 0x10 | supports notification | 16 | | 0x20 | supports indication | 17 | | 0x40 | supports write with signature | 18 | | 0x80 | has extended properties | 19 | 20 | 21 | 22 | ## 字符串转换成十六进制 23 | 24 | ```java 25 | /** 26 | * 字符串转换成十六进制 27 | * @param str 待转换的字符串 28 | * @return 十六进制数组 29 | */ 30 | DataUtil.INSTANCE.string2hex("Hello"); 31 | ``` 32 | 33 | ## 字节数组按要求的编码格式转换成字符串 34 | 35 | ```java 36 | /** 37 | * 字节数组按要求的编码格式转换成字符串 38 | * @param bytes 带转换的字节数组 39 | * @param charsetName 要求的编码格式 40 | * @return 转换成功的字符串 41 | */ 42 | SerialPortTools.bytes2string(bytes, "GBK"); 43 | ``` 44 | 45 | ## 字符串按要求的编码格式转换成字节数组 46 | 47 | ```java 48 | /** 49 | * 字符串按要求的编码格式转换成字节数组 50 | * @param string 带转换的字符串 51 | * @param charsetName 要求的编码格式 52 | * @return 转换成功的字节数组 53 | */ 54 | SerialPortTools.bytes2string("Hello", "GBK"); 55 | ``` 56 | -------------------------------------------------------------------------------- /docs/zh_CN/tutorials/tools_kotlin.md: -------------------------------------------------------------------------------- 1 | # 工具 2 | 3 | ## 打印UUID及其属性 4 | 5 | 若我们不能知晓当前BLE设备的UUID可以调用函数`printPossibleBleUUID`来打印出当前连接设备的可选UUID 6 | 7 | 其中`Properties` 为二进制数,其每一位对应的意思见下表: 8 | 9 | | 数值(以十六进制表示) | 意思 | 10 | | :--------------------: | :-----------------------------: | 11 | | 0x01 | broadcastable | 12 | | 0x02 | readable | 13 | | 0x04 | can be written without response | 14 | | 0x08 | can be written | 15 | | 0x10 | supports notification | 16 | | 0x20 | supports indication | 17 | | 0x40 | supports write with signature | 18 | | 0x80 | has extended properties | 19 | 20 | 21 | 22 | ## 字符串转换成十六进制 23 | 24 | ```kotlin 25 | /** 26 | * 字符串转换成十六进制 27 | * @param str 待转换的字符串 28 | * @return 十六进制数组 29 | */ 30 | DataUtil.string2hex("Hello") 31 | ``` 32 | 33 | ## 字节数组按要求的编码格式转换成字符串 34 | 35 | ```kotlin 36 | /** 37 | * 字节数组按要求的编码格式转换成字符串 38 | * @param bytes 带转换的字节数组 39 | * @param charsetName 要求的编码格式 40 | * @return 转换成功的字符串 41 | */ 42 | SerialPortTools.bytes2string(bytes, "GBK") 43 | ``` 44 | 45 | ## 字符串按要求的编码格式转换成字节数组 46 | 47 | ```kotlin 48 | /** 49 | * 字符串按要求的编码格式转换成字节数组 50 | * @param string 带转换的字符串 51 | * @param charsetName 要求的编码格式 52 | * @return 转换成功的字节数组 53 | */ 54 | SerialPortTools.bytes2string("Hello", "GBK") 55 | ``` 56 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/gradle.properties -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Mar 03 12:07:38 CST 2022 2 | distributionBase=GRADLE_USER_HOME 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /javademo/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /javademo/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | 3 | 4 | id 'com.android.application' 5 | } 6 | 7 | android { 8 | compileSdk 32 9 | 10 | defaultConfig { 11 | applicationId "cn.shanyaliux.javademo" 12 | minSdk 21 13 | targetSdk 32 14 | versionCode 1 15 | versionName "1.0" 16 | 17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 18 | } 19 | 20 | buildTypes { 21 | release { 22 | minifyEnabled false 23 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 24 | } 25 | } 26 | compileOptions { 27 | sourceCompatibility JavaVersion.VERSION_1_8 28 | targetCompatibility JavaVersion.VERSION_1_8 29 | } 30 | } 31 | 32 | dependencies { 33 | 34 | implementation 'androidx.appcompat:appcompat:1.4.1' 35 | implementation 'com.google.android.material:material:1.6.0' 36 | implementation 'androidx.constraintlayout:constraintlayout:2.1.4' 37 | testImplementation 'junit:junit:4.13.2' 38 | androidTestImplementation 'androidx.test.ext:junit:1.1.3' 39 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' 40 | 41 | implementation 'cn.shanyaliux.serialport:serialport:4.2.0' 42 | } -------------------------------------------------------------------------------- /javademo/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /javademo/src/androidTest/java/cn/shanyaliux/javademo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package cn.shanyaliux.javademo; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.platform.app.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 24 | assertEquals("cn.shanyaliux.javademo", appContext.getPackageName()); 25 | } 26 | } -------------------------------------------------------------------------------- /javademo/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /javademo/src/main/java/cn/shanyaliux/javademo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package cn.shanyaliux.javademo; 2 | 3 | import androidx.appcompat.app.AppCompatActivity; 4 | 5 | import android.annotation.SuppressLint; 6 | import android.bluetooth.BluetoothDevice; 7 | import android.os.Bundle; 8 | import android.widget.Button; 9 | import android.widget.EditText; 10 | import android.widget.TextView; 11 | 12 | import kotlin.Unit; 13 | import kotlin.jvm.functions.Function1; 14 | import kotlin.jvm.functions.Function2; 15 | import world.shanya.serialport.SerialPort; 16 | import world.shanya.serialport.SerialPortBuilder; 17 | 18 | @SuppressLint("MissingPermission") 19 | public class MainActivity extends AppCompatActivity { 20 | 21 | @Override 22 | protected void onCreate(Bundle savedInstanceState) { 23 | super.onCreate(savedInstanceState); 24 | setContentView(R.layout.activity_main); 25 | 26 | StringBuilder stringBuilder = new StringBuilder(); 27 | TextView textViewReceived = findViewById(R.id.textViewReceiced); 28 | TextView textViewConnectInfo = findViewById(R.id.textViewConnectInfo); 29 | Button buttonConnect = findViewById(R.id.buttonConnect); 30 | Button buttonDisconnect = findViewById(R.id.buttonDisconnect); 31 | EditText editTextSendData = findViewById(R.id.editTextTextSend); 32 | Button buttonSend = findViewById(R.id.buttonSend); 33 | 34 | SerialPort serialPort = SerialPortBuilder.INSTANCE 35 | .setReceivedDataCallback((Function1) s -> { 36 | runOnUiThread(() -> { 37 | stringBuilder.append(s); 38 | textViewReceived.setText(stringBuilder.toString()); 39 | }); 40 | return null; 41 | }) 42 | .setConnectionStatusCallback((aBoolean, bluetoothDevice) -> { 43 | runOnUiThread(() -> { 44 | String info; 45 | if (aBoolean) { 46 | info = "设备名称:\t" + bluetoothDevice.getName() + 47 | "\n设备地址:\t" + bluetoothDevice.getAddress() + 48 | "\n 设备类型:\t" + bluetoothDevice.getType(); 49 | 50 | }else{ 51 | info = ""; 52 | } 53 | textViewConnectInfo.setText(info); 54 | }); 55 | return null; 56 | }) 57 | .build(this); 58 | 59 | buttonConnect.setOnClickListener((v) -> serialPort.openDiscoveryActivity()); 60 | 61 | buttonDisconnect.setOnClickListener((v)-> serialPort.disconnect()); 62 | 63 | buttonSend.setOnClickListener((v-> serialPort.sendData(editTextSendData.getText().toString()))); 64 | 65 | } 66 | } -------------------------------------------------------------------------------- /javademo/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /javademo/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /javademo/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /javademo/src/main/res/mipmap-hdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/javademo/src/main/res/mipmap-hdpi/ic_launcher.webp -------------------------------------------------------------------------------- /javademo/src/main/res/mipmap-hdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/javademo/src/main/res/mipmap-hdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /javademo/src/main/res/mipmap-mdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/javademo/src/main/res/mipmap-mdpi/ic_launcher.webp -------------------------------------------------------------------------------- /javademo/src/main/res/mipmap-mdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/javademo/src/main/res/mipmap-mdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /javademo/src/main/res/mipmap-xhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/javademo/src/main/res/mipmap-xhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /javademo/src/main/res/mipmap-xhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/javademo/src/main/res/mipmap-xhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /javademo/src/main/res/mipmap-xxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/javademo/src/main/res/mipmap-xxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /javademo/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/javademo/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /javademo/src/main/res/mipmap-xxxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/javademo/src/main/res/mipmap-xxxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /javademo/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shanyaliux/SerialPortSample/384513f6676c2f0bba64a2410d4019335477a5db/javademo/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /javademo/src/main/res/values-night/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16 | -------------------------------------------------------------------------------- /javademo/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFBB86FC 4 | #FF6200EE 5 | #FF3700B3 6 | #FF03DAC5 7 | #FF018786 8 | #FF000000 9 | #FFFFFFFF 10 | -------------------------------------------------------------------------------- /javademo/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | JavaDemo 3 | -------------------------------------------------------------------------------- /javademo/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16 | -------------------------------------------------------------------------------- /javademo/src/test/java/cn/shanyaliux/javademo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package cn.shanyaliux.javademo; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /kotlindemo/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /kotlindemo/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | 3 | 4 | id 'com.android.application' 5 | id 'org.jetbrains.kotlin.android' 6 | } 7 | 8 | android { 9 | compileSdk 32 10 | 11 | defaultConfig { 12 | applicationId "cn.shanyaliux.kotlindemo" 13 | minSdk 21 14 | targetSdk 32 15 | versionCode 1 16 | versionName "1.0" 17 | 18 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 19 | } 20 | 21 | buildTypes { 22 | release { 23 | minifyEnabled false 24 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 25 | } 26 | } 27 | compileOptions { 28 | sourceCompatibility JavaVersion.VERSION_1_8 29 | targetCompatibility JavaVersion.VERSION_1_8 30 | } 31 | kotlinOptions { 32 | jvmTarget = '1.8' 33 | } 34 | } 35 | 36 | dependencies { 37 | 38 | implementation 'androidx.core:core-ktx:1.7.0' 39 | implementation 'androidx.appcompat:appcompat:1.4.1' 40 | implementation 'com.google.android.material:material:1.6.0' 41 | implementation 'androidx.constraintlayout:constraintlayout:2.1.4' 42 | testImplementation 'junit:junit:4.13.2' 43 | androidTestImplementation 'androidx.test.ext:junit:1.1.3' 44 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' 45 | 46 | implementation 'cn.shanyaliux.serialport:serialport:4.2.0' 47 | } -------------------------------------------------------------------------------- /kotlindemo/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /kotlindemo/src/androidTest/java/cn/shanyaliux/kotlindemo/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package cn.shanyaliux.kotlindemo 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("cn.shanyaliux.kotlindemo", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /kotlindemo/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /kotlindemo/src/main/java/cn/shanyaliux/kotlindemo/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package cn.shanyaliux.kotlindemo 2 | 3 | import android.annotation.SuppressLint 4 | import androidx.appcompat.app.AppCompatActivity 5 | import android.os.Bundle 6 | import android.widget.Button 7 | import android.widget.EditText 8 | import android.widget.TextView 9 | import kotlinx.coroutines.MainScope 10 | import kotlinx.coroutines.launch 11 | import org.w3c.dom.Text 12 | import world.shanya.serialport.SerialPortBuilder 13 | 14 | class MainActivity : AppCompatActivity() { 15 | @SuppressLint("MissingPermission", "SetTextI18n") 16 | override fun onCreate(savedInstanceState: Bundle?) { 17 | super.onCreate(savedInstanceState) 18 | setContentView(R.layout.activity_main) 19 | 20 | val stringBuilder = StringBuilder() 21 | val textViewReceived = findViewById(R.id.textViewReceiced) 22 | val textViewConnectInfo = findViewById(R.id.textViewConnectInfo) 23 | val buttonConnect = findViewById