蓝牙打印

蓝牙打印热敏纸是我工作第一个小程序项目时用到的,当时啃下这块硬骨头是真的感觉好难,这篇文章我将监听蓝牙特征这部分删减掉了,减少难度,而且我个人使用中感觉也不是那么必须。
微信开发者文档-蓝牙

app.js

// 全局变量,蓝牙对象模板,但是在初始化蓝牙之前需要删除数据。globalData: {btDevices: [{id: 1,rssi: -40,  // 信号name: 'BT626',  // 蓝牙设备名称devId: '0',   // 蓝牙序列idimg: '/imgs/scan/2.png',  // 蓝牙图标},],}

首先:判断是Android还是iOS

    if(app.globalData.platform == 'ios'){console.log('ios bt')}else{console.log('android bt')}

第一阶段

第一步:初始化蓝牙设备

wx.openBluetoothAdapter({success: function (res) {},fail:function(res){console.log("蓝牙未开启!");}
})

第二步:搜索附近蓝牙设备

wx.startBluetoothDevicesDiscovery({success: function (res) {})

第三步:监听寻找到新设备的事件

wx.onBluetoothDeviceFound(function(devices) {})

第四步:获取已搜素到的蓝牙设备列表

wx.getBluetoothDevices({success: function (res) {var ln = 0;console.log(res,that.data.btDevices.length)if(that.data.btDevices.length != null)ln = that.data.btDevices.lengthfor (var i = ln; i < res.devices.length; ++i)   {var rssi_level_img;if(res.devices[i].RSSI > 0 || res.devices[i].name == '未知设备'){continue;}if(res.devices[i].RSSI > -40)  {rssi_level_img = '/imgs/scan/5.png'}    else if(res.devices[i].RSSI > -50){rssi_level_img = '/imgs/scan/4.png'}   else if(res.devices[i].RSSI > -60){rssi_level_img = '/imgs/scan/3.png'}   else if(res.devices[i].RSSI > -70){rssi_level_img = '/imgs/scan/2.png'}else{rssi_level_img = '/imgs/scan/1.png'}var newBtDevice = [{rssi : res.devices[i].RSSI,name : res.devices[i].name,devId : res.devices[i].deviceId,img : rssi_level_img,}];for (var k = 0; k < that.data.btDevices.length; ++k){if(res.devices[i].deviceId == that.data.btDevices[k].devId){that.data.btDevices.splice(k,1);break;}}that.data.btDevices = that.data.btDevices.concat(newBtDevice)}that.setData({btDevices : that.data.btDevices});app.globalData.btDevices = that.data.btDevices}})

下拉刷新

 // 下拉清空记录,并重新搜索onPullDownRefresh: function() {var that = this//停止当前页面下拉刷新wx.stopPullDownRefresh()wx.stopBluetoothDevicesDiscovery({success: function (res) {//关闭蓝牙模块wx.closeBluetoothAdapter({success: function (res) {console.log("关闭蓝牙模块")var num = that.data.btDevices.lengththat.data.btDevices.splice(0,num)that.setData({btDevices : that.data.btDevices});    //蓝牙模块初始化       wx.openBluetoothAdapter({success: function (res) {console.log("蓝牙模块初始化")wx.startBluetoothDevicesDiscovery({//services: ['FFF0'],success: function (res) {console.log("搜索附近蓝牙设备")}})},fail:function(res){console.log("蓝牙未开启!下拉!");}})}})}})},

第二阶段

这是蓝牙打印最终重要的三个数据。

 // 蓝牙设备iddevId: openid.devId,// 蓝牙打印设备服务idwriteServiceId: '', //蓝牙打印设备特征值idwriteCharacteristicId: '',// 蓝牙打印设备服务序列writeServiceSearchIndex: 0,

第五步:连接蓝牙设备

 wx.createBLEConnection({// 这里的 deviceId需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取,这个devId是你点击选择连接的蓝牙deviceId: app.globalData.btDevices[i].devId,});

第六步: 获取蓝牙服务

 wx.getBLEDeviceServices({deviceId: app.globalData.btDevices[i].devId,})

第七步: 获取蓝牙设备某个服务中所有特征值

 wx.getBLEDeviceCharacteristics({// 这里的 devId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取deviceId: app.globalData.btDevices[i].devId,// 这里的 writeServiceId 需要在 getBLEDeviceServices 接口中获取serviceId: that.data.services[that.data.writeServiceSearchIndex].uuidcomplete: function () {//递归调用自身直到找到write特征或遍历完所有特征that.seekFirstWriteCharacteristic()},success: function (res) {console.log('device getBLEDeviceCharacteristics:', res.characteristics)for (var n = 0; n < res.characteristics.length && that.data.writeCharacteristicId == 'invalid'; ++n) {if (res.characteristics[n].properties.write == true) {that.data.writeCharacteristicId = res.characteristics[n].uuid}}}})

第八步:打印

需要一个cpcl文件,要将服务器的值传到 utils/cpcl.js里去。通过字符串操作赋值。

 //将字符串转成arrayBufferbase64Str = gbToBase64.encode64(sendData)arrayBuffer = wx.base64ToArrayBuffer(base64Str);
 if (app.globalData.platform == 'ios') {wx.writeBLECharacteristicValue({// 这里的 devId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取deviceId: that.data.devId,// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取serviceId: that.data.writeServiceId,// 这里的 writeCharacteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取characteristicId: that.data.writeCharacteristicId,// 这里的value是ArrayBuffer类型value: arrayBuffer,success: function (res) {wx.showToast({title: '发送成功',icon: 'success',duration: 2000})that.setData({block:"block",block1: "none"})console.log('writeBLECharacteristicValue success', res.errMsg)},fail: function (res) {console.log('writeBLECharacteristicValue fail', res.errMsg)}})} else {let dataView1 = new DataView(arrayBuffer)var length = dataView1.byteLengthvar size = 100var package_count = Math.round(length / size + 0.5);this.writeFuction(that, arrayBuffer, package_count, size);}
 writeFuction(that, data, count, size) {let dataView_temp1 = new DataView(data);var packages = Math.round(dataView_temp1.byteLength / size + 0.5)var yushu = dataView_temp1.byteLength % sizelet buffer = nulllet dataView_temp2 = nullif (yushu != 0 && count == 1) {buffer = new ArrayBuffer(yushu)dataView_temp2 = new DataView(buffer)for (var i = 0; i < dataView_temp2.byteLength; i++) {dataView_temp2.setUint8(i, dataView_temp1.getUint8((packages - count) * size + i))}} else {buffer = new ArrayBuffer(size)dataView_temp2 = new DataView(buffer)for (var i = 0; i < dataView_temp2.byteLength; i++) {dataView_temp2.setUint8(i, dataView_temp1.getUint8((packages - count) * size + i))}}wx.writeBLECharacteristicValue({// 这里的 devId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取deviceId: that.data.devId,// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取serviceId: that.data.writeServiceId,// 这里的 writeCharacteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取characteristicId: that.data.writeCharacteristicId,// 这里的value是ArrayBuffer类型value: buffer,// value: arrayBuffer,success: function (res) {if (count != 0) {that.writeFuction(that, data, count, size)} else {wx.showToast({title: '发送成功',icon: 'success',duration: 2000})that.setData({block: "block",block1: "none"})}},complete: function (res) {console.log(res);}})count--;return 0;},

关于cpcl介绍

exports.val = "! 0 200 200 1030 1\r\n" +"PAGE - WIDTH 576\r\n" +"UNDERLINE OFF\r\n" +"SETBOLD 1\r\n" +"SETMAG 2 2\r\n" +"T 20 0 0 25 HC慧驰速递\r\n" +"UNDERLINE OFF\r\n" +"SETBOLD 1\r\n" +"SETMAG 1 1\r\n" +"{df}"+"T 25 0 445 950 已验视\r\n" +"UNDERLINE OFF\r\n" +"SETBOLD 0\r\n" +"SETMAG 1 1\r\n" +"LINE 0 1000 566 1000 4\r\n" +// "FORM\r\n"+"PRINT\r\n";

微信小程序之蓝牙打印相关推荐

  1. 微信小程序/uni-app 蓝牙打印开发教程和常见问题总结【文末附源码】

    微信小程序/uni-app 蓝牙打印开发教程和常见问题总结[文末附源码] 文章目录 微信小程序/uni-app 蓝牙打印开发教程和常见问题总结[文末附源码] 1️⃣ 写在前面 2️⃣ 蓝牙连接流程 3 ...

  2. 微信小程序实现蓝牙打印(图片、二维码、文字)

    小程序连接蓝牙打印机实现打印功能 这真是一块超级难啃的骨头,我也是参考了很多文章才勉强理解,但是如果你啃动了,那你以后也有了拿得出手的资本了 完整DEMO地址可直接下载下来使用 搜索与连接蓝牙,这个我 ...

  3. 在HbuilderX中实现微信小程序下蓝牙连接打印机完整实战案例

    1.基础开发环境,所用到的 Api 以及实现的思路. 应用场景: 商家打印小票,小票包含顾客消费的商品明细信息以及末尾附上二维码,二维码供顾客扫码开票. HbuilderX开发工具: HBuilder ...

  4. 微信小程序 PDF下载打印

    在开发微信小程序时,需要打印生成的PDF,实现思路是:后端生成相应的PDF,微信小程序下载并打开. 但是微信小程序并不可以打印,所以需要借助其他APP比如:WPS,但是发现微信小程序down的PDF在 ...

  5. 微信小程序使用蓝牙连接设备流程

    微信小程序使用蓝牙连接设备流程 小程序使用蓝牙连接设备介绍 使用到的api 流程: 初始化蓝牙模块 wx.openBluetoothAdapter wx.openBluetoothAdapter({ ...

  6. 开源一个基于微信小程序的蓝牙室内定位软件(附下载链接)

    文章目录 1. 运行环境要求 2. 软件功能及程序说明 2.1 软件组成 2.2 主要功能 2.3 文件及函数功能说明 3. 软件设计及操作说明 4. 完整版代码获取 1. 运行环境要求 软件运行环境 ...

  7. 微信小程序实现蓝牙BLE(demo版)

    微信小程序实现蓝牙BLE(看文章最后一句话) 这是楼主在学校自己开发的用蓝牙小程序控制机械臂的(独立开发的). https://pan.baidu.com/s/1AmCW_ARhu--eapzd8Af ...

  8. 在微信小程序中有关于打印的小程序吗

    微信小程序是一种不用下载安装即可使用的应用,给大家的日常工作及生活带来了很大的的便利,使用微信小程序大家可以触手可及一些应用,再也不用下载安装应用占用手机空间内存了.那么,在微信小程序中有关于打印的小 ...

  9. 微信小程序的蓝牙使用

    微信小程序的蓝牙使用 1.蓝牙分类 蓝牙基础率/增强数据率 (BR/EDR) 常用在对数据传输速率有要求的设备上,如蓝牙音箱等 低功耗 (LE) 蓝牙 常用在低功耗.对续航有要求的设备上,智能穿戴设备 ...

  10. 微信小程序之蓝牙开发虚拟摇杆

    文章用于学习记录 文章目录 前言 一.App Inventor 二.uni-app 三.微信小程序 3.1 示例&应用 3.2 服务值与特征值 3.3 控制指令 3.4 测试 3.5 十六进制 ...

最新文章

  1. mysql5.5.9_mysql5.5.9字符集设置
  2. PHP扩展开发入门3------带参数的函数
  3. 字典、列表、元祖、字符串的综合(1)
  4. python数独游戏源代码_使用Python编写数独游戏自动出题程序
  5. Spring Cloud与Duddo比较(非原创)
  6. 云漫圈 | AR VR傻傻分不清楚。。。
  7. 接纳强迫,不要过于追求完美,允许自己慢下来,扎实的学习
  8. Django_ajax
  9. UltraEdit 所有快捷键 说明
  10. 判断浏览器是否支持html5
  11. vue在filters中使用sort()无限循环踩坑实践
  12. 测试计划一般包括哪些方面?
  13. hadoop文件存储位置_hadoop – HDFS存储数据的位置
  14. python第二十四天
  15. linux操作系下RAR的使用
  16. 对混淆矩阵、F1-Score
  17. c7200-adventerprisek9.124-9.T.bin
  18. Linux组播编程 组播发送和组播接收
  19. 【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(二)
  20. 玩回合制手游《问道》心得

热门文章

  1. Arduino 交通灯实验
  2. python普通滑块验证码破解初级版
  3. 漂亮的消息通知html邮件模板,好看的邮件模板?
  4. matlab希尔伯特变换,希尔伯特变换和傅里叶变换MATLAB仿真
  5. 自然语言处理NLP技术里程碑、知识结构、研究方向和机构导师(公号回复“NLP总结”下载彩标PDF典藏版资料)
  6. 《你的灯亮着吗》第一遍读后感
  7. TCP与UDP的区别
  8. 系统软件内部测试报告模板,软件系统测试报告模板-20210316091936.pdf-原创力文档...
  9. Sigmoid函数介绍
  10. 光环PMP 疑难点辨析