更新记录
1.0.2(2026-11-13)
更新适应范围
1.0.1(2026-11-13)
更新使用说明
1.0.0(2026-11-13)
初始化
查看更多平台兼容性
uni-app x(4.81)
| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|
| × | × | 6.0 | √ | √ | - |
其他
| 多语言 | 暗黑模式 | 宽屏模式 | 蒸汽模式 |
|---|---|---|---|
| √ | √ | √ | √ |
yk-ble 使用说明
三端 BLE 蓝牙 UTS 插件(Android / iOS / HarmonyOS NEXT),源码混编,支持扫描、连接、服务/特征发现、读写、notify/indicate 订阅、MTU 协商与批量写入。
| 平台 | 支持 | 说明 |
|---|---|---|
| App-Android | ✅ | minSdk 23 |
| App-iOS | ✅ | 需 Mac + Xcode 打包 |
| App-Harmony | ✅ | HarmonyOS NEXT,DevEco 本地打包 |
| H5 / 小程序 | ❌ | 自动兜底,返回 unsupported,不会报错 |
要求:HBuilderX ^4.66,uni-app x ^4.81。
1. 安装
把 uni_modules/yk-ble 整个目录放到你工程的 uni_modules/ 下,保持目录名不变,然后重新「运行到手机」。
import * as POC from '@/uni_modules/yk-ble'
import 路径以
uni_modules下的实际目录名为准。
2. 权限配置
2.1 Android
插件已自带 utssdk/app-android/AndroidManifest.xml,声明了:
- Android 12+:
BLUETOOTH_SCAN(neverForLocation)/BLUETOOTH_CONNECT - Android 11 及以下:
BLUETOOTH/BLUETOOTH_ADMIN/ 定位权限
注意:标准基座不会合并插件内的 Manifest 权限。若真机扫描无反应,在主工程 manifest.json 的 app-plus.distribute.android.permissions 里补上同样权限,或制作一次自定义调试基座。运行时权限由插件自动申请。
2.2 iOS
插件已自带 utssdk/app-ios/Info.plist(NSBluetoothAlwaysUsageDescription)与 CoreBluetooth.framework 引用,正常自定义基座/正式打包会合并,无需额外配置。
2.3 HarmonyOS
权限必须写在主工程 harmony-configs/entry/src/main/module.json5(HBuilderX 每次运行都会重新生成鸿蒙工程,插件内的 module.json5 不会进最终 entry):
"requestPermissions": [
{
"name": "ohos.permission.INTERNET"
},
{
"name": "ohos.permission.ACCESS_BLUETOOTH",
"reason": "$string:bluetooth_reason",
"usedScene": {
"abilities": ["EntryAbility"],
"when": "inuse"
}
}
]
并在 resources/base/element/string.json 提供 reason 文案:
{ "name": "bluetooth_reason", "value": "用于扫描和连接蓝牙打印机、电子秤" }
要点:
- 只需
ACCESS_BLUETOOTH,不要加DISCOVER_BLUETOOTH(受限权限)和定位权限(鸿蒙 BLE 扫描不需要)。 ACCESS_BLUETOOTH是运行时权限,插件会在openBluetoothAdapter时自动弹窗申请;也可手动调requestBluetoothPermission命令。- 部分机型要求系统「位置信息」开关处于打开状态(属于系统开关,不是权限)。
3. 快速开始
import * as POC from '@/uni_modules/yk-ble'
// 1. 注册事件(设备发现、连接状态、数据上报)
POC.onEvent((json : string) => {
const ev = JSON.parse(json)
if (ev.type === 'deviceFound') {
console.log('发现设备', ev.devices)
} else if (ev.type === 'characteristicValue') {
console.log('收到数据', ev.hex, '来源', ev.source)
}
})
// 2. 初始化并扫描
POC.call('openBluetoothAdapter', '{}', (json : string) => {
const r = JSON.parse(json)
if (!r.ok) { console.log('初始化失败', r.errMsg); return }
POC.call('startBluetoothDevicesDiscovery', '{"services":[]}', (json : string) => {})
})
// 3. 查询插件信息(同步)
console.log(POC.getInfo()) // {"platform":"android|ios|harmony","version":"...","native":true}
// 4. 页面销毁时解注册
// POC.offEvent()
典型调用顺序:
openBluetoothAdapter
→ startBluetoothDevicesDiscovery (onEvent 收 deviceFound)
→ stopBluetoothDevicesDiscovery
→ createBLEConnection (onEvent 收 connectionState)
→ getBLEDeviceServices
→ getBLEDeviceCharacteristics
→ notifyBLECharacteristicValueChange / readBLECharacteristicValue / writeBLECharacteristicValue
→ closeBLEConnection → closeBluetoothAdapter
4. API
插件只导出 4 个方法,参数与结果全部是 JSON 字符串。
| 方法 | 说明 |
|---|---|
call(action, params, callback) |
执行一条命令,callback 只回调一次结果 |
onEvent(callback) |
注册持续事件监听 |
offEvent() |
解注册事件监听 |
getInfo() |
同步返回插件信息 JSON |
结果格式:
{ "ok": true, ...附加字段 }
{ "ok": false, "errCode": 10001, "errMsg": "..." }
事件格式:
{ "type": "deviceFound", ...事件字段 }
4.1 命令表
params 为 JSON 字符串,无参数传 "{}"。UUID 支持 4 / 8 / 36 位写法。
| action | params | 结果附加字段 |
|---|---|---|
openBluetoothAdapter |
{} |
鸿蒙另带 granted / bluetoothOn / locationEnabled |
closeBluetoothAdapter |
{} |
— |
getBluetoothAdapterState |
{} |
available, discovering(同步返回) |
setBluetoothEnable |
{enable:boolean} |
iOS / 鸿蒙返回不支持提示 |
startBluetoothDevicesDiscovery |
{services:string[], allowDuplicatesKey, powerLevel} |
— |
stopBluetoothDevicesDiscovery |
{} |
— |
getBluetoothDevices |
{} |
devices[](deviceId / name / localName / RSSI) |
createBLEConnection |
{deviceId, timeout} |
Android 连上才回调 ok 并带 connected:true;iOS / 鸿蒙先回已发起,由事件确认 |
closeBLEConnection |
{deviceId} |
— |
getBLEDeviceServices |
{deviceId} |
services[](uuid / isPrimary) |
getBLEDeviceCharacteristics |
{deviceId, serviceId} |
characteristics[](uuid / properties) |
setBLEMTU |
{mtu} |
mtu(异步结果走 mtuChange 事件) |
getBLEMTU |
{} |
mtu |
readBLECharacteristicValue |
{deviceId, serviceId, characteristicId} |
数据走 characteristicValue 事件 |
writeBLECharacteristicValue |
{deviceId, serviceId, characteristicId, hex, writeType} |
written |
batchWriteBLECharacteristicValue |
{..., chunks:hex[], interval, writeType} |
written |
notifyBLECharacteristicValueChange |
{deviceId, serviceId, characteristicId, state} |
— |
requestBluetoothPermission |
{} |
仅鸿蒙,手动触发运行时授权 |
4.2 事件表
| 事件 | 触发时机 | 主要字段 |
|---|---|---|
deviceFound |
扫描到设备 | devices[](deviceId / name / localName / RSSI) |
connectionState |
连接成功或断开 | deviceId, connected, status(断开为 10006) |
characteristicValue |
收到 notify / indicate / read 数据 | characteristicId, hex, source, status |
notifyState |
订阅(CCCD)结果 | status, characteristicId |
mtuChange |
MTU 协商完成 | mtu, status |
servicesDiscovered |
服务树就绪 | count |
error |
原生层异常 | errCode, errMsg |
unsupported |
不支持的平台 | message |
另有 permission / scanDiagnostics / scanStarted / scanFailed / serviceDiscovery 等诊断事件,仅用于排障,业务可忽略。
4.3 错误码表
| errCode | 含义 |
|---|---|
0 |
成功 |
-1 |
未知命令 / 平台不支持 |
10000 |
未初始化(未先调 openBluetoothAdapter) |
10001 |
蓝牙未开启 / 扫描启动失败 |
10002 |
蓝牙权限未授予 |
10003 |
连接发起失败 |
10004 |
服务不存在 / 服务发现失败 |
10005 |
特征不存在 |
10006 |
未连接 / 连接已断开 |
10007 |
读 / 订阅失败 |
10008 |
写入失败 / 上一个写任务未完成 |
10012 |
连接超时 |
10013 |
deviceId 为空 / 设备未找到 |
errMsg 中可能附带系统错误码:鸿蒙 201(权限)、401(参数非法)、2900099(GATT 忙)、2901008(服务树为空);Android GATT 133 / 8 / 22。
5. 数据格式约定
- 写数据用 hex 字符串(大写、无分隔符):
1B40=ESC @。 writeType:write(默认,有响应)/writeNoResponse(无响应,部分透传模块只支持这种)。batchWrite的chunks是已分好包的 hex 数组,原生层串行发送,每包间隔interval毫秒。characteristicValue事件的source标识数据来源:read/notify。- 所有 UUID 支持短写法,如
FFE0、0000ffe0、0000ffe0-0000-1000-8000-00805f9b34fb均可。
6. 使用注意
- 只支持 BLE,不支持 SPP 经典蓝牙(部分老款打印机是 SPP,搜不到属正常)。
- GATT 操作不要并行:连接、发现服务、读、写、订阅一次只做一件,尤其鸿蒙。
- MTU 协商放在服务发现之后,并按 20 字节分包更稳妥(部分模块协商后仍只收 20 字节)。
- 改过
.kt/.swift/.ets后,必须把package.json的version加 1,否则 HBuilderX 跳过原生编译,真机跑的是旧代码。 - 页面/应用销毁时调
closeBLEConnection、closeBluetoothAdapter并offEvent()。

收藏人数:
购买普通授权版(
试用
赞赏(0)
下载 0
赞赏 0
下载 12763894
赞赏 1959
赞赏
京公网安备:11010802035340号