qq网站登录网址,太原网站优化服务,行业网站开发方案,徐州市网站目录 一、蓝牙官方api文档
二、蓝牙重要参数介绍
三、案例教程
1. 获取蓝牙权限#xff08;openBluetoothAdapter#xff09; 2. 开始搜索蓝牙设备(startBluetoothDevicesDiscovery)
3. 监听搜索到新设备的事件(onBluetoothDeviceFound)
4.连接蓝牙设备#xff08;crea…目录 一、蓝牙官方api文档
二、蓝牙重要参数介绍
三、案例教程
1. 获取蓝牙权限openBluetoothAdapter 2. 开始搜索蓝牙设备(startBluetoothDevicesDiscovery)
3. 监听搜索到新设备的事件(onBluetoothDeviceFound)
4.连接蓝牙设备createBLEConnection
5.获取蓝牙设备服务getBLEDeviceServices
6.获取某个蓝牙服务特征getBLEDeviceCharacteristics
7.启动蓝牙服务值变化监听及监听特征值变化notifyBLECharacteristicValueChange、onBLECharacteristicValueChange
8.向蓝牙写入数据writeBLECharacteristicValue
9.string2buffer函数
四、扩展 一、蓝牙官方api文档
wx.startBluetoothDevicesDiscovery(Object object) | 微信开放文档微信开发者平台文档https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth/wx.startBluetoothDevicesDiscovery.html
二、蓝牙重要参数介绍
1 deviceid 蓝牙设备的id这个参数是蓝牙设备的唯一id2 uuid 服务的id这个是通过deviceid获取到的这个设备服务的uuid3 characteristic 特性值这个是通过deviceid、uuid获取到的特性值
重点辅助理解这几个值的意思 首先deviceid是比较清楚的它是蓝牙设备的唯一标识它只有一个它的用途在于找到蓝牙之后进行匹配蓝牙。其次是uuid它是通过deviced获得得到的通过deviced就可以获取到它蓝牙的所有服务服务就是蓝牙设置支持的某个能力例如开关led灯。还有服务嘛就是有很多所以uuid不止有一个使用哪个服务uuid看自己。下来就是特征值他是由devided和uuid得到的它相当于是一个里面的功能而uuid是这个功能的门牌码功能当然也有很多个所以特性值也有好多个一般使用写功能遍历出来之后拿到写的特性值就行了。 三、案例教程
1. 获取蓝牙权限openBluetoothAdapter
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. 开始搜索蓝牙设备(startBluetoothDevicesDiscovery)
findBlue(){var that thiswx.startBluetoothDevicesDiscovery({allowDuplicatesKey: true,interval: 0,powerLevel: high,success: function (res) {that.getBlue()//3.0}})
},
3. 监听搜索到新设备的事件(onBluetoothDeviceFound)
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.连接蓝牙设备createBLEConnection
connetBlue(){ var that this;var deviceid that.data.deviceid;wx.createBLEConnection({// 这里的 deviceid 需要已经通过 createBLEConnection 与对应设备建立链接deviceId: deviceid,//设备idtimeout: 10000, // 10s连接超时success: function (res) wx.showToast({title: 连接成功,icon: fails,duration: 800})wx.stopBluetoothDevicesDiscovery()that.getServiceId()//5.0}})
},
5.获取蓝牙设备服务getBLEDeviceServices
getServiceId(){var that thiswx.getBLEDeviceServices({// 这里的 deviceid 需要已经通过 createBLEConnection 与对应设备建立链接deviceId: that.data.deviceid,success: function (res) {var model res.services[0].uuidthat.getCharacteId()//6.0}})
},
6.获取某个蓝牙服务特征getBLEDeviceCharacteristics
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 属性为 falseif (!characteristic.properties.read) {// 当找到 read 属性为 false 的特征值时设置 writeId 为该特征值的 UUIDthat.setData({writeId: characteristic.uuid});break; // 找到符合条件的特征值后结束循环}}},fail: function (res) {// 处理获取特征值失败的情况console.log(失败的原因,res);}})
},
7.启动蓝牙服务值变化监听及监听特征值变化notifyBLECharacteristicValueChange、onBLECharacteristicValueChange 启用蓝牙低功耗设备特征值变化时的 notify 功能订阅特征。注意必须设备的特征支持 notify 或者 indicate 才可以成功调用。 另外必须先启用 wx.notifyBLECharacteristicValueChange 才能监听到设备 characteristicValueChange 事件 startNotice(uuid){var that this;wx.notifyBLECharacteristicValueChange({state: true, // 启用 notify 功能deviceId: that.data.deviceid,serviceId: that.data.services,characteristicId: uuid, //第一步 开启监听 notityid 第二步发送指令 writesuccess: function (res) {wx.onBLECharacteristicValueChange(function (res) {// 这里面需要把回你的数据下转换格式})}})
},
8.向蓝牙写入数据writeBLECharacteristicValue
数据不能大于20字节直接用就行大于20字节则进行轮训发送string2buffer函数在下面 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函数 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;},
四、扩展
蓝牙 (Bluetooth) | 微信开放文档
只能连接蓝牙4.0以上低功耗就是手机啥的蓝牙检测不到耳机什么的可以检测到