基础库版本 1.1.0 开始支持,低版本需做兼容处理
ios 微信客户端 6.5.6 版本开始支持,android 客户端暂不支持
wx.openbluetoothadapter(object)
基础库版本 1.1.0 开始支持,低版本需做兼容处理
初始化蓝牙适配器
object参数说明:
参数 | 类型 | 必填 | 说明 |
---|
success | function | 是 | 成功则返回成功初始化信息 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例代码:
wx.openbluetoothadapter({
success: function (res) {
console.log(res)
}
})
bug & tip
tip
: 由于系统的问题,目前仅在 mac 版的开发工具上支持蓝牙调试tip
: 基础库版本 1.1.0 开始支持,低版本需做兼容处理
wx.closebluetoothadapter(object)
基础库版本 1.1.0 开始支持,低版本需做兼容处理
关闭蓝牙模块。调用该方法将断开所有已建立的链接并释放系统资源
object参数说明:
参数 | 类型 | 必填 | 说明 |
---|
success | function | 是 | 成功则返回成功关闭模块信息 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例代码:
wx.closebluetoothadapter({
success: function (res) {
console.log(res)
}
})
wx.getbluetoothadapterstate(object)
基础库版本 1.1.0 开始支持,低版本需做兼容处理
获取本机蓝牙适配器状态
object参数说明:
参数 | 类型 | 必填 | 说明 |
---|
success | function | 是 | 成功则返回本机蓝牙适配器状态 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
success返回参数:
参数 | 类型 | 说明 |
---|
discovering | boolean | 是否正在搜索设备 |
available | boolean | 蓝牙适配器是否可用 |
errmsg | string | 成功:ok,错误:详细信息 |
示例代码:
wx.getbluetoothadapterstate({
success: function (res) {
console.log(res)
}
})
wx.onbluetoothadapterstatechange(callback)
基础库版本 1.1.0 开始支持,低版本需做兼容处理
监听蓝牙适配器状态变化事件
callback参数说明:
参数 | 类型 | 说明 |
---|
available | boolean | 蓝牙适配器是否可用 |
discovering | boolean | 蓝牙适配器是否处于搜索状态 |
示例代码:
wx.onbluetoothadapterstatechange(function(res) {
console.log(`adapterstate changed, now is`, res)
})
wx.startbluetoothdevicesdiscovery(object)
基础库版本 1.1.0 开始支持,低版本需做兼容处理
开始搜寻附近的蓝牙外围设备。注意,该操作比较耗费系统资源,请在搜索并连接到设备后调用 stop 方法停止搜索。
object参数说明:
参数 | 类型 | 必填 | 说明 |
---|
services | array | 否 | 蓝牙设备主 service 的 uuid 列表 |
allowduplicateskey | boolean | 否 | 是否允许重复上报同一设备, 如果允许重复上报,则ondevicefound 方法会多次上报同一设备,但是 rssi 值会有不同 |
interval | integer | 否 | 上报设备的间隔,默认为0,意思是找到新设备立即上报,否则根据传入的间隔上报 |
success | function | 是 | 成功则返回本机蓝牙适配器状态 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
services参数说明:某些蓝牙设备会广播自己的主 service 的 uuid。如果这里传入该数组,那么根据该 uuid 列表,只搜索有这个主服务的设备。
success返回参数:
参数 | 类型 | 说明 |
---|
errmsg | string | 成功:ok,错误:详细信息 |
isdiscovering | boolean | 当前蓝牙适配器是否处于搜索状态 |
示例代码:
wx.startbluetoothdevicesdiscovery({
services: ['fee7'],
success: function (res) {
console.log(res)
}
})
wx.stopbluetoothdevicesdiscovery(object)
基础库版本 1.1.0 开始支持,低版本需做兼容处理
停止搜寻附近的蓝牙外围设备。请在确保找到需要连接的设备后调用该方法停止搜索。
object参数说明:
参数 | 类型 | 必填 | 说明 |
---|
success | function | 是 | 成功则返回本机蓝牙适配器状态 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
success返回参数:
参数 | 类型 | 说明 |
---|
errmsg | string | 成功:ok,错误:详细信息 |
adapterstate
蓝牙适配器状态信息
参数 | 类型 | 说明 |
---|
discovering | boolean | 是否正在搜索设备 |
available | boolean | 蓝牙适配器是否可用 |
示例代码:
wx.stopbluetoothdevicesdiscovery({
success: function (res) {
console.log(res)
}
})
wx.getbluetoothdevices(object)
基础库版本 1.1.0 开始支持,低版本需做兼容处理
获取所有已发现的蓝牙设备,包括已经和本机处于连接状态的设备
object参数说明:
参数 | 类型 | 必填 | 说明 |
---|
success | function | 是 | 成功则返回本机蓝牙适配器状态 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
success返回参数:
参数 | 类型 | 说明 |
---|
devices | array | uuid 对应的的已连接设备列表 |
errmsg | string | 成功:ok,错误:详细信息 |
device 对象
蓝牙设备信息
参数 | 类型 | 说明 |
---|
name | string | 蓝牙设备名称,某些设备可能没有 |
deviceid | string | 用于区分设备的 id |
rssi | int | 当前蓝牙设备的信号强度 |
advertisdata | arraybuffer | 当前蓝牙设备的广播内容(注意:vconsole 无法打印出 arraybuffer 类型数据) |
示例代码:
wx.getbluetoothdevices({
success: function (res) {
console.log(res)
}
})
bug & tip
tip
: mac系统可能无法获取advertisdata
及rssi
,请使用真机调试tip
: 开发者工具和 android 上获取到的deviceid
为设备 mac 地址,ios 上则为设备 uuid。因此deviceid
不能硬编码到代码中
wx.getconnectedbluetoothdevices(object)
基础库版本 1.1.0 开始支持,低版本需做兼容处理
根据 uuid 获取处于已连接状态的设备
object参数说明:
参数 | 类型 | 必填 | 说明 |
---|
services | array | 是 | 蓝牙设备主 service 的 uuid 列表 |
success | function | 是 | 成功则返回本机蓝牙适配器状态 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
success返回参数:
参数 | 类型 | 说明 |
---|
devices | array | 搜索到的设备列表 |
errmsg | string | 成功:ok,错误:详细信息 |
device对象
蓝牙设备信息
参数 | 类型 | 说明 |
---|
name | string | 蓝牙设备名称,某些设备可能没有 |
deviceid | string | 用于区分设备的 id |
示例代码:
wx.getconnectedbluetoothdevices({
success: function (res) {
console.log(res)
}
})
bug & tip
tip
: 开发者工具和 android 上获取到的deviceid
为设备 mac 地址,ios 上则为设备 uuid。因此deviceid
不能硬编码到代码中
wx.onbluetoothdevicefound(callback)
基础库版本 1.1.0 开始支持,低版本需做兼容处理
监听寻找到新设备的事件
callback参数说明:
参数 | 类型 | 说明 |
---|
devices | array | 新搜索到的设备列表 |
device对象
参数 | 类型 | 说明 |
---|
deviceid | string | 蓝牙设备 id,参考 device 对象 |
name | string | 蓝牙设备名称,参考 device 对象 |
rssi | int | 当前蓝牙设备的信号强度 |
advertisdata | arraybuffer | 当前蓝牙设备的广播内容(注意:vconsole 无法打印出 arraybuffer 类型数据) |
示例代码:
wx.onbluetoothdevicefound(function(devices) {
console.log('new device list has founded')
console.dir(devices)
})
bug & tip
tip
: mac系统可能无法获取advertisdata
及rssi
,请使用真机调试tip
: 开发者工具和 android 上获取到的deviceid
为设备 mac 地址,ios 上则为设备 uuid。因此deviceid
不能硬编码到代码中
低功耗蓝牙接口
wx.createbleconnection(object)
基础库版本 1.1.0 开始支持,低版本需做兼容处理
连接低功耗蓝牙设备
object参数说明:
参数 | 类型 | 必填 | 说明 |
---|
deviceid | string | 是 | 蓝牙设备 id,参考 getdevices 接口 |
success | function | 是 | 成功则返回本机蓝牙适配器状态 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
success返回参数:
参数 | 类型 | 说明 |
---|
errmsg | string | 成功:ok,错误:详细信息 |
示例代码:
wx.createbleconnection({
deviceid: deviceid,
success: function (res) {
console.log(res)
}
})
wx.closebleconnection(object)
基础库版本 1.1.0 开始支持,低版本需做兼容处理
断开与低功耗蓝牙设备的连接
object参数说明:
参数 | 类型 | 必填 | 说明 |
---|
deviceid | string | 是 | 蓝牙设备 id,参考 getdevices 接口 |
success | function | 是 | 成功则返回本机蓝牙适配器状态 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
success返回参数:
参数 | 类型 | 说明 |
---|
errmsg | string | 成功:ok,错误:详细信息 |
示例代码:
wx.closebleconnection({
success: function (res) {
console.log(res)
}
})
wx.getbledeviceservices(object)
基础库版本 1.1.0 开始支持,低版本需做兼容处理
获取蓝牙设备所有 service(服务)
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: 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: deviceid,
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: deviceid,
serviceid: serviceid,
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,错误:详细信息 |
示例代码:
wx.onblecharacteristicvaluechange(function(characteristic) {
console.log('characteristic value changed:', characteristic)
})
let buffer = new arraybuffer(1)
let dataview = new dataview(buffer)
dataview.setuint8(0, 0)
wx.writeblecharacteristicvalue({
deviceid: deviceid,
serviceid: serviceid,
characteristicid: characteristicid,
value: buffer,
success: function (res) {
console.log('writeblecharacteristicvalue success', res.errmsg)
}
})
wx.notifyblecharacteristicvaluechanged(object)
基础库版本 1.1.0 开始支持,低版本需做兼容处理
启用低功耗蓝牙设备特征值变化时的 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,
deviceid: deviceid,
serviceid: serviceid,
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 | 没有找到指定描述符 |