更新记录

1.0.1(2026-04-30) 下载此版本

已弃坑UTS,为了方便技术研究,遂决定开源,不再积极维护

1.0.0(2026-04-19) 下载此版本

新增

  • 完整的 BLE 功能实现
  • 7 大功能模块(状态管理、扫描、连接、GATT、数据转换、事件系统、高级功能)
  • 40+ API 接口
  • 10 种事件类型
  • 自动权限适配(Android 12+)
  • 多设备连接支持
  • 自动重连机制
  • MTU 协商
  • 配对/绑定管理
  • 数据转换工具
  • CRC16 校验

平台兼容性

uni-app(4.41)

Vue2 Vue3 Vue3插件版本 Chrome Safari app-vue app-nvue Android Android插件版本 iOS 鸿蒙
- 1.0.0 - - - - 5.0 1.0.0 × ×
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 小红书小程序 快应用-华为 快应用-联盟
- - - - - - - - - - - -

uni-app x(4.85)

Chrome Safari Android Android插件版本 iOS 鸿蒙 微信小程序
- - 5.0 1.0.0 - - -

Changbi BLE 插件

一个功能全面、易用稳定的 UniApp 低功耗蓝牙(BLE)通信 UTS 插件。

特性

  • ✅ 完整的 BLE 功能支持(扫描、连接、读写、通知等)
  • ✅ 自动权限适配(Android 12+ 新权限体系)
  • ✅ 多设备同时连接
  • ✅ 自动重连机制
  • ✅ MTU 协商(23-517 字节)
  • ✅ 配对/绑定管理
  • ✅ RSSI 信号强度监测
  • ✅ 完善的事件系统
  • ✅ 数据转换工具(Hex、字符串、CRC16)
  • ✅ 详细的错误处理

平台支持

  • ✅ Android API 21+ (Android 5.0+)
  • ❌ iOS(暂不支持)
  • ❌ HarmonyOS(暂不支持)

安装

将插件复制到项目的 uni_modules 目录下。

快速开始

import * as BLE from '@/uni_modules/changbi-ble'

// 1. 初始化蓝牙适配器
await BLE.openBluetoothAdapter({ debug: true })

// 2. 请求权限
const granted = await BLE.requestBluetoothPermissions()
if (!granted) {
  console.log('权限被拒绝')
  return
}

// 3. 监听设备发现事件
BLE.on('deviceFound', (data) => {
  console.log('发现设备:', data.device)
})

// 4. 开始扫描
await BLE.startBluetoothDevicesDiscovery({
  scanMode: 'balanced',
  timeout: 10000
})

// 5. 连接设备
await BLE.createBLEConnection({
  deviceId: 'AA:BB:CC:DD:EE:FF',
  timeout: 10000,
  autoReconnect: true
})

// 6. 发现服务
const services = await BLE.getBLEDeviceServices('AA:BB:CC:DD:EE:FF')
console.log('服务列表:', services)

// 7. 启用通知
await BLE.notifyBLECharacteristicValueChange({
  deviceId: 'AA:BB:CC:DD:EE:FF',
  serviceId: '0000180d-0000-1000-8000-00805f9b34fb',
  characteristicId: '00002a37-0000-1000-8000-00805f9b34fb',
  state: true
})

// 8. 监听特征值变化
BLE.on('characteristicValueChange', (data) => {
  console.log('收到数据:', data.value)
})

// 9. 写入数据
await BLE.writeBLECharacteristicValue({
  deviceId: 'AA:BB:CC:DD:EE:FF',
  serviceId: '0000180d-0000-1000-8000-00805f9b34fb',
  characteristicId: '00002a39-0000-1000-8000-00805f9b34fb',
  value: '48656C6C6F', // "Hello" 的 Hex
  writeType: 'write'
})

核心功能

1. 状态与权限管理

  • openBluetoothAdapter() - 初始化蓝牙适配器
  • closeBluetoothAdapter() - 关闭蓝牙适配器
  • getBluetoothAdapterState() - 获取适配器状态
  • isBLESupported() - 检查 BLE 支持
  • requestBluetoothPermissions() - 请求蓝牙权限
  • checkBluetoothPermissions() - 检查权限状态
  • openBluetoothSettings() - 打开蓝牙设置
  • openLocationSettings() - 打开位置设置

2. 扫描与发现

  • startBluetoothDevicesDiscovery() - 开始扫描
  • stopBluetoothDevicesDiscovery() - 停止扫描
  • getBluetoothDevices() - 获取已发现设备列表

3. 连接与配对

  • createBLEConnection() - 连接设备
  • closeBLEConnection() - 断开连接
  • isDeviceConnected() - 检查连接状态
  • getConnectedBluetoothDevices() - 获取已连接设备
  • createBLEBond() - 创建配对
  • removeBLEBond() - 移除配对
  • getBondState() - 获取配对状态
  • getBondedDevices() - 获取已配对设备

4. GATT 数据交互

  • getBLEDeviceServices() - 获取服务列表
  • readBLECharacteristicValue() - 读取特征值
  • writeBLECharacteristicValue() - 写入特征值
  • notifyBLECharacteristicValueChange() - 启用/禁用通知
  • readBLEDescriptorValue() - 读取描述符
  • writeBLEDescriptorValue() - 写入描述符
  • setBLEMTU() - 请求 MTU
  • getBLEMTU() - 获取当前 MTU

5. 数据处理与转换

  • hexToBytes() - Hex 转字节数组
  • bytesToHex() - 字节数组转 Hex
  • stringToHex() - 字符串转 Hex
  • hexToString() - Hex 转字符串
  • calculateCRC16() - 计算 CRC16 校验值

6. 事件监听系统

  • on() - 注册事件监听器
  • off() - 移除事件监听器
  • offAll() - 移除所有监听器

支持的事件:

  • adapterStateChange - 适配器状态变化
  • deviceFound - 发现新设备
  • connectionStateChange - 连接状态变化
  • characteristicValueChange - 特征值变化
  • mtuChange - MTU 变化
  • bondStateChange - 配对状态变化
  • rssiChange - RSSI 变化
  • scanStart - 扫描开始
  • scanStop - 扫描停止
  • error - 错误事件

7. 高级功能

  • readBLERSSI() - 读取 RSSI
  • setConnectionPriority() - 设置连接优先级
  • getDeviceName() - 获取设备名称
  • getConnectionParameters() - 获取连接参数
  • clearDeviceCache() - 清除设备缓存

注意事项

  1. Android 12+ 需要新的蓝牙权限(BLUETOOTH_SCAN、BLUETOOTH_CONNECT)
  2. 扫描需要位置权限(ACCESS_FINE_LOCATION)
  3. 所有蓝牙数据传输使用 Hex 字符串格式
  4. 建议在使用前先检查权限和蓝牙状态
  5. 连接失败时可以尝试清除设备缓存后重试

完整文档

详细的 API 文档请查看 API.md

许可证

MIT License

隐私、权限声明

1. 本插件需要申请的系统权限列表:

android.permission.BLUETOOTH,android.permission.BLUETOOTH_ADMIN,android.permission.BLUETOOTH_SCAN,android.permission.BLUETOOTH_CONNECT,android.permission.ACCESS_FINE_LOCATION

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT License

Copyright (c) 2024 Changbi BLE Plugin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.