更新记录

1.0.0(2026-05-22)

初始化版本发布。


平台兼容性

uni-app(5.0)

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

uni-app x(5.0)

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

XF-bleUTS

XF-bleUTS 是面向 uni-app x 的 BLE 4.0 UTS 插件,并在 Android、iOS、Harmony 三端提供同名 API。插件只处理 BLE Central 侧常见流程:初始化、扫描、获取设备、连接、发现服务/特征/描述符、读写、notify 监听、缓存 simple notify 数据和清理本地缓存。

快速调用流程

  1. 运行前确认静态权限已配置,并在页面中完成动态权限申请。
  2. 调用 initManager,确认返回 state: 'poweredOn'
  3. 调用 scan 开始扫描,再轮询或手动调用 getPeripheral 获取设备列表。
  4. 选中 peripheralUUID 后调用 connect
  5. 调用 discoverService,再调用 discoverCharacteristics,需要描述符时再调用 discoverDescriptorsForCharacteristic
  6. 根据特征属性调用 readValueForCharacteristicwriteValueForCharacteristicsetNotifysetSimpleNotify
  7. 不再使用时调用 stopNotifydisconnectstopScan,必要时调用 clean 清空扫描缓存。
import { initManager, scan, getPeripheral } from '@/uni_modules/XF-bleUTS'

initManager({
  success: (res) => {
    if (res.state == 'poweredOn') {
      scan({ success: () => console.log('scan started') })
    }
  },
  fail: (err) => console.log(JSON.stringify(err))
})

getPeripheral({
  success: (res) => console.log(JSON.stringify(res.peripherals))
})

权限配置

Android

插件内置 utssdk/app-android/AndroidManifest.xml,声明:

  • android.permission.BLUETOOTH
  • android.permission.BLUETOOTH_ADMIN
  • android.permission.BLUETOOTH_SCAN
  • android.permission.BLUETOOTH_CONNECT
  • android.permission.ACCESS_FINE_LOCATION
  • android.permission.ACCESS_COARSE_LOCATION

Android 12 及以上运行时申请 BLUETOOTH_SCANBLUETOOTH_CONNECT;Android 12 以下申请定位权限。

iOS

项目需要声明蓝牙隐私说明:

  • NSBluetoothAlwaysUsageDescription
  • NSBluetoothPeripheralUsageDescription

如果需要后台蓝牙能力,还需要配置 bluetooth-centralbluetooth-peripheral 后台模式。

Harmony

项目需要在 harmony-configs/entry/src/main/module.json5 声明:

  • ohos.permission.ACCESS_BLUETOOTH

通用类型

回调

所有异步接口沿用 uni-app x 常见的 successfailcomplete 回调字段。成功结果直接返回对象;失败结果为 XFBleFail

export interface XFBleFail extends IUniError {
  errCode: XFBleErrorCode
  nativeCode: number
}

errCode 是插件统一错误码,nativeCode 是平台原生错误码或参考错误编号。

errCode 含义
9010001 蓝牙管理器未初始化
9010002 设备不支持蓝牙
9010003 蓝牙未开启
9010004 蓝牙权限未授权
9010005 参数无效
9010010 缺少 peripheralUUID
9010011 缺少 serviceUUID
9010012 缺少 characteristicUUID
9010013 缺少 descriptorUUID
9010014 缺少或无法解析 value
9010020 未找到外围设备
9010021 未找到服务
9010022 未找到特征
9010023 未找到描述符
9010099 原生 BLE 操作失败

数据对象

XFBlePeripheral

字段 类型 说明
uuid string 外围设备标识。Android 通常为 MAC 地址,iOS 为 CoreBluetooth UUID,Harmony 为设备 ID
name string 设备名称,可能为空
rssi number 信号强度,可能为空
manufacturerData string 广播数据,十六进制字符串,可能为空
services Array<string> 已知服务 UUID,部分接口返回

XFBleCharacteristic

字段 类型 说明
uuid string 特征 UUID
serviceUUID string 所属服务 UUID
value string 特征值,十六进制字符串
permissions string 权限描述,如 readablewriteable
properties string 属性描述,如 readwritenotify
propertie string 原字段拼写
descriptors Array<XFBleDescriptor> 描述符列表,可能为空

XFBleDescriptor

字段 类型 说明
uuid string 描述符 UUID
serviceUUID string 所属服务 UUID
characteristicUUID string 所属特征 UUID
decode boolean 值是否已按二进制转码
value string 描述符值,十六进制字符串

读写特征和描述符时,value 统一传十六进制字符串,例如 0102030a

三端接口对比

