更新记录
1.0.0(2026-11-25)
- 首发:四端原生混编 BLE 实现(Android Kotlin / iOS CoreBluetooth / 鸿蒙 ArkTS / 微信小程序 wx API)
- 扫描:时长、空名称、名称关键字、名称前缀数组、服务 UUID 过滤,实时广播/RSSI 监听
- 连接:连接/断开/自动重连(可配间隔)/连接超时,iOS 已连接设备查询与接管
- 服务:服务与特征值发现,自动识别可通知/可写 UUID 组合
- 通信:Notify/Indicate 订阅、读取、写入(ArrayBuffer/数字数组/十六进制)、writeType、自动分包、MTU、RSSI
- 对齐主流 BLE 插件全量 API,含历史拼写别名与旧接口兼容
平台兼容性
uni-app(4.07)
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|
| √ | √ | - | - | - | - | √ | √ | √ |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 小红书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| √ | - | - | - | - | - | - | - | - | - | - | - |
uni-app x(4.07)
| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 | 支付宝小程序 |
|---|---|---|---|---|---|---|
| - | - | √ | √ | √ | √ | - |
其他
| 多语言 | 暗黑模式 | 宽屏模式 | 蒸汽模式 |
|---|---|---|---|
| × | × | × | √ |
qf-ble BLE 低功耗蓝牙引擎
Android / iOS / 鸿蒙 / 微信小程序四端 BLE 插件,完整对齐 DCloud 插件市场主流 BLE 插件的 API 与能力,
采用 原生混编 架构:.kt / .swift / .ets 负责原生能力,UTS 层只做薄封装,
小程序端用 wx 原生蓝牙 API 实现(utssdk/mp-weixin/index.ts),
兼容 uni-app(Vue2/Vue3/nvue)与 uni-app x 蒸汽模式(uvue)。
✅ 四端(Android / iOS / 鸿蒙 / 微信小程序)原生实现已完成,并在 Android / iOS / 鸿蒙三端通过编译与真机验证,开箱即用。
特性
完整能力(对齐第三方)
- 扫描:启动/停止扫描、扫描时长、空名称过滤、名称关键字过滤、名称前缀数组过滤(
filterNames/fliterNames)、服务 UUID 过滤(iosFilterUUIDs) - 实时广播监听:
onScanDataListener实时回调广播数据与 RSSI 变化 - 连接:连接/断开/单设备关闭、连接超时(
setBtConnectTimeout)、自动重连(可配间隔)、多设备连接管理 - 发现服务:扫描服务与特征值、自动解析可通知/可写 UUID 组合、切换 UUID
- 数据通信:订阅通知(Notify)/ 指示(Indicate)、读取数据、写数据(ArrayBuffer / 数字数组 / 十六进制)、写入类型(
writeType:0=WRITE / 1=WRITE_NO_RESPONSE)、自动分包、MTU 设置 - RSSI:读取信号强度
- 广播数据:扫描结果含
advertData(十六进制广播数据) - 平台增强:iOS 查询系统已连接设备、iOS 定向重连、iOS 连接弹窗开关;Android 2M PHY 预留;鸿蒙 MTU 调整
- 微信小程序:完整实现扫描/连接/服务/通知/读写/分包/MTU/RSSI/多实例多连接
架构优势
- 原生混编:BLE 状态机、Android GATT 回调、iOS CBCentralManager delegate、鸿蒙 BLE 异步 Promise 全部在原生层完成,避开 UTS 直调复杂 API 的类型坑
- 统一返回结构:所有异步接口统一回调
BleResult { type, message, data },与qf-keeplive风格一致 - 多端一致 API:三端方法名与参数完全一致,业务层无需条件编译
- 可配对保活:与
qf-keeplive配套使用,保活里注册蓝牙业务适配器即可实现后台断线重连
目录结构
uni_modules/qf-ble/
├── package.json
├── readme.md
├── changelog.md
└── utssdk/
├── interface.uts # 公共类型(三端共享)
├── app-android/
│ ├── AndroidManifest.xml # 蓝牙权限(Android 12+ 与旧版)
│ ├── BleNative.kt # Kotlin 原生 BLE 实现
│ └── index.uts # UTS 薄封装
├── app-ios/
│ ├── Info.plist # 蓝牙权限描述
│ ├── BleNative.swift # Swift/CoreBluetooth 原生实现
│ └── index.uts # UTS 薄封装
└── app-harmony/
├── module.json5 # 鸿蒙权限声明
├── resources/base/element/string.json
├── BleNative.ets # ArkTS 原生实现
└── index.uts # UTS 薄封装
快速开始
1. 引入
// #ifdef APP
import { BleLib } from '@/uni_modules/qf-ble'
const ble = new BleLib()
ble.setDebugMode(true)
// #endif
2. 初始化与扫描
// #ifdef APP
ble.openBtBluetooth(res => {
if (res.type !== 0) {
console.warn('蓝牙不可用', res.message)
return
}
ble.onStartScanBle({
scantime: 10000,
showEmptyName: 0,
btNameFilter: 'MY_DEVICE',
onScanResult: res => {
if (res.type === 0) {
const dev = res.data // { address, name, rssi }
console.log('发现设备', dev)
}
},
scanComplate: () => console.log('扫描完成')
})
})
// #endif
3. 连接与监听
// #ifdef APP
ble.connect(dev.address, true, res => {
console.log('连接状态', res.type, res.message)
if (res.type === 0 && res.message === 'connected') {
ble.scanServices(svcRes => {
console.log('服务列表', svcRes.data)
const uuids = ble.getUUIDs()
if (uuids.length > 0) {
const u = uuids[0]
// 订阅通知
ble.onNotityBleData(u.serviceId, u.notifyUuid, true, nres => {
console.log('收到数据', nres.data) // { data: number[], mac, ... }
})
// 写数据(十六进制)
ble.sendData({
serviceId: u.serviceId,
characteristicId: u.writeUuid,
hexStrData: 'A1B2C3',
fenbao: true
}, wres => console.log('写入结果', wres))
}
})
}
})
// #endif
4. iOS 专属能力
// #ifdef APP-IOS
// 查询系统已连接的设备(按服务 UUID)
const res = ble.getConnectedDevicesWithServices(['FFF0'])
console.log(res.data)
// 定向重连指定设备
ble.redictToConnect(mac, r => console.log(r))
// #endif
API 总览
| 分类 | 方法 |
|---|---|
| 状态 | isEnabled() openBtBluetooth(cb) openBluetooth(on) openBluetoothSettings() onBtOpenStateListener(cb) setDebugMode(on) |
| 扫描 | onStartScanBle(options) startScanBleDevice(time,cb) stopScanBle() onScanDataListener(cb) |
| 连接 | connect(mac,autoConnect,cb) close() closeDevice(mac) isConnected() getConnectMac() setBtConnectTimeout(ms) setBtAutoConnectTime(ms) cancelAutoConnectBt() |
| 服务 | scanServices(cb) getUUIDs() setSelectUUID(index) getServiceUUID() getNotityUUID() getWriteUUID() |
| 通信 | onNotityBleData(service,ch,enabled,cb) onIndicate(service,ch,cb) onReadData(service,ch,cb) sendData(options,cb) readRssi(cb) setMtu(mtu,cb) setPhy2MMode() |
| iOS | getConnectedDevicesWithServices(uuids) redictToConnect(mac,cb) setIOSConnectNotifyAlertEnabled(on) |
状态码(BleResult.type)
| 值 | 含义 |
|---|---|
| 0 | 成功 |
| 1000 | 订阅成功 |
| 10000 | 通用失败 |
| 10001 | 异常断开 |
| 10002 | 蓝牙未开启/未就绪 |
| 10003 | 无权限 |
| 10004 | 已连接 |
| 10005 | 非法地址 |
为兼容第三方插件的历史拼写,保留别名:
getSericUUID()/getwriteUUID()。
关于 res.data 的类型
原生层回传的 data 经 JSON 桥接后在 UTS 侧是 UTSJSONObject(不是 BleDevice / NotifyData 等 type 的实例),
不能直接 as DeviceInfo(type 在 Kotlin 下编译为具体类,会抛 ClassCastException)。请用下标取值:
const obj = res.data as UTSJSONObject
const address = obj['address'] as string
const rssi = obj['rssi'] as number
getUUIDs() 返回的是真正的 BleUuid[],可以直接访问 .serviceId 等属性。
已知限制
- 当前设备模型:插件沿用同类插件的"当前选中设备"模型——
connect()后,读写/通知/服务查询都作用于最后连接的设备;closeDevice(mac)可断开指定设备。若需要真正的多设备并发通信,需为每个设备维护独立的BleLib实例(后续版本会做按地址隔离的状态管理)。 - 空实现接口:
setBtConnectTimeout()、setPhy2MMode()、setThreadTypeName()为预留接口,暂时无实际效果。 - iOS:不支持代码开关系统蓝牙;MTU 由系统决定,
setMtu()返回提示但不改变实际值。 - 鸿蒙:
readRssi()暂返回"不支持"提示(鸿蒙 GATT 客户端无对应 API);openBluetoothSettings()需业务层用 uni API 跳转。 onScanDataListener()为兼容旧 API 的保留接口,扫描结果统一由onStartScanBle的onScanResult回调返回。- 写数据统一在 UTS 层转换为十六进制字符串再传给原生层,
buffer/data/hexStrData三种入参均支持。
权限与上架
Android
插件 AndroidManifest.xml 已声明全部权限。Android 12+ 需运行时申请
BLUETOOTH_SCAN / BLUETOOTH_CONNECT / BLUETOOTH_ADVERTISE;Android 11 及以下需 ACCESS_FINE_LOCATION。
业务层可用 uni.authorize 或 UTSAndroid.checkSystemPermissionGranted 申请。
iOS
插件 Info.plist 已声明 NSBluetoothAlwaysUsageDescription / NSBluetoothPeripheralUsageDescription,
云打包时会自动合并进主工程。如主工程已有同名键,以主工程为准。
鸿蒙
插件 module.json5 已声明 APPROXIMATELY_LOCATION / LOCATION 权限。
扫描类权限需在设置中引导用户开启定位(系统要求)。
与保活插件配套
在 qf-keeplive 中注册蓝牙业务适配器,即可在后台持续监听设备断开并自动重连:
import { createBleBusiness } from '@/uni_modules/qf-keeplive/business/index.ts'
keep.registerBusiness(createBleBusiness({ ble, mac: targetMac }))

收藏人数:
购买源码授权版(
试用
使用 HBuilder 导入示例项目
赞赏(0)
下载 0
赞赏 0
下载 12787884
赞赏 1960
赞赏
京公网安备:11010802035340号