小程序连接蓝牙打印机

1.openBluetoothAdapter初始化蓝牙适配器

wx.openBluetoothAdapter({success: function (res) {console.log('---初始化蓝牙适配器状态---');console.log(res);}
})


res结果,errcode = 10001,当前蓝牙适配器不可用,本机蓝牙没打开。

2.getBluetoothAdapterState获取蓝牙适配器状态,如果蓝牙未开或不可用,这里可用检测到

wx.getBluetoothAdapterState({success: function (res) {console.log('---获取本机蓝牙适配器状态---');console.log(res);//   res.discovering ? "在搜索。" : "未搜索。",//   res.available ? "可用。" : "不可用。",}
})


3.开始搜索蓝牙设备startBluetoothDevicesDiscovery() , 提示蓝牙搜索

wx.startBluetoothDevicesDiscovery({success: function (res) {that.setData({msg: "搜索设备" + JSON.stringify(res),})//监听蓝牙适配器状态wx.onBluetoothAdapterStateChange(function (res) {that.setData({sousuo: res.discovering ? "在搜索。" : "未搜索。",status: res.available ? "可用。" : "不可用。",})})}
})
4.获取已配对的蓝牙设备。
**此方法特别说明参数services(Array)是必填的,**但是官方示例中以及各种坑爹demo里从没见过有谁填写,但是不填写这个属性此方法无法获取到任何已配对设备。如果要调用此方法则是需要连接特定设备,并且知道该设备的一个主服务serviceId。如果未知可以先手动连接一次想要连接的设备,然后获取service列表,记录属性primary为true的值至少一个。

1) 无services

wx.getBluetoothDevices({success: function (res) {//是否有已连接设备wx.getConnectedBluetoothDevices({success: function (res) {console.log(JSON.stringify(res.devices));that.setData({connectedDeviceId: res.deviceId})}})that.setData({msg: "搜索设备" + JSON.stringify(res.devices),devices: res.devices,})//监听蓝牙适配器状态wx.onBluetoothAdapterStateChange(function (res) {that.setData({sousuo: res.discovering ? "在搜索。" : "未搜索。",status: res.available ? "可用。" : "不可用。",})})}
})

2) 有services的demo

getConnectedBluetoothDevices: function () {var that = this;wx.getConnectedBluetoothDevices({services: [that.serviceId],success: function (res) {console.log("获取处于连接状态的设备", res);var devices = res['devices'], flag = false, index = 0, conDevList = [];devices.forEach(function (value, index, array) {if (value['name'].indexOf('FeiZhi') != -1) { // 如果存在包含FeiZhi字段的设备flag = true;index += 1;conDevList.push(value['deviceId']);that.deviceId = value['deviceId'];return;}});if (flag) { this.connectDeviceIndex = 0;that.loopConnect(conDevList);} else {if (!this.getConnectedTimer) {that.getConnectedTimer = setTimeout(function () {that.getConnectedBluetoothDevices();}, 5000);}},fail: function (err) {if (!this.getConnectedTimer) {that.getConnectedTimer = setTimeout(function () {that.getConnectedBluetoothDevices();}, 5000);}}});}

5.停止搜索周边设备。

  wx.stopBluetoothDevicesDiscovery({success: function (res) {that.setData({msg: "停止搜索周边设备" + "/" + JSON.stringify(res.errMsg),sousuo: res.discovering ? "在搜索。" : "未搜索。",status: res.available ? "可用。" : "不可用。",})}
})

6.获取连接设备的service服务

wx.getBLEDeviceServices({// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取deviceId: that.data.connectedDeviceId,success: function (res) {console.log('device services:', JSON.stringify(res.services));that.setData({services: res.services,msg: JSON.stringify(res.services),})}
})

7.获取连接设备的所有特征值 for循环获取不到值

lanya7: function () {var that = this;wx.getBLEDeviceCharacteristics({// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取deviceId: that.data.connectedDeviceId,// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取serviceId: that.data.services[0].uuid,success: function (res) {for (var i = 0; i < res.characteristics.length; i++) {if (res.characteristics[i].properties.notify) {console.log("11111111", that.data.services[0].uuid);console.log("22222222222222222", res.characteristics[i].uuid);that.setData({notifyServicweId: that.data.services[0].uuid,notifyCharacteristicsId: res.characteristics[i].uuid,})}if (res.characteristics[i].properties.write) {that.setData({writeServicweId: that.data.services[0].uuid,writeCharacteristicsId: res.characteristics[i].uuid,})} else if (res.characteristics[i].properties.read) {that.setData({readServicweId: that.data.services[0].uuid,readCharacteristicsId: res.characteristics[i].uuid,})}}console.log('device getBLEDeviceCharacteristics:', res.characteristics);that.setData({msg: JSON.stringify(res.characteristics),})},fail: function () {console.log("fail");},complete: function () {console.log("complete");}})wx.getBLEDeviceCharacteristics({// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取deviceId: that.data.connectedDeviceId,// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取serviceId: that.data.services[1].uuid,success: function (res) {for (var i = 0; i < res.characteristics.length; i++) {if (res.characteristics[i].properties.notify) {that.setData({notifyServicweId: that.data.services[1].uuid,notifyCharacteristicsId: res.characteristics[i].uuid,})}if (res.characteristics[i].properties.write) {that.setData({writeServicweId: that.data.services[1].uuid,writeCharacteristicsId: res.characteristics[i].uuid,})} else if (res.characteristics[i].properties.read) {that.setData({readServicweId: that.data.services[1].uuid,readCharacteristicsId: res.characteristics[i].uuid,})}}console.log('device getBLEDeviceCharacteristics1:', res.characteristics);that.setData({msg1: JSON.stringify(res.characteristics),})},fail: function () {console.log("fail1");},complete: function () {console.log("complete1");}})},
  1. 发送

         // 这里的回调可以获取到 write 导致的特征值改变wx.onBLECharacteristicValueChange(function (characteristic) {console.log('characteristic value changed:1', characteristic)})var buf = new ArrayBuffer(16)var dataView = new DataView(buf)wx.request({url: getEncrypt,success: function (data) {var arr = data.data.data.split(",");console.log(arr);for (var i = 0; i < arr.length; i++) {dataView.setInt8(i, arr[i]);}console.log('str', buf);console.log("writeServicweId", that.data.writeServicweId);console.log("writeCharacteristicsId", that.data.writeCharacteristicsId);wx.writeBLECharacteristicValue({// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取deviceId: that.data.connectedDeviceId,// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取serviceId: that.data.writeServicweId,// 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取characteristicId: that.data.writeCharacteristicsId,// 这里的value是ArrayBuffer类型value: buf,success: function (res) {console.log('writeBLECharacteristicValue success', res.errMsg)}})}})
    

9.启用低功耗蓝牙设备特征值变化时的 notify 功能

wx.notifyBLECharacteristicValueChange({state: true, // 启用 notify 功能// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取deviceId: that.data.connectedDeviceId,// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取serviceId: that.data.notifyServicweId,// 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取characteristicId: that.data.notifyCharacteristicsId,success: function (res) {console.log('notifyBLECharacteristicValueChange success', res.errMsg)},fail: function () {console.log('shibai');console.log(that.data.notifyServicweId);console.log(that.data.notifyCharacteristicsId);},
})

10.接收消息

// 必须在这里的回调才能获取
wx.onBLECharacteristicValueChange(function (characteristic) {let hex = Array.prototype.map.call(new Uint8Array(characteristic.value), x => ('00' + x.toString(16)).slice(-2)).join('');console.log(hex)wx.request({url: getDecrypt,data: { hexString: hex },method: "POST",header: {'content-type': 'application/x-www-form-urlencoded'},success: function (data) {//console.log(data)var res = data.data.data;that.setData({jieshou: res,})}})
})
console.log(that.data.readServicweId);
console.log(that.data.readCharacteristicsId);
wx.readBLECharacteristicValue({// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取deviceId: that.data.connectedDeviceId,// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取serviceId: that.data.readServicweId,// 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取characteristicId: that.data.readCharacteristicsId,success: function (res) {console.log('readBLECharacteristicValue:', res.errMsg);}
})

小程序连接蓝牙打印机相关推荐

  1. 微信小程序连接蓝牙打印机打印图片示例

    微信小程序连接蓝牙打印机示例 完整的代码示例请点击看github 小程序连接蓝牙打印机打印文本与二维码等示例在 github 上都能找到一些,唯独打印图片这个案例几乎没有.希望能帮助到有打印图片需求的 ...

  2. uniapp微信小程序连接蓝牙打印机 打印文字、图片

    首先感谢几位的文章分享 https://blog.csdn.net/guairena/article/details/127941515 https://blog.csdn.net/qq_379700 ...

  3. uniapp 小程序连接蓝牙打印机打印条码

    搜索蓝牙 连接蓝牙 获取特征 存储特征 import {mapState} from 'vuex';export default {data() {return {isSearching: false ...

  4. 微信小程序连接蓝牙ble教程(目录)

    微信小程序连接蓝牙教程(目录) 1.新建小程序项目 2.初始化蓝牙适配器(wx.openBluetoothAdapter) 3.获取蓝牙适配器状态(getBluetoothAdapterState) ...

  5. 微信小程序连接蓝牙 并分包发送 接收数据完整版

    微信小程序连接蓝牙并分包发送接收数据 初始化蓝牙 初始化蓝牙设备 搜索蓝牙设备 连接蓝牙设备 获取蓝牙设备所有service(支持读写的) 向蓝牙发送数据 断开蓝牙 停止搜索蓝牙 转16进制 Arra ...

  6. 小程序蓝牙打印机CPCL图片打印问题梳理

    小程序蓝牙打印机CPCL指令图片打印问题梳理 本文以汉印HM-T3便携式打印机示例讲解 手册中要求需要用ASCII码十六进制字符下发命令,源引自 有赞零售小票打印跨平台解决方案 图片处理 由于 JS ...

  7. 微信小程序连接蓝牙模块发送数据的详解以及封装版

    连接蓝牙 前言 详解 小程序链接蓝牙流程图 需要的数据 api openBluetoothAdapter:打开蓝牙适配器 getBluetoothAdapterState:查看蓝牙适配器状态 star ...

  8. 微信小程序连接蓝牙电子秤

    本文后续已更新,解决了第一本存在的跳转页面过多问题: https://blog.csdn.net/weixin_59128282/article/details/119867721 背景:小程序需要对 ...

  9. 微信小程序连接蓝牙工具类支持IOS和安卓

    // 初始化蓝牙(判断用户有没有开蓝牙) --> 搜索蓝牙 --> 连接蓝牙 --> 根据连接的deviceId获取服务serviceUUID --> // 根据服务servi ...

最新文章

  1. linux shell编程(三) if 和 for
  2. js获取当前日期_vue项目中获取前后N天日期
  3. 03-CoreData原理
  4. 蔡成功贷款警示:没事别玩高利贷
  5. Servlet 应用程序事件、监听器
  6. 洛谷P3392 涂国旗
  7. C++ 序列化和反序列化学习
  8. 电脑机器人_【头条】厚积薄发!卡达电脑智能机器人之纸箱码垛机赋能智能工厂...
  9. GIS案例练习-----------第八天
  10. Linux内核学习(二)编写最简单的字符设备驱动
  11. 第二章 复杂的HTML解析(上)
  12. ANSYS ICEM CFD三维结构网格生成实例——汽车外流
  13. 高绩效团队-VUCA时代的五个管理策略《二》—代际管理
  14. 面试常问的16个C语言问题,你能答上来几个?
  15. 如何打造一支优秀的技术团队?
  16. 使用Visio画UML模型
  17. silverlight beet - 动态设置Clip
  18. redis incr mysql_redis命令_INCR
  19. 布袋除尘器--预喷涂
  20. 转载批处理之FOR语句祥解

热门文章

  1. 2020年金属非金属矿山(地下矿山)主要负责人考试题库及金属非金属矿山(地下矿山)主要负责人考试申请表
  2. 手机运行《仙剑奇侠传》耶~
  3. 中国Spark技术峰会(上):Spark与生态圈中组件结合实战
  4. 时间日期选择器(bootstrap-datepicker)
  5. Python基础知识必知道!!!
  6. 城市一卡通系统走进线上支付“芯”时代
  7. golang excel数据写入到sqlite3中--v3版本
  8. 学会自己测天气之卜卦应期口诀-----05
  9. 《python数据分析》
  10. 关于投篮的数学建模模型_投篮问题的数学建模[共10页]