经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » HTML5 » 查看文章
uni-app开发经验分享十五: uni-app 蓝牙打印功能
来源:cnblogs  作者:林恒  时间:2020/12/14 17:19:52  对本文有异议

最近在做uni-app项目时,遇到了需要蓝牙打印文件的功能需要制作,在网上找到了一个教程,这里分享给大家。

引入tsc.js

简单得引入到自己所需要得页面中去,本次我们只要到了标签模式,他同时还有账单模式可以选择。

  1. // 蓝牙打印 指令和转码
  2. var tsc = require('@components/gprint/tsc.js')

蓝牙适配前期工作

首先我们需要先初始化蓝牙模块,在进行搜索蓝牙。在监听到附近蓝牙设备时,记录他的名称和deviceId。

  1. onBlue(e) {
  2. uni.openBluetoothAdapter({
  3. success(res) {
  4. //监听寻找到新设备的事件
  5. that.findDevice()
  6. //监听本机蓝牙适配器状态变化事件
  7. that.onStatus()
  8. }
  9. })
  1. findDevice(){
  2. console.log("监听寻找到新设备的事件---------------")
  3. //监听寻找到新设备的事件
  4. uni.onBluetoothDeviceFound(function(devices) {
  5. const {name,deviceId} = devices[0];
  6. if(name == "未知设备")return;
  7. if(!name || !name.length){
  8. that.devices.push({
  9. name: name,
  10. deviceId: deviceId,
  11. services: []
  12. })
  13. }
  14. that.devices.forEach(e=>{
  15. if(that.devicesList){
  16. let b = true;
  17. that.devicesList.forEach(e1=>{
  18. if(e.name == e1.name){
  19. b = false;
  20. }
  21. });
  22. if(b)that.devicesList.push(e);
  23. }else{
  24. that.devicesList.push(e);
  25. }
  26. });
  27. }
  28. }
  1. onStatus(){
  2. uni.getBluetoothAdapterState({
  3. success: function(res) {
  4. //本机蓝牙开启时
  5. if (res.available) {
  6. //如在正在搜索设备,则停止搜索
  7. if (res.discovering) {
  8. uni.stopBluetoothDevicesDiscovery()
  9. }
  10. //搜索蓝牙
  11. //开始搜寻附近的蓝牙外围设备
  12. uni.startBluetoothDevicesDiscovery()
  13. } else {
  14. console.log('本机蓝牙不可用')
  15. }
  16. },
  17. })
  18. }

连接蓝牙

搜索出附近蓝牙设备后,获取蓝牙设备的deviceId传入createBLEConnection方法中。在连接蓝牙设备时,我们需要注意的是保证尽量成对的调用 createBLEConnection 和 closeBLEConnection 接口。安卓如果多次调用 createBLEConnection 创建连接,有可能导致系统持有同一设备多个连接的实例,导致调用 closeBLEConnection 的时候并不能真正的断开与设备的连接。
我们将连接成功的蓝牙信息存到currDev中,以便直接连接,无需进行搜索操作。

  1. onLink(item){
  2. const {deviceId} = item;
  3. console.log("连接蓝牙---------------" + deviceId);
  4. //连接低功耗蓝牙设备。
  5. uni.createBLEConnection({
  6. deviceId: deviceId,
  7. complete(res) {
  8. if (res.errMsg != "createBLEConnection:ok") return
  9. //连接设备时,需断开本机连接设备
  10. uni.closeBLEConnection({
  11. deviceId
  12. })
  13. that.connId = deviceId;
  14. that.currDev = item
  15. setTimeout(()=> {
  16. //获取蓝牙设备所有服务(service)
  17. that.getBLEServices(deviceId)
  18. }, 2000)
  19. }
  20. //连接成功 关闭搜索
  21. uni.stopBluetoothDevicesDiscovery()
  22. })
  23. }
  1. getBLEServices(deviceId) {
  2. uni.getBLEDeviceServices({
  3. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  4. deviceId: deviceId,
  5. complete(res) {
  6. const {services} = res;
  7. services.forEach(item=>{
  8. const {uuid} = item;
  9. uni.getBLEDeviceCharacteristics({
  10. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  11. deviceId: deviceId,
  12. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  13. serviceId: uuid,
  14. success(res) {
  15. const {characteristics} = res;
  16. for(let block of characteristics){
  17. if(!block.properties.write)return
  18. for (let index in that.devices) {
  19. if (that.devices[index].deviceId == deviceId) {
  20. that.devices[index].services.push({
  21. serviceId: uuid,
  22. characteristicId: block.uuid,
  23. })
  24. break
  25. }
  26. }
  27. }
  28. uni.setStorage({
  29. key: 'currDev',
  30. data: that.devices,
  31. });
  32. }
  33. })
  34. })
  35. }
  36. })
  37. } 

打印

打印格式需要自己根据当前设备的格式来进行设置打印。本章用到的是tsc.js中的form格式。

  1. onPrint(){
  2. if(this.currDev.length == 0){
  3. uni.showToast({
  4. title: '请先连接蓝牙打印机',
  5. duration: 2000
  6. });
  7. return
  8. }
  9. //标签模式
  10. const {deviceId} = this.currDev;
  11. const {serviceId,characteristicId} = this.currDev.services[0];
  12. var command = tsc.jpPrinter.createNew();
  13. //DaYin这个字段存放我们需要打印的数据
  14. let DaYin = JSON.parse(JSON.stringify(this.rowsList));
  15. let Customer = JSON.stringify(this.Customer);
  16. //打印格式需要根据打印机的特定格式来。在tsc文件中修改格式。
  17. DaYin.forEach(e=>{
  18. command.form(e.ReceSheetNo,`客 户:${Customer}`,`匹 数:${e.Rolls}`,`坯布品名:${e.GrayID}`,`进仓编号:${e.LotNo}`,`坯布类型:${e.GrayTypeName}`)
  19. command.setPagePrint()
  20. })
  21. //转码处理
  22. this.senBlData(deviceId, serviceId, characteristicId,command.getData())
  23. }
  1. senBlData(deviceId, serviceId, characteristicId,uint8Array) {
  2. let uint8Buf = Array.from(uint8Array);
  3. function split_array(datas,size){
  4. let result = {};
  5. let j = 0
  6. for (var i = 0; i < datas.length; i += size) {
  7. result[j] = datas.slice(i, i + size)
  8. j++
  9. }
  10. return result
  11. }
  12. let sendloop = split_array(uint8Buf, 20);
  13. function realWriteData(sendloop, i) {
  14. let data = sendloop[i]
  15. if(typeof(data) == "undefined"){
  16. return
  17. }
  18. let buffer = new ArrayBuffer(data.length)
  19. let dataView = new DataView(buffer)
  20. uni.writeBLECharacteristicValue({
  21. deviceId,
  22. serviceId,
  23. characteristicId,
  24. value: buffer,
  25. success(res) {
  26. realWriteData(sendloop, i + 1);
  27. }
  28. })
  29. }
  30. let i = 0;
  31. realWriteData(sendloop, i);
  32. },

form条码格式

  1. // 条形码和文字合成打印
  2. jpPrinter.form = function (content,text1,text2,text3,text4) {
  3. data = header + "LEFT" + "\r\n" + "GAR-SENSE" + "\r\n" + barcodeText +
  4. "BARCODE " + 128 + " " + 1 + " " + 1 + " " + 125 + " " + 125 + " " + 0 + " " +
  5. content + "\r\n" +
  6. "TEXT " + " " + 12 + " " + 0 + " " + 125 + " " + 180 + " " + text1 + "\r\n" +
  7. "TEXT " + " " + 12 + " " + 2 + " " + 125 + " " + 210 + " " + text2 + "\r\n" +
  8. "TEXT " + " " + 12 + " " + 2 + " " + 125 + " " + 240 + " " + text3 + "\r\n" +
  9. "TEXT " + " " + 12 + " " + 2 + " " + 125 + " " + 270 + " " + text4 + "\r\n" +
  10. "FORM" + "\r\n" ;
  11. jpPrinter.addCommand(data)
  12. };

转载于:https://blog.csdn.net/zhanleibo/article/details/103035645

原文链接:http://www.cnblogs.com/smileZAZ/p/14110915.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号