OBJECT参数说明:
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
deviceId | string | 是 | 蓝牙设备 id,参考 getDevices 接口 |
success | Function | 是 | 成功则返回本机蓝牙适配器状态 |
fail | Function | 否 | 接口调用失败的回调函数 |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
success返回参数:
参数 | 类型 | 说明 |
---|---|---|
services | array | 设备服务列表 |
errMsg | string | 成功:ok,错误:详细信息 |
service对象
蓝牙设备service(服务)信息
参数 | 类型 | 说明 |
---|---|---|
uuid | string | 蓝牙设备服务的 uuid |
isPrimary | boolean | 该服务是否为主服务 |
示例代码:
wx.getBLEDeviceServices({
// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
deviceId: deviceId,
success: function (res) {
console.log('device services:', res.services)
}
})
wx.getBLEDeviceCharacteristics(OBJECT)
基础库版本 1.1.0 开始支持,低版本需做兼容处理获取蓝牙设备所有 characteristic(特征值)
OBJECT参数说明:
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
deviceId | string | 是 | 蓝牙设备 id,参考 device 对象 |
serviceId | string | 是 | 蓝牙服务 uuid |
success | Function | 是 | 成功则返回本机蓝牙适配器状态 |
fail | Function | 否 | 接口调用失败的回调函数 |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
success返回参数:
参数 | 类型 | 说明 |
---|---|---|
characteristics | array | 设备特征值列表 |
errMsg | string | 成功:ok,错误:详细信息 |
characteristic对象
蓝牙设备characteristic(特征值)信息
参数 | 类型 | 说明 |
---|---|---|
uuid | string | 蓝牙设备特征值的 uuid |
properties | object | 该特征值支持的操作类型 |
properties对象
参数 | 类型 | 说明 |
---|---|---|
read | boolean | 该特征值是否支持 read 操作 |
write | boolean | 该特征值是否支持 write 操作 |
notify | boolean | 该特征值是否支持 notify 操作 |
indicate | boolean | 该特征值是否支持 indicate 操作 |
示例代码:
wx.getBLEDeviceCharacteristics({
// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
deviceId: deviceId,
// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
serviceId: serviceId,
success: function (res) {
console.log('device getBLEDeviceCharacteristics:', res.characteristics)
}
})
wx.readBLECharacteristicValue(OBJECT)
基础库版本 1.1.0 开始支持,低版本需做兼容处理读取低功耗蓝牙设备的特征值的二进制数据值。注意:必须设备的特征值支持read
才可以成功调用,具体参照 characteristic 的 properties 属性
OBJECT参数说明:
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
deviceId | string | 是 | 蓝牙设备 id,参考 device 对象 |
serviceId | string | 是 | 蓝牙特征值对应服务的 uuid |
characteristicId | string | 是 | 蓝牙特征值的 uuid |
success | Function | 是 | 成功则返回本机蓝牙适配器状态 |
fail | Function | 否 | 接口调用失败的回调函数 |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
success返回参数:
参数 | 类型 | 说明 |
---|---|---|
characteristic | object | 设备特征值信息 |
errMsg | string | 成功:ok,错误:详细信息 |
characteristic对象
蓝牙设备characteristic(特征值)信息
参数 | 类型 | 说明 |
---|---|---|
characteristicId | string | 蓝牙设备特征值的 uuid |
serviceId | object | 蓝牙设备特征值对应服务的 uuid |
value | ArrayBuffer | 蓝牙设备特征值对应的二进制值(注意:vConsole 无法打印出 ArrayBuffer 类型数据) |
示例代码:
// 必须在这里的回调才能获取
wx.onBLECharacteristicValueChange(function(characteristic) {
console.log('characteristic value comed:', characteristic)
})
wx.readBLECharacteristicValue({
// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
deviceId: deviceId,
// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
serviceId: serviceId,
// 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
characteristicId: characteristicId,
success: function (res) {
console.log('readBLECharacteristicValue:', res.characteristic.value)
}
})
Bug & Tip
tip
: 并行调用多次读写接口存在读写失败的可能性。tip
:read
接口读取到的信息需要在onBLECharacteristicValueChange
方法注册的回调中获取。
wx.writeBLECharacteristicValue(OBJECT)
基础库版本 1.1.0 开始支持,低版本需做兼容处理向低功耗蓝牙设备特征值中写入二进制数据。注意:必须设备的特征值支持write
才可以成功调用,具体参照 characteristic 的 properties 属性
tips: 并行调用多次读写接口存在读写失败的可能性
OBJECT参数说明:
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
deviceId | string | 是 | 蓝牙设备 id,参考 device 对象 |
serviceId | string | 是 | 蓝牙特征值对应服务的 uuid |
characteristicId | string | 是 | 蓝牙特征值的 uuid |
value | ArrayBuffer | 是 | 蓝牙设备特征值对应的二进制值(注意:vConsole 无法打印出 ArrayBuffer 类型数据) |
success | Function | 是 | 成功则返回本机蓝牙适配器状态 |
fail | Function | 否 | 接口调用失败的回调函数 |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
success返回参数:
参数 | 类型 | 说明 |
---|---|---|
errMsg | string | 成功:ok,错误:详细信息 |
示例代码:
// 这里的回调可以获取到 write 导致的特征值改变
wx.onBLECharacteristicValueChange(function(characteristic) {
console.log('characteristic value changed:', characteristic)
})
// 向蓝牙设备发送一个0x00的16进制数据
let buffer = new ArrayBuffer(1)
let dataView = new DataView(buffer)
dataView.setUint8(0, 0)
wx.writeBLECharacteristicValue({
// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
deviceId: deviceId,
// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
serviceId: serviceId,
// 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
characteristicId: characteristicId,
// 这里的value是ArrayBuffer类型
value: buffer,
success: function (res) {
console.log('writeBLECharacteristicValue success', res.errMsg)
}
})
wx.notifyBLECharacteristicValueChanged(OBJECT)
启用低功耗蓝牙设备特征值变化时的 notify 功能。注意:必须设备的特征值支持notify
才可以成功调用,具体参照 characteristic 的 properties 属性
另外,必须先启用notify
才能监听到设备 characteristicValueChange 事件
OBJECT参数说明:
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
deviceId | string | 是 | 蓝牙设备 id,参考 device 对象 |
serviceId | string | 是 | 蓝牙特征值对应服务的 uuid |
characteristicId | string | 是 | 蓝牙特征值的 uuid |
state | boolean | 是 | true: 启用 notify; false: 停用 notify |
success | Function | 是 | 成功则返回本机蓝牙适配器状态 |
fail | Function | 否 | 接口调用失败的回调函数 |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
success返回参数:
参数 | 类型 | 说明 |
---|---|---|
errMsg | string | 成功:ok,错误:详细信息 |
示例代码:
wx.notifyBLECharacteristicValueChanged({
state: true, // 启用 notify 功能
// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
deviceId: deviceId,
// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
serviceId: serviceId,
// 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
characteristicId: characteristicId,
success: function (res) {
console.log('notifyBLECharacteristicValueChanged success', res.errMsg)
}
})
wx.onBLEConnectionStateChanged(CALLBACK)
基础库版本 1.1.0 开始支持,低版本需做兼容处理监听低功耗蓝牙连接的错误事件,包括设备丢失,连接异常断开等等。
CALLBACK参数说明:
参数 | 类型 | 说明 |
---|---|---|
deviceId | string | 蓝牙设备 id,参考 device 对象 |
connected | boolean | 连接目前的状态 |
示例代码:
wx.onBLEConnectionStateChanged(function(res) {
// 该方法回调中可以用于处理连接意外断开等异常情况
console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
})
wx.onBLECharacteristicValueChange(CALLBACK)
基础库版本 1.1.0 开始支持,低版本需做兼容处理监听低功耗蓝牙设备的特征值变化。必须先启用notify
接口才能接收到设备推送的notification。
CALLBACK参数说明:
参数 | 类型 | 说明 |
---|---|---|
deviceId | string | 蓝牙设备 id,参考 device 对象 |
serviceId | string | 特征值所属服务 uuid |
characteristicId | string | 特征值 uuid |
value | ArrayBuffer | 特征值最新的值(注意:vConsole 无法打印出 ArrayBuffer 类型数据) |
示例代码:
wx.onBLECharacteristicValueChange(function(res) {
console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)
})
蓝牙错误码(errCode)列表
错误码 | 说明 | 备注 |
---|---|---|
0 | ok | 正常 |
10000 | not init | 未初始化蓝牙适配器 |
10001 | not available | 当前蓝牙适配器不可用 |
10002 | no device | 没有找到指定设备 |
10003 | connection fail | 连接失败 |
10004 | no service | 没有找到指定服务 |
10005 | no characteristic | 没有找到指定特征值 |
10006 | no connection | 当前连接已断开 |
10007 | property not support | 当前特征值不支持此操作 |
10008 | system error | 其余所有系统上报的异常 |
10009 | system not support | Android 系统特有,系统版本低于 4.3 不支持BLE |
10010 | no descriptor | 没有找到指定描述符 |
wx.makeBluetoothPair(Object object)
基础库 2.12.0 开始支持,低版本需做兼容处理。蓝牙配对接口,仅安卓使用。安卓上蓝牙连接时,部分设备需先配对。
参数
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
deviceId | string | 是 | 蓝牙设备 id | |
pin | ArrayBuffer | 否 | pin 码 | |
timeout | number | 20 | 否 | 超时时间 |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
标签:微信小程序
相关阅读 >>
微信小程序api 创建并返回上下文 wx.createcontext (不推荐使用)
更多相关阅读请进入《微信小程序》频道 >>

Vue.js 设计与实现 基于Vue.js 3 深入解析Vue.js 设计细节
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
相关推荐
评论
管理员已关闭评论功能...
- 欢迎访问木庄网络博客
- 可复制:代码框内的文字。
- 方法:Ctrl+C。