一、蓝牙官方api文档
设备 / 蓝牙-通用 / wx.startBluetoothDevicesDiscovery (qq.com)
二、蓝牙重要参数介绍以及自我理解参数
1 | deviceid | 蓝牙设备的id | 这个参数是蓝牙设备的唯一id |
2 | uuid | 服务的id | 这个是通过deviceid获取到的这个设备服务的uuid |
3 | characteristic | 特性值 | 这个是通过deviceid、uuid获取到的特性值 |
重点:辅助理解这几个值的意思
首先deviceid是比较清楚的,它是蓝牙设备的唯一标识它只有一个,它的用途在于找到蓝牙之后进行匹配蓝牙。其次是uuid它是通过deviced获得得到的,通过deviced就可以获取到它蓝牙的所有服务,服务就比如蓝牙的读写功能。还有服务嘛就是有很多,所以uuid不止有一个,使用哪个服务uuid看自己。下来就是特征值他是由devided和uuid得到的,它相当于是一个里面的功能而uuid是这个功能的门牌码,功能当然也有很多个,所以特性值也有好多个,一般使用写功能,遍历出来之后拿到写的特性值就行了。
三、实操代码
1、初始化蓝牙
initBlue:function(){
var that = this;
wx.openBluetoothAdapter({
success: function (res) {
wx.showToast({
title: '初始化成功',
icon: 'success',
duration: 800
})
that.findBlue();//2.0
},
fail: function (res) {//如果手机上的蓝牙没有打开,可以提醒用户
wx.showToast({
title: '请开启蓝牙',
icon: 'fails',
duration: 1000
})
}
})
},
2、开始搜索蓝牙设备(那几个参数要写,具体自己查,还有的厂商有自己的service,可以直接过滤蓝牙设备)
findBlue(){
var that = this
wx.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
interval: 0,
powerLevel: 'high',
success: function (res) {
that.getBlue()//3.0
}
})
},
3、拿到搜索到的蓝牙设备
getBlue(){
var that = this;
wx.onBluetoothDeviceFound(function (devices) {
if (devices.devices) {
if(devices.devices[0].name=="这个要自己修改看要怎样的设备")
{
that.setData({
deviceid: devices.devices[0].deviceId,
})
that.connetBlue();
}
}
})
},
4、拿到上面的蓝牙id进行连接
connetBlue(){
var that = this;
var deviceid = that.data.deviceid;
wx.createBLEConnection({
// 这里的 deviceid 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId: deviceid,//设备id
timeout: 10000, // 10s连接超时
success: function (res)
wx.showToast({
title: '连接成功',
icon: 'fails',
duration: 800
})
wx.stopBluetoothDevicesDiscovery()
that.getServiceId()//5.0
}
})
},
5、连接之后拿到蓝牙id获取uuid
getServiceId(){
var that = this
wx.getBLEDeviceServices({
// 这里的 deviceid 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId: that.data.deviceid,
success: function (res) {
var model = res.services[0].uuid
that.getCharacteId()//6.0
}
})
},
6、获取特征值(这一步需要蓝牙id和uuid来获取特征值)
getCharacteId(){
var that = this
wx.getBLEDeviceCharacteristics({
deviceId: that.data.deviceid,
serviceId: that.data.services,
success: function (res) {
// 遍历特征值列表
for (let i = 0; i < res.characteristics.length; i++) {
let characteristic = res.characteristics[i];
// 检查特征值是否具有 read 属性为 false
if (!characteristic.properties.read) {
// 当找到 read 属性为 false 的特征值时,设置 writeId 为该特征值的 UUID
that.setData({
writeId: characteristic.uuid
});
break; // 找到符合条件的特征值后,结束循环
}
}
},
fail: function (res) {
// 处理获取特征值失败的情况
console.log("失败的原因",res);
}
})
},
7、启用 notify 功能(他就是下一步的时候你给你连接的蓝牙设备写入数据的时候,设备给你回消息的时候检测用的)
startNotice(uuid){
var that = this;
wx.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
deviceId: that.data.deviceid,
serviceId: that.data.services,
characteristicId: uuid, //第一步 开启监听 notityid 第二步发送指令 write
success: function (res) {
wx.onBLECharacteristicValueChange(function (res) {
// 这里面需要把回你的数据下转换格式
})
}
})
},
8、开始给蓝牙发送数据(数据不能大于20字节),直接用就行。
sendmmm:function(sendData){
var that = this;
//向蓝牙发送数据
let buffer = that.string2buffer(sendData);//转16进制
let pos = 0;
let bytes = buffer.byteLength;
var result = ''
let tmpBuffer;
var sdtim= setInterval(function(){
if (bytes > 20) {
tmpBuffer = buffer.slice(pos, pos + 20);
console.log('字节:');
console.log(tmpBuffer);
pos += 20;
bytes -= 20;
wx.writeBLECharacteristicValue({
deviceId: that.data.deviceid,
serviceId: that.data.services,
characteristicId: that.data.writeId,//第二步写入的特征值
value: tmpBuffer,
success(res) {
console.log('发送成功!', res)
}
})
} else {
tmpBuffer = buffer.slice(pos, pos + bytes);
console.log('字节:');
console.log(tmpBuffer);
pos += bytes;
bytes -= bytes;
wx.writeBLECharacteristicValue({
deviceId: that.data.deviceid,
serviceId: that.data.services,
characteristicId: that.data.writeId,//第二步写入的特征值
value: tmpBuffer,
success(res) {
console.log('最后次发送', res)
})
clearInterval(sdtim);
},
fail: function (res) {
console.log('发送失败', res)
}
})
}
}, 30);
},
9、转换数据类型,就是上一步写的里面有个调用这个函数,直接抄就行
string2buffer(str) {
let buffer = new ArrayBuffer(str.length * 2); // 每个字符占2个字节
let bufferView = new Uint16Array(buffer);
for (let i = 0; i < str.length; i++) {
bufferView[i] = str.charCodeAt(i);
}
return buffer;
},
四、注意事项
只能连接蓝牙4.0以上低功耗,就是手机啥的蓝牙检测不到,耳机什么的可以检测到