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

引入tsc.js

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

// 蓝牙打印 指令和转码

var tsc = require('@components/gprint/tsc.js')

蓝牙适配前期工作

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

onBlue(e) {

uni.openBluetoothAdapter({

success(res) {

//监听寻找到新设备的事件

that.findDevice()

//监听本机蓝牙适配器状态变化事件

that.onStatus()

}

})

findDevice(){

console.log("监听寻找到新设备的事件---------------")

//监听寻找到新设备的事件

uni.onBluetoothDeviceFound(function(devices) {

const {name,deviceId} = devices[0];

if(name == "未知设备")return;

if(!name || !name.length){

that.devices.push({

name: name,

deviceId: deviceId,

services: []

})

}

that.devices.forEach(e=>{

if(that.devicesList){

let b = true;

that.devicesList.forEach(e1=>{

if(e.name == e1.name){

b = false;

}

});

if(b)that.devicesList.push(e);

}else{

that.devicesList.push(e);

}

});

}

}

onStatus(){

uni.getBluetoothAdapterState({

success: function(res) {

//本机蓝牙开启时

if (res.available) {

//如在正在搜索设备,则停止搜索

if (res.discovering) {

uni.stopBluetoothDevicesDiscovery()

}

//搜索蓝牙

//开始搜寻附近的蓝牙外围设备

uni.startBluetoothDevicesDiscovery()

} else {

console.log('本机蓝牙不可用')

}

},

})

}

连接蓝牙

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

我们将连接成功的蓝牙信息存到currDev中,以便直接连接,无需进行搜索操作。

onLink(item){

const {deviceId} = item;

console.log("连接蓝牙---------------" + deviceId);

//连接低功耗蓝牙设备。

uni.createBLEConnection({

deviceId: deviceId,

complete(res) {

if (res.errMsg != "createBLEConnection:ok") return

//连接设备时,需断开本机连接设备

uni.closeBLEConnection({

deviceId

})

that.connId = deviceId;

that.currDev = item

setTimeout(()=> {

//获取蓝牙设备所有服务(service)

that.getBLEServices(deviceId)

}, 2000)

}

//连接成功 关闭搜索

uni.stopBluetoothDevicesDiscovery()

})

}

getBLEServices(deviceId) {

uni.getBLEDeviceServices({

// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接

deviceId: deviceId,

complete(res) {

const {services} = res;

services.forEach(item=>{

const {uuid} = item;

uni.getBLEDeviceCharacteristics({

// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接

deviceId: deviceId,

// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取

serviceId: uuid,

success(res) {

const {characteristics} = res;

for(let block of characteristics){

if(!block.properties.write)return

for (let index in that.devices) {

if (that.devices[index].deviceId == deviceId) {

that.devices[index].services.push({

serviceId: uuid,

characteristicId: block.uuid,

})

break

}

}

}

uni.setStorage({

key: 'currDev',

data: that.devices,

});

}

})

})

}

})

}

打印

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

onPrint(){

if(this.currDev.length == 0){

uni.showToast({

title: '请先连接蓝牙打印机',

duration: 2000

});

return

}

//标签模式

const {deviceId} = this.currDev;

const {serviceId,characteristicId} = this.currDev.services[0];

var command = tsc.jpPrinter.createNew();

//DaYin这个字段存放我们需要打印的数据

let DaYin = JSON.parse(JSON.stringify(this.rowsList));

let Customer = JSON.stringify(this.Customer);

//打印格式需要根据打印机的特定格式来。在tsc文件中修改格式。

DaYin.forEach(e=>{

command.form(e.ReceSheetNo,`客 户:${Customer}`,`匹 数:${e.Rolls}`,`坯布品名:${e.GrayID}`,`进仓编号:${e.LotNo}`,`坯布类型:${e.GrayTypeName}`)

command.setPagePrint()

})

//转码处理

this.senBlData(deviceId, serviceId, characteristicId,command.getData())

}

senBlData(deviceId, serviceId, characteristicId,uint8Array) {

let uint8Buf = Array.from(uint8Array);

function split_array(datas,size){

let result = {};

let j = 0

for (var i = 0; i < datas.length; i += size) {

result[j] = datas.slice(i, i + size)

j++

}

return result

}

let sendloop = split_array(uint8Buf, 20);

function realWriteData(sendloop, i) {

let data = sendloop[i]

if(typeof(data) == "undefined"){

return

}

let buffer = new ArrayBuffer(data.length)

let dataView = new DataView(buffer)

uni.writeBLECharacteristicValue({

deviceId,

serviceId,

characteristicId,

value: buffer,

success(res) {

realWriteData(sendloop, i + 1);

}

})

}

let i = 0;

realWriteData(sendloop, i);

},

form条码格式

// 条形码和文字合成打印

jpPrinter.form = function (content,text1,text2,text3,text4) {

data = header + "LEFT" + "\r\n" + "GAR-SENSE" + "\r\n" + barcodeText +

"BARCODE " + 128 + " " + 1 + " " + 1 + " " + 125 + " " + 125 + " " + 0 + " " +

content + "\r\n" +

"TEXT " + " " + 12 + " " + 0 + " " + 125 + " " + 180 + " " + text1 + "\r\n" +

"TEXT " + " " + 12 + " " + 2 + " " + 125 + " " + 210 + " " + text2 + "\r\n" +

"TEXT " + " " + 12 + " " + 2 + " " + 125 + " " + 240 + " " + text3 + "\r\n" +

"TEXT " + " " + 12 + " " + 2 + " " + 125 + " " + 270 + " " + text4 + "\r\n" +

"FORM" + "\r\n" ;

jpPrinter.addCommand(data)

};

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

html5 app如何连接打印机,uni-app开发经验分享十五: uni-app 蓝牙打印功能相关推荐

  1. android 蓝牙打印机(ESC/POS 热敏打印机),打印菜单小票和图片,对蓝牙配对和连接打印功能进行了封装,让你超快实现蓝牙打印功能

    BluetoothPrint 项目地址:liuGuiRong18/BluetoothPrint  简介:android 蓝牙打印机(ESC/POS 热敏打印机),打印菜单小票和图片,对蓝牙配对和连接打 ...

  2. uniapp微信小程序实现连接低功耗蓝牙打印功能

    微信小程序项目中有使用到蓝牙连接打印,参考官方文档做了一个参考笔记,这样使用的时候就按着步骤查看. uni-app蓝牙连接 蓝牙: 1.初始化蓝牙 uni.openBluetoothAdapter(O ...

  3. HP1020打印机加入域后,域用户无法使用HP1020域打印功能

    企业内部的PC电脑都加入了msits.com的域 ,现有一客户机表现为:域用户无法打印HP1020 打印机是设置在网内的一台已经加入域的机器上.打印机是HP1020的. 连接打印机的电脑本地打印没问题 ...

  4. wgt文件怎么安装到手机_uni-app开发经验分享十二: Android平台应用启动时读写手机存储、访问设备信息(如IMEI)等权限策略及提示信息...

    Android平台从6.0(API23)开始系统对权限的管理更加严格,所有涉及敏感权限都需要用户授权允许才能获取. 因此一些应用基础业务逻辑需要的权限会在应用启动时申请,并引导用户允许. 读写手机存储 ...

  5. pd15不能连接oracle11g,PowerDesigner15 使用时的十五个问题附解决方法

    15个问题列表: 一般常用的有CDM,PDM,UML建模,CDM可以转为PDM. 支持正向[生成数据库]和逆向工程[从数据库中生成],并直接关联到到数据库中,PDM可以直接和数据库进行关联,并将数据库 ...

  6. uni-app开发经验分享十九: uni-app对接微信小程序直播

    uni-app对接微信小程序直播 1.登录微信小程序后台-点击>设置->第三方设置->添加直播插件 2.添加直播组件后->点击<详情>      记录这两个参数直播 ...

  7. java 蓝牙打印_Android蓝牙打印(app源码)

    [实例简介]android 蓝牙打印机(ESC/POS 热敏打印机),打印菜单小票和图片,对蓝牙配对和连接打印功能进行了封装,让你超快实现蓝牙打印功能 [实例截图] [核心代码] package co ...

  8. PDA连接打印机使用说明

    一:功能说明: 1. 关于Demo 中使用到的打印机的说明:本Demo 使用PDA 手持终端连接蓝牙打印机,程序中使用蓝牙打印机SDK与指令集实现蓝牙打印功能:用户可根据自己使用的打印机的SDK 和指 ...

  9. uniapp APP实现通过蓝牙连接打印机打印

    蓝牙连接德佟打印机打印 1.导入插件: 在插件市场中搜索LPAPI,进入之后,点击右侧的"购买for云打包",选择目标项目,按照提示操作即可: 2. 配置插件: 用HBuilder ...

最新文章

  1. oracle 条件反转,Oracle反转倒置函数
  2. JavaScript try/catch/finally 语句
  3. Eclipse + Apache Axis2 发布RESTful WebService(一)基础知识
  4. perl学习之:编译、执行与内存关系(转)
  5. 不同电脑 命名管道_电脑键盘上的F1到F12,这些键都有哪些用处?用了5年总算明白了...
  6. c#日期转换周几_RPA经验:使用 selector 选择日期
  7. 串口通信工具android,Android串口通信工具
  8. 怎么管理Websphere应用服务器?
  9. K3救砖,梅林刷回官方
  10. imx6ull的boot, 之我的理解
  11. 数据链路层的主要功能
  12. **alon_MM DMA Interface for PCIe使用详解
  13. windows +caffe+python2.7或者python3.5编译 。
  14. 【力扣精选】3分钟拿下反转链表所有题型
  15. linux中rcf命名管道,RCF-进程间为C通讯
  16. 实现 Trie (前缀树)
  17. EI会议论文,第二届云计算、大数据与数字经济国际学术会议最终截稿倒计时10天
  18. 由学生学号查询该学生对应的课表--查询函数
  19. SSD与HDD如何混合组raid并永久挂载硬盘?
  20. (eblog)8、消息异步通知、细节调整

热门文章

  1. bzoj3362[Usaco2004 Feb]Navigation Nightmare 导航噩梦
  2. 使用kettle采集excel表格中的数据
  3. 主板芯片组的南桥和北桥
  4. 蓝桥杯-基础练习-字母图形(BASIC-3)
  5. Dreamweaver css浮动
  6. Nginx 防止被域名恶意解析的方法
  7. ubuntu提示“播放此文件需要MPEG-4-AAC解码器,H264解码器“
  8. 如何嗅探并下载ts并合成视频文件,m3u8文件处理
  9. xp系统如何连接服务器,xp系统怎么连接远程服务器
  10. java 集成MinIo