接口 Android iOS Harmony 说明
initManager 支持 支持 支持 初始化蓝牙管理器,返回蓝牙状态
scan 支持 支持 支持 开始扫描,可传 serviceUUIDs 过滤
getPeripheral 支持 支持 支持 返回当前缓存的扫描设备列表
getPeripheralRssi 支持 支持 支持 连接后读取 RSSI;参考文档原只标 iOS,本插件三端均实现
isScanning 支持 支持 支持 查询扫描状态
stopScan 支持 支持 支持 停止扫描
connect 支持 支持 支持 连接指定设备
disconnect 支持 支持 支持 断开指定设备
isConnected 支持 支持 支持 判断指定设备是否连接
getPeripheralState 支持 支持 支持 插件额外接口,返回连接状态字符串
retrievePeripheral 支持 支持 支持 按 UUID 从已知设备或系统记录中取设备
retrieveConnectedPeripheral 支持 支持 支持 按服务 UUID 查找已连接设备
discoverService 支持 支持 支持 发现服务
discoverCharacteristics 支持 支持 支持 发现特征
discoverDescriptorsForCharacteristic 支持 支持 支持 发现描述符
setNotify 支持 支持 支持 开启 notify 并通过回调返回数据
stopNotify 支持 支持 支持 停止指定 notify;不传参数时清理全部 notify 缓存和回调
readValueForCharacteristic 支持 支持 支持 读取特征值
readValueForDescriptor 支持 支持 支持 读取描述符值
writeValueForCharacteristic 支持 支持 支持 写入特征值
writeValueForDescriptor 支持 支持 支持 写入描述符值
connectPeripherals 支持 支持 支持 批量连接,按设备逐个触发回调
setSimpleNotify 支持 支持 支持 开启 notify,但数据进入内部缓存
getAllSimpleNotifyData 支持 支持 支持 读取 simple notify 缓存
clearAllSimpleNotifyData 支持 支持 支持 清空 simple notify 缓存
clean 支持 支持 支持 清空插件缓存的扫描设备信息

接口说明

initManager

初始化 BLE 管理器。

参数:

字段 类型 必填 说明
single boolean 是否使用单例管理器,默认 false
success (res) => void 成功回调
fail (err) => void 失败回调
complete (res) => void 完成回调

返回:{ state }state 可为 poweredOnpoweredOffresettingunauthorizedunknownunsupported

scan

开始扫描 BLE 设备。扫描结果不会直接在 scan 回调中返回,需要调用 getPeripheral 获取当前缓存列表。

参数:

字段 类型 必填 说明
serviceUUIDs Array<string> 服务 UUID 过滤列表;不传或空数组表示扫描所有 BLE 广播设备
clean boolean 扫描前是否清空已有扫描缓存,默认 true

返回:{ status: true }

三端差异:Android 21+ 使用 BluetoothLeScanner,旧版使用 startLeScan;iOS 使用 scanForPeripherals;Harmony 使用 startBLEScan,不传过滤条件时向系统传 null

getPeripheral

获取当前扫描缓存中的设备。

参数:只有回调字段。

返回:{ peripherals: XFBlePeripheral[] }

getPeripheralRssi

读取指定已连接设备的 RSSI。

参数:

字段 类型 必填 说明
peripheralUUID string 设备 UUID

返回:{ status: true, rssi: number }

建议连接成功后调用。

isScanning

查询是否正在扫描。

返回:{ status: boolean }

stopScan

停止扫描。无参数,无回调。

connect

连接指定设备。

参数:

字段 类型 必填 说明
peripheralUUID string 设备 UUID

返回:{ status: true }

连接前通常需要先 scan 并通过 getPeripheral 取得设备 UUID。

disconnect

断开指定设备连接。

参数:peripheralUUID

返回:{ status: boolean, peripheralUUID: string }

isConnected

判断指定设备是否处于连接状态。

参数:peripheralUUID

返回:{ status: boolean, peripheralUUID: string }

getPeripheralState

获取指定设备连接状态。该接口是插件保留的扩展接口。

参数:peripheralUUID

返回:{ state, peripheralUUID }state 可为 disconnectedconnectingconnecteddisconnectingunknow

retrievePeripheral

根据 UUID 数组找回设备信息。

参数:

字段 类型 必填 说明
peripheralUUIDs Array<string> 设备 UUID 列表

返回:{ peripherals: XFBlePeripheral[] }

三端差异:iOS 调用系统 retrievePeripherals;Android 和 Harmony 主要从插件当前已知设备缓存中查找。

retrieveConnectedPeripheral

根据服务 UUID 查找当前已连接设备。

参数:

字段 类型 必填 说明
serviceUUIDs Array<string> 服务 UUID 列表;Android/Harmony 空数组表示不过滤本插件已连接设备;iOS 需要有效服务列表

