微信小程序 您所在的位置:网站首页 微信小程序怎么扫二维码 微信小程序

微信小程序

2024-06-17 01:06| 来源: 网络整理| 查看: 265

使用微信小程序的扫码功能连接蓝牙,具体操作如下

实现流程图 Created with Raphaël 2.2.0 准备好二维码 小程序调用扫码功能 小程序获取到二维码内容(我这里为蓝牙的名字) 小程序通过搜索到该蓝牙名后连接 结束

首先,准备需要使用的二维码 这里我们可以使用网上的二维码生成器,把需要的信息写入然后就能自动生成二维码了。我使用的是蓝牙的名字 使用蓝牙的名字生成一个二维码

调用微信小程序的蓝牙扫码功能 界面

在这里插入图片描述 以下是该功能代码

// index.wxml 扫码 // index.js Page({ data: { deviceName: "", }, onLoad: function () { }, onShow: function () { }, /* 点击事件 */ click: function (event) { var that = this; //调用扫码功能 wx.scanCode({ success: (res) => { console.log(res) that.setData({ deviceName: res.result }) //获取到二维码中的蓝牙设备名后跳转到连接页面 var title = that.data.deviceName; wx.redirectTo({ url: '../detail/detail?id=' + title }) }, fail: (res) => { wx.showToast({ title: '扫码失败', icon: 'success', duration: 2000 }) }, }) }, })

通过二维码获取到蓝牙设备名后连接 下面直接贴代码,只需要修改js就够了

//detail.js data: { deviceId: '', }, //获取二维码中的蓝牙name,然后连接 onLoad: function(options) { wx.showLoading({ title: '请打开蓝牙', }); console.log(options); this.setData({ deviceName: options.id, }); console.log('设备的Name', this.data.deviceName); var that = this; var deviceName = options.id; wx.closeBluetoothAdapter({ success: function(res) { console.log('关闭蓝牙模块'); /* 初始化蓝牙适配器 */ wx.openBluetoothAdapter({ success: function(res) { console.log('初始化蓝牙适配器成功'); wx.hideLoading(); wx.showLoading({ title: '请稍后....', }); wx.startBluetoothDevicesDiscovery({ allowDuplicatesKey: false, success: function(res) { console.log('这里是开始搜索附近设备', res); //添加延迟 setTimeout(() => { wx.getBluetoothDevices({ success: function(res) { console.log(res) //在搜索到的所有蓝牙中找到需要连接的那一个蓝牙 for (var i = 0; i that.setData({ deviceId: res.devices[i].deviceId, }) console.log(res.devices[i].deviceId) wx.hideLoading(); /* 连接中动画 */ wx.showLoading({ title: '正在连接...', }); that.CreateBLEConnection(); } } }, fail: function() { console.log("搜索蓝牙设备失败") } }); }, 1300); }, }); }, }) } }); }, //连接蓝牙 CreateBLEConnection: function() { var that = this; wx.stopBluetoothDevicesDiscovery({ success: function(res) { console.log('停止搜索设备', res) } }) /* 开始连接蓝牙设备 */ wx.createBLEConnection({ deviceId: that.data.deviceId, success: function(res) { console.log('连接成功', res); wx.hideLoading(); /* 获取设备的服务UUID */ wx.getBLEDeviceServices({ deviceId: that.data.deviceId, success: function(service) { var all_UUID = service.services; //取出所有的服务 console.log('所有的服务', all_UUID); var UUID_lenght = all_UUID.length; //获取到服务数组的长度 /* 遍历服务数组 */ for (var index = 0; index var index_uuid = index; that.setData({ serviceId: all_UUID[index_uuid].uuid //确定需要的服务UUID }); }; }; console.log('需要的服务UUID', that.data.serviceId) that.Characteristics(); //调用获取特征值函数 }, }); }, }) }, Characteristics: function() { var that = this; var device_characteristics = []; var characteristics_uuid = {}; wx.getBLEDeviceCharacteristics({ deviceId: that.data.deviceId, serviceId: that.data.serviceId, success: function(res) { var characteristics = res.characteristics; //获取到所有特征值 var characteristics_length = characteristics.length; //获取到特征值数组的长度 console.log('获取到特征值', characteristics); console.log('获取到特征值数组长度', characteristics_length); /* 遍历获取characteristicsId */ for (var index = 0; index var index_uuid = index; that.setData({ characteristicsId: characteristics[index_uuid].uuid //确定的写入UUID }); }; }; console.log('写入characteristicsId', that.data.characteristicsId); that.SendTap(); //发送指令 }, }) }, /* 发送指令 */ SendTap: function() { var that = this; var value_ascii = ""; var value_initial = "01"; //发送的信息 console.log('输入框中的值', value_initial); /* 以Ascii字符发送 */ var value_split = value_initial.split(''); //将字符一个一个分开 console.log('value_split', value_split); for (var i = 0; i var that = this; var value = str; console.log('value', value); /* 将数值转为ArrayBuffer类型数据 */ var typedArray = new Uint8Array(value.match(/[\da-f]{2}/gi).map(function(h) { return parseInt(h, 16) })); var buffer = typedArray.buffer; wx.writeBLECharacteristicValue({ deviceId: that.data.deviceId, serviceId: that.data.serviceId, characteristicId: that.data.characteristicsId, value: buffer, success: function(res) { console.log('数据发送成功', res); wx.showToast({ title: '发送成功', icon: 'success', duration: 2000 }) }, fail: function(res) { console.log('调用失败', res); /* 调用失败时,再次调用 */ wx.writeBLECharacteristicValue({ deviceId: that.data.deviceId, serviceId: that.data.serviceId, characteristicId: that.data.characteristicsId, value: buffer, success: function(res) { console.log('第2次数据发送成功', res); wx.showToast({ title: '发送成功', icon: 'success', duration: 2000 }) }, fail: function(res) { console.log('第2次调用失败', res); /* 调用失败时,再次调用 */ wx.writeBLECharacteristicValue({ deviceId: that.data.deviceId, serviceId: that.data.serviceId, characteristicId: that.data.characteristicsId, value: buffer, success: function(res) { console.log('第3次数据发送成功', res); wx.showToast({ title: '发送成功', icon: 'success', duration: 2000 }) }, fail: function(res) { console.log('第3次调用失败', res); } }); } }); } }); }, /** * 生命周期函数--监听页面卸载 */ onUnload: function() { var that = this; wx.closeBLEConnection({ deviceId: that.data.deviceId, success: function(res) { console.log('断开设备连接', res); } }); }, })

注意:代码中的UUID提取出的FFE0,FFE1是我自己的设备的,不同的蓝牙UUID也会不同,需要自己修改,这数据一般在自己使用蓝牙模块的介绍文档里给出



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有