微信小程序连接蓝牙

news/2024/10/19 3:29:13/

准备工作:

1:需要一个蓝牙板子和串口软件(卖蓝牙板子的商家会提供),手机上需要蓝牙调试助手(为了测试蓝牙是否正常连接)
2:蓝牙板通过usb插入到电脑端
3:安装好串口调试工具并打开连接上蓝牙
4:打开手机蓝牙调试助手,连接上后进行读写操作,确认已正常连接通信
准备工作完结!

直接上代码

第一步:初始化蓝牙设备, onShow里面调用或者onLoad里面调用都可,看你的需求场景

initBlue () {wx.openBluetoothAdapter({success: (res) => {wx.showToast({title: '正在搜索蓝牙设备...',icon: 'loading'})this.findBlue()},fail: (error) => {wx.showToast({title: '请开启蓝牙',icon: 'fails',duration: 1000})this.setData({ isTrigger: false })}})},

第二步:搜索蓝牙设备

findBlue(){wx.startBluetoothDevicesDiscovery({allowDuplicatesKey: true,interval: 0,powerLevel: 'high',success: (res) => {this.getBlue()//3.0},complete: () => {this.setData({ isTrigger: false })}})},

第三步:拿到需要的蓝牙设备进行列表渲染

getBlue () {wx.onBluetoothDeviceFound((devices) => {if (devices.devices[0].name.includes('demo')) {console.log(devices.devices[0].name, 'devices.devices[0].name')// 添加有名字的蓝牙设备,并去重处理this.setData({ bluetoolList: [...this.data.bluetoolList, devices.devices[0]].filter((obj, index, self) =>index === self.findIndex((t) => (t.name === obj.name))) })wx.stopBluetoothDevicesDiscovery() // 停止搜索}})},

第四步:点击某个蓝牙设备进行连接

connetBluetooth(evt) {wx.showToast({title: '正在连接...',icon: 'loading'})console.log(evt.currentTarget.dataset, '=========')const { row } = evt.currentTarget.datasetthis.setData({ currentDeviceid: row.deviceId })wx.setStorageSync('deviceId', row.deviceId)wx.createBLEConnection({deviceId: row.deviceId,timeout: 5000,success: (res) => {console.log('connet success: ', res)wx.stopBluetoothDevicesDiscovery()this.getServiceUUid() // 连接的收就获取服务ID 用于后面获取特征值id(特征值id可以用来进行读、写、监听蓝牙操作)},fail: (error) => {console.log('createBLEConnection error: ', error)if (error.errCode === -1) { // 表示已经连接, 直接跳转到对应页面即可wx.navigateTo({ url: '/pages/xxx/xxx' })} else {wx.showToast({title: '连接失败',icon: 'error',timeout: 500,complete: () => {}})}}})},

第五步:获取服务id,不同蓝牙设备,不同机型,获取的服务值不一样,根据你自己的蓝牙板子和机型来获取,一定要获取准确,错了就读写不了蓝牙设备

getServiceUUid() {console.log('=====================getServiceUUid=======================')let _this = thiswx.getBLEDeviceServices({// 这里的 deviceid 需要已经通过 createBLEConnection 与对应设备建立链接deviceId: _this.data.currentDeviceid,success: async (res) => {console.log('res.services: ', res.services)if (wx.getStorageSync('platform') === 'ios') {// readawait _this.setData({ readServiceId: res.services[0].uuid })await wx.setStorageSync('readServiceId', res.services[0].uuid)// write trueawait _this.setData({ writeServiceId: res.services[1].uuid })await wx.setStorageSync('writeServiceId', res.services[1].uuid)// notifyawait _this.setData({ notifyServiceId: res.services[1].uuid })await wx.setStorageSync('notifyServiceId', res.services[1].uuid)await _this.getReadCharacteIdIos()await _this.getWriteCharacteIdIos()} else {// read true// _this.setData({ readServiceId: res.services[0].uuid })await _this.setData({ readServiceId: res.services[2].uuid })await wx.setStorageSync('readServiceId', res.services[2].uuid)// 第一个 write: true;第二个 notify: trueawait _this.setData({ writeServiceId: res.services[1].uuid })await wx.setStorageSync('writeServiceId', res.services[1].uuid)await _this.setData({ notifyServiceId: res.services[1].uuid })await wx.setStorageSync('notifyServiceId', res.services[1].uuid)// read true// await _this.setData({ readServiceId: res.services[2].uuid })// await wx.setStorageSync('readServiceId', res.services[2].uuid)// indicate 安卓端监听不到蓝牙发送过来的回调// await _this.setData({ notifyServiceId: res.services[3].uuid })// await wx.setStorageSync('notifyServiceId', res.services[3].uuid)await _this.getReadCharacteId()// await _this.getNotifyCharacteId()await _this.getWriteCharacteId()}}})},

第六步:获取特征值id,并保证在了本地(也可以挂在全局,方便另一个页面获取),跳转到对应页面,这里你可以封装一下,我这里做demo就没封装,五个方法,分别获取IOS端和安卓端的对应读写和监听的特征值,一定要根据蓝牙的板子获取对应正确的特征值(为true的才能进行后面的连接和读写操作),慢慢调试

 // readServiceIdgetReadCharacteId() {let _this = thiswx.getBLEDeviceCharacteristics({deviceId: _this.data.currentDeviceid,serviceId: _this.data.readServiceId,success: (res) => {console.log('res.haracteristics read: ', res.characteristics)for (let i = 0; i < res.characteristics.length; i++) {let characteristic = res.characteristics[i]// 检查特征值是否具有 read 属性为 falseif (characteristic.properties.read) {// 当找到 read 属性为 false 的特征值时,设置 writeId 为该特征值的 UUID_this.setData({ characteReadId: characteristic.uuid })wx.setStorageSync('characteReadId', characteristic.uuid)// break // 找到符合条件的特征值后,结束循环}}},fail: (error) => {console.log('get read CharacteId error: ', error)}})},// use bluetool deviceId and uuid get characteId (特征值) writeServiceIdgetWriteCharacteId () {let _this = thiswx.getBLEDeviceCharacteristics({deviceId: _this.data.currentDeviceid,serviceId: _this.data.writeServiceId,success: (res) => {// 遍历特征值列表console.log('res.characteristics write : ', res.characteristics)for (let i = 0; i < res.characteristics.length; i++) {let characteristic = res.characteristics[i]// 检查特征值是否具有 read 属性为 falseif (characteristic.properties.write) {// 当找到 read 属性为 false 的特征值时,设置 writeId 为该特征值的 UUID_this.setData({characteWriteId: characteristic.uuid})wx.setStorageSync('characteWriteId', characteristic.uuid)// break // 找到符合条件的特征值后,结束循环}if (characteristic.properties.notify) {_this.setData({ characteNotifyId: characteristic.uuid })wx.setStorageSync('characteNotifyId', characteristic.uuid)}}console.log('currentDeviceid: ', _this.data.currentDeviceid)console.log('----------------------read--------------------')console.log('readServiceId: ', _this.data.readServiceId)console.log('characteReadId: ', _this.data.characteReadId)console.log('----------------------write--------------------')console.log('writeServiceId: ', _this.data.writeServiceId)console.log('characteWriteId: ', _this.data.characteWriteId)wx.navigateTo({url: '/pages/action/action'})wx.hideToast()},fail: function (error) {console.log("error reason: ", error)}})},// notifyServiceIdgetNotifyCharacteId() {let _this = thiswx.getBLEDeviceCharacteristics({deviceId: _this.data.currentDeviceid,serviceId: _this.data.notifyServiceId,success: (res) => {console.log('res.characteristics notify: ', res.characteristics)for (let i = 0; i < res.characteristics.length; i++) {let characteristic = res.characteristics[i]// 检查特征值是否具有 read 属性为 falseif (characteristic.properties.indicate) {// 当找到 read 属性为 false 的特征值时,设置 writeId 为该特征值的 UUID_this.setData({ characteNotifyId: characteristic.uuid })wx.setStorageSync('characteNotifyId', characteristic.uuid)break // 找到符合条件的特征值后,结束循环}}},fail: (error) => {console.log('get notify CharacteId error: ', error)}})},// ios read characteIdgetReadCharacteIdIos() {wx.getBLEDeviceCharacteristics({deviceId: this.data.currentDeviceid,serviceId: this.data.readServiceId,success: (res) => {// 遍历特征值列表console.log('res.characteristics read : ', res.characteristics)for (let i = 0; i < res.characteristics.length; i++) {let characteristic = res.characteristics[i]// 检查特征值是否具有 read 属性为 falseif (characteristic.properties.read) {// 当找到 read 属性为 false 的特征值时,设置 writeId 为该特征值的 UUIDthis.setData({characteReadId: characteristic.uuid})wx.setStorageSync('characteReadId', characteristic.uuid)break // 找到符合条件的特征值后,结束循环}}}})},// ios write characteIdgetWriteCharacteIdIos() {wx.getBLEDeviceCharacteristics({deviceId: this.data.currentDeviceid,serviceId: this.data.writeServiceId,success: (res) => {// 遍历特征值列表console.log('res.characteristics write : ', res.characteristics)for (let i = 0; i < res.characteristics.length; i++) {let characteristic = res.characteristics[i]// 检查特征值是否具有 read 属性为 falseif (characteristic.properties.write) {// 当找到 read 属性为 false 的特征值时,设置 writeId 为该特征值的 UUIDthis.setData({characteWriteId: characteristic.uuid})wx.setStorageSync('characteWriteId', characteristic.uuid)// break // 找到符合条件的特征值后,结束循环}if (characteristic.properties.notify) {// 当找到 read 属性为 false 的特征值时,设置 writeId 为该特征值的 UUIDthis.setData({characteNotifyId: characteristic.uuid})wx.setStorageSync('characteNotifyId', characteristic.uuid)}wx.navigateTo({url: '/pages/xxx/xxx'})wx.hideToast()}}})},

第七步:这时候已经跳转到对应页面了,在页面onLoad里开启监听,后面蓝牙发送设备到小程序可以实时监听到值

async onLoad(options) {// 监听蓝牙发送到小程序的数据await wx.onBLECharacteristicValueChange((res) => {console.log('characteristicis: ', res.value)console.log('转换后的值: ', this.ab2hex(res.value))this.setData({ fromBlueToolsData: this.ab2hex(res.value) })// this.setData({ fromBlueToolsData: res.value })})await wx.readBLECharacteristicValue({deviceId: wx.getStorageSync('deviceId'),serviceId: wx.getStorageSync('readServiceId'),characteristicId: wx.getStorageSync('characteReadId'),success: (res) => {console.log('read data is: ', res)},fail: (error) => {console.log('read error, error message is: ', error)}})await wx.notifyBLECharacteristicValueChange({state: true,type: 'notification', // 根据你蓝牙的特征值来选择类型deviceId: wx.getStorageSync('deviceId'),serviceId: wx.getStorageSync('notifyServiceId'),characteristicId: wx.getStorageSync('characteNotifyId'),success: (res) => {console.log('notify success, success message is: ', res)},fail: (error) => {console.log('notify error, error message is: ', error)this.setData({ fromBlueToolsData: '' })}})},// 16 进制到 10进制ab2hex(buffer) {let hexArr = Array.prototype.map.call(new Uint8Array(buffer),function(bit) {return ('00' + bit.toString(16)).slice(-2)})return hexArr.join('');},

第八步:测试读写,看蓝牙串口与小程序是否有通信

// 写入数据
writeTap() {console.log('-----------------start write action----------------------------')console.log('inputValue: ', this.data.inputValue)let data = this.data.inputValueif (data.length === 0) {wx.showModal({ title: '提示', content: '请输入要发送的数据' })return}wx.writeBLECharacteristicValue({deviceId: wx.getStorageSync('deviceId'),serviceId: wx.getStorageSync('writeServiceId'),characteristicId: wx.getStorageSync('characteWriteId'),value: this.string2buffer(data), // 数据转换发送success (res) {console.log('writeBLECharacteristicValue success', res)this.setData({ inputValue: '' })},fail: (error) => {console.log('write error, error message is: ', error)}})},string2buffer(hexString) {hexString = hexString.replace(/^0x/, '');const bytes = new Uint8Array(hexString.length / 2);for (let i = 0; i < hexString.length; i += 2) {bytes[i / 2] = parseInt(hexString.substring(i, i + 2), 16);}console.log('写入的buffer:', bytes.buffer)return bytes.buffer;},// input输入值onWriteInput(evt) {const { value } = evt.detailthis.setData({ inputValue: value })},

最终测试后在ios与安卓上能够与蓝牙串口进行正常通信,完结!


http://www.ppmy.cn/news/1437714.html

相关文章

VUE 打包后 动态修改 后台服务器地址

使用的是第三方 continew-admin 项目 在 continew-admin-ui 项目中 添加 config.json 到public 目录下 {"baseURL": "http://localhost:8000" } 在 request.ts 文件中 async function fetchConfig() {const response await fetch(/config.json);con…

STL Array、ForwardList源码剖析

STL Array、ForwardList源码剖析 参考文章: https://blog.csdn.net/weixin_45389639/article/details/121618243 array 源代码 template<typename _Tp,std::size_t _Nm> struct array {typedef _Tp value_type;typedef _Tp* pointer;typedef value_type* iterator;// Su…

js修改路由参数+vue——js基础

最近在写看板&#xff0c;要求执行某个操作后更改路由参数&#xff0c;方便用户保存地址以便于下次直接获取对应的数据。 比如&#xff1a;原地址&#xff1a;http://localhost:4200/tvType/out 执行某个操作后&#xff0c;地址变更为&#xff1a;http://localhost:4200/tvTyp…

React复习笔记

基础语法 创建项目 借助脚手架&#xff0c;新建一个React项目(可以使用vite或者cra&#xff0c;这里使用cra) npx create-react-app 项目名 create-react-app是React脚手架的名称 启动项目 npm start 或者 yarn start src是源文件index.js相当于Vue的main.js文件。整个…

MVC4自带的JS、CSS优化功能

方法: Application_Start事件中 或 类 BundleConfig中的RegisterBundles方法中 加入代码: BundleTable.EnableOptimizations true;//是否启用优化 说明: 所说的JS、CSS优化&#xff1a; 缓存&#xff1a;首次访问站点&#xff0c;JS和CSS文件会下载到浏览器缓存中&#…

commvault学习(6):备份oracle(包括oracle的安装)

1.环境 CS、MA&#xff1a;一台windows server2012 客户端&#xff1a;2台安装了oracle11g的windows server2008 1.1 windows server2008安装oracle11g &#xff08;1&#xff09;右击安装包内的setup&#xff0c;以管理员方式运行 &#xff08;2&#xff09;取消勾选接收安…

第 2 篇 : Netty客户端之间互发消息和服务端广播

1. 服务端改造 1.1 修改 Message类 package com.hahashou.netty.server.config;import com.alibaba.fastjson.JSON; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.util.CharsetUtil; import lombok.Data;/*** description:* author: 哼…

OpenStack云计算(十)——OpenStack虚拟机实例管理,增加一个计算节点并进行实例冷迁移,增加一个计算节点的步骤,实例冷迁移的操作方法

项目实训一 本实训任务对实验环境要求较高&#xff0c;而且过程比较复杂&#xff0c;涉及的步骤非常多&#xff0c;有一定难度&#xff0c;可根据需要选做。可以考虑改为直接观看相关的微课视频 【实训题目】 增加一个计算节点并进行实例冷迁移 【实训目的】 熟悉增加一个…