返回:{ peripherals: XFBlePeripheral[] }

discoverService

发现指定设备的服务。

参数:

字段 类型 必填 说明
peripheralUUID string 设备 UUID
serviceUUIDs Array<string> 需要过滤的服务 UUID 列表

返回:{ status: true, services: string[] }

discoverCharacteristics

发现指定服务下的所有特征。

参数:peripheralUUIDserviceUUID

返回:{ status: true, characteristics: XFBleCharacteristic[] }

discoverDescriptorsForCharacteristic

发现指定特征下的所有描述符。

参数:peripheralUUIDserviceUUIDcharacteristicUUID

返回:{ status: true, descriptors: XFBleDescriptor[] }

setNotify

开启指定特征 notify。收到数据时会重复触发 success/complete 回调。

参数:peripheralUUIDserviceUUIDcharacteristicUUID

返回:{ status: true, characteristic: XFBleCharacteristic }

stopNotify

停止 notify。

参数可选:

字段 类型 必填 说明
peripheralUUID string 设备 UUID
serviceUUID string 服务 UUID
characteristicUUID string 特征 UUID

不传参数时,三端都会按各自实现清理 notify 相关记录;传入完整三元组时停止对应特征的 notify。

readValueForCharacteristic

读取指定特征值。

参数:peripheralUUIDserviceUUIDcharacteristicUUID

返回:{ status: true, characteristic: XFBleCharacteristic }

readValueForDescriptor

读取指定描述符值。

参数:peripheralUUIDserviceUUIDcharacteristicUUIDdescriptorUUID

返回:{ status: true, descriptor: XFBleDescriptor }

writeValueForCharacteristic

写入指定特征值。

参数:

字段 类型 必填 说明
peripheralUUID string 设备 UUID
serviceUUID string 服务 UUID
characteristicUUID string 特征 UUID
value string 十六进制字符串
writeType string autoresponsewithoutResponsesigned

返回:{ status: true, characteristic: XFBleCharacteristic }

三端差异:Android 支持 signed;iOS 的 auto 会在支持 writeWithoutResponse 时自动使用无响应写;Harmony 当前只区分 withoutResponse 和其它写法,其它值按有响应写处理。

writeValueForDescriptor

写入指定描述符值。

参数:peripheralUUIDserviceUUIDcharacteristicUUIDdescriptorUUIDvalue

返回:{ status: true, descriptor: XFBleDescriptor }

connectPeripherals

批量连接设备。

参数:

字段 类型 必填 说明
peripheralUUIDs Array<string> 设备 UUID 列表

每个设备会按平台连接结果触发回调。

setSimpleNotify

开启指定特征 notify,但通知数据不会直接通过该接口持续返回,而是缓存到插件内部。

参数:peripheralUUIDserviceUUIDcharacteristicUUID

返回:{ status: true }

getAllSimpleNotifyData

读取 setSimpleNotify 缓存的数据。

返回类型:Map<string, XFBleSimpleNotifyData>,key 为 peripheralUUID

type XFBleSimpleNotifyData = {
  serviceUUID: string
  characterUUID: string
  data: string[]
}

clearAllSimpleNotifyData

清空 simple notify 缓存。

返回:{ status: true }

clean

清空插件缓存的扫描设备信息。建议在未连接或业务确认不再使用缓存设备时调用;清空后依赖缓存的接口可能需要重新扫描或重新发现。

返回:{ status: true }

调试建议

  • 扫描为空:确认系统蓝牙已开启、动态权限已允许、附近设备正在 BLE 广播;Android 12 以下还要确认定位权限和系统定位开关。
  • 能扫描但连接失败:先确认设备未被其它手机占用,再检查 peripheralUUID 是否来自当前平台本次扫描结果。
  • 服务或特征为空:确认已连接成功,并按设备厂商文档使用正确的服务 UUID、特征 UUID。
  • 写入失败:确认 value 是偶数字符数的十六进制字符串,并确认特征支持 writewriteWithoutResponse
  • notify 无数据:确认特征支持 notifyindicate,并确认设备端确实在发送通知。

隐私、权限声明

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

- android.permission.BLUETOOTH - android.permission.BLUETOOTH_ADMIN - android.permission.BLUETOOTH_SCAN - android.permission.BLUETOOTH_CONNECT - android.permission.ACCESS_FINE_LOCATION - android.permission.ACCESS_COARSE_LOCATION - NSBluetoothAlwaysUsageDescription - NSBluetoothPeripheralUsageDescription

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

不采集任何数据;仅在调用方触发蓝牙功能时扫描、连接并收发 BLE 设备数据

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

暂无用户评论。