搜索蓝牙 连接蓝牙 获取特征 存储特征

 import {mapState} from 'vuex';export default {data() {return {isSearching: false, //是否正在搜索中list: [],services: [],detail:"", // 货品详情serviceId: 0,writeCharacter: false,readCharacter: false,notifyCharacter: false};},computed: {...mapState(['sysinfo', 'Bluetooth'])},onLoad(option) {this.detail = option.data},onUnload() {//停止搜索蓝牙设备if (this.isSearching) {uni.stopBluetoothDevicesDiscovery();}},methods:{ //错误码提示errorCodeTip(code) {if (code == 0) {//正常} else if (code == 10000) {uni.showToast({title: '未初始化蓝牙适配器',icon: 'none'})} else if (code == 10001) {uni.showToast({title: '当前蓝牙适配器不可用',icon: 'none'})} else if (code == 10002) {uni.showToast({title: '没有找到指定设备',icon: 'none'})} else if (code == 10003) {uni.showToast({title: '连接失败',icon: 'none'})} else if (code == 10004) {uni.showToast({title: '没有找到指定服务',icon: 'none'})} else if (code == 10005) {uni.showToast({title: '没有找到指定特征值',icon: 'none'})} else if (code == 10006) {uni.showToast({title: '当前连接已断开',icon: 'none'})} else if (code == 10007) {uni.showToast({title: '当前特征值不支持此操作',icon: 'none'})} else if (code == 10008) {uni.showToast({title: '其余所有系统上报的异常',icon: 'none'})} else if (code == 10009) {uni.showToast({title: 'Android 系统特有,系统版本低于 4.3 不支持 BLE',icon: 'none'})}},//开始搜索蓝牙handleStartSearch() {let that = thisuni.openBluetoothAdapter({success(res) {uni.getBluetoothAdapterState({success(res2) {if (res2.available) {that.isSearching = true;if (res2.discovering) {uni.showToast({title: '正在搜索附近打印机设备',icon: "none"})return;}//获取蓝牙设备信息that.getBluetoothDevices()// that.checkPemission()} else {uni.showModal({title: '提示',content: '本机蓝牙不可用',})}}});},fail() {uni.showModal({title: '提示',content: '蓝牙初始化失败,请打开蓝牙',})}})},// 停止搜索handleStopSearch() {uni.stopBluetoothDevicesDiscovery({success: (res) => {this.isSearching = false;},fail: (e) => {this.errorCodeTip(e.errCode);}})},//获取蓝牙设备信息getBluetoothDevices() {let that = thisthat.list = [];uni.startBluetoothDevicesDiscovery({success(res) {//蓝牙设备监听 uni.onBluetoothDeviceFounduni.onBluetoothDeviceFound((result) => {let arr = that.list;let devices = [];let list = result.devices;for (let i = 0; i < list.length; ++i) {if (list[i].name && list[i].name != "未知设备") {let arrNew = arr.filter((item) => {return item.deviceId == list[i].deviceId;});if (arrNew.length == 0) {devices.push(list[i]);}}}that.list = arr.concat(devices);});that.time = setTimeout(() => {// uni.getBluetoothDevicesuni.getBluetoothDevices({success(res2) {let devices = [];let list = res2.devices;for (let i = 0; i < list.length; ++i) {if (list[i].name && list[i].name != "未知设备") {devices.push(list[i]);}}that.list = devices;},})clearTimeout(that.time);}, 3000);}});},//绑定蓝牙bindViewTap(e) {let that = this;let {title} = e.currentTarget.dataset;let {BLEInformation} = that.Bluetooth;this.handleStopSearch();that.serviceId = 0;that.writeCharacter = false;that.readCharacter = false;that.notifyCharacter = false;uni.showLoading({title: '正在连接',})uni.createBLEConnection({deviceId: title,success(res) {BLEInformation.deviceId = title;that.$store.commit('BLEInformationSet', BLEInformation);uni.hideLoading()that.getSeviceId()},fail(e) {if (e.errCode === -1) {BLEInformation.deviceId = title;that.$store.commit('BLEInformationSet', BLEInformation);that.getSeviceId()}that.errorCodeTip(e.errCode);uni.hideLoading()}})},//获取蓝牙设备所有服务(service)。getSeviceId() {let that = this;let {BLEInformation} = that.Bluetooth;// uni.getBLEDeviceServiceslet t=setTimeout(()=>{uni.getBLEDeviceServices({deviceId: BLEInformation.deviceId,success(res) {that.services = res.services;that.getCharacteristics()},fail: function(e) {that.errorCodeTip(e.code);    }})clearTimeout(t);},1500)},getCharacteristics() {var that = thislet {services: list,serviceId: num,writeCharacter: write,readCharacter: read,notifyCharacter: notify} = that;let {BLEInformation} = that.Bluetooth;// uni.getBLEDeviceCharacteristicsuni.getBLEDeviceCharacteristics({deviceId: BLEInformation.deviceId,serviceId: list[num].uuid,success(res) {for (var i = 0; i < res.characteristics.length; ++i) {var properties = res.characteristics[i].propertiesvar item = res.characteristics[i].uuidif (!notify) {if (properties.notify) {BLEInformation.notifyCharaterId = item;BLEInformation.notifyServiceId = list[num].uuid;that.$store.commit('BLEInformationSet', BLEInformation);notify = true}}if (!write) {if (properties.write) {BLEInformation.writeCharaterId = item;BLEInformation.writeServiceId = list[num].uuid;that.$store.commit('BLEInformationSet', BLEInformation);write = true}}if (!read) {if (properties.read) {BLEInformation.readCharaterId = item;BLEInformation.readServiceId = list[num].uuid;that.$store.commit('BLEInformationSet', BLEInformation);read = true}}}if (!write || !notify || !read) {num++that.writeCharacter = write;that.readCharacter = read;that.notifyCharacter = notify;that.serviceId = num;if (num == list.length) {uni.showModal({title: '提示',content: '找不到该读写的特征值',})} else {that.getCharacteristics()}} else {that.openControl()}},fail: function(e) {that.errorCodeTip(e.errCode);   }})},openControl: function() {uni.navigateTo({url: '../code-print/code-print?data='+this.detail,})},}}

打印

 var tsc = require("../../../utils/ble/tsc.js");import {mapState} from 'vuex';export default {data() {return {canvasWidth: 400,canvasHeight: 400,looptime: 0,currentTime: 1,lastData: 0,oneTimeData: 0,printerNum: 1,currentPrint: 1,detail:"",// 货品详情buffSize: [],printNum: [],isReceiptSend: false,isLabelSend: false};},onReady(){let list = []let numList = []let j = 0for (let i = 20; i < 200; i += 10) {list[j] = i;j++}for (let i = 1; i < 10; i++) {numList[i - 1] = i}this.buffSize = list;this.oneTimeData = list[0];this.printNum = numList;this.printerNum = numList[0];},onLoad(option) {this.detail = JSON.parse(option.data)},computed: {...mapState(['sysinfo', 'Bluetooth'])},onShow(){let that = this;const ctx = uni.createCanvasContext("edit_area_canvas", this);ctx.font = 'normal bold 20px sans-serif';ctx.setFillStyle('#171717'); // 文字颜色ctx.translate(0,0)ctx.rotate(90*Math.PI/180)ctx.fillText(that.detail.name, 20,-15); // 绘制文字ctx.draw();},methods:{// 返回handleBack(){uni.navigateBack({delta:3})},//标签测试labelTest(){let that = this;let canvasWidth = that.canvasWidthlet canvasHeight = that.canvasHeightlet command = tsc.jpPrinter.createNew()command.setSize(14, 40)command.setGap(0)command.setCls()command.setDensity(7)// command.setText(80, 40, "TSS24.BF2", 1, 1, "名称:驰腾贴贴乐")command.setBarCode(60, 20, "EAN128", 50, 0, 2, 2, that.detail.code)uni.canvasGetImageData({canvasId: 'edit_area_canvas',x: 0,y: 0,width: canvasHeight,height: canvasWidth,success: function(res) {command.setBitmap(60, 0, 1, res)},complete: function() {command.setPagePrint()that.isLabelSend = true;that.prepareSend(command.getData())}})},//准备发送,根据每次发送字节数来处理分包数量prepareSend(buff){let that = thislet time = that.oneTimeDatalet looptime = parseInt(buff.length / time);let lastData = parseInt(buff.length % time);this.looptime = looptime + 1;this.lastData = lastData;this.currentTime = 1;that.Send(buff)},//分包发送Send(buff){let that = thislet {currentTime,looptime:loopTime,lastData,oneTimeData:onTimeData,printerNum:printerNum,currentPrint}=that;let buf;let dataView;if (currentTime < loopTime) {buf = new ArrayBuffer(onTimeData)dataView = new DataView(buf)for (var i = 0; i < onTimeData; ++i) {dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])}} else {buf = new ArrayBuffer(lastData)dataView = new DataView(buf)for (var i = 0; i < lastData; ++i) {dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])}}let {BLEInformation} = that.Bluetooth;uni.writeBLECharacteristicValue({deviceId: BLEInformation.deviceId,serviceId: BLEInformation.writeServiceId,characteristicId: BLEInformation.writeCharaterId,value: buf,complete: function() {currentTime++if (currentTime <= loopTime) {that.currentTime = currentTime;that.Send(buff)} else {if (currentPrint > printerNum) {that.looptime = 0;that.lastData = 0;that.currentTime = 1;that.isReceiptSend = false;that.isLabelSend = false;that.currentPrint = 1;} else {uni.showToast({title: '已打印第' + currentPrint + '张',})currentPrint++;that.currentPrint = currentPrint;that.currentTime = 1;that.Send(buff)}}}})},},onUnload() {let that = this;let {BLEInformation} = that.Bluetooth;    },}

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

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

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

  2. 小程序连接蓝牙打印机

    小程序连接蓝牙打印机 1.openBluetoothAdapter初始化蓝牙适配器 wx.openBluetoothAdapter({success: function (res) {console. ...

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

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

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

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

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

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

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

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

  7. uni-app H5+ 连接蓝牙打印机打印文字及二维码

    基于Native.js 实现的连接蓝牙打印机 打印效果图 核心代码 测试代码 运行设备及环境 PS: PPS: Demo 打印效果图 核心代码 /*** @Description: 蓝牙打印类 基于h ...

  8. uni-app 小程序蓝牙库

    分享一下之前用uni-app写小程序的时候,做的蓝牙库 git地址:https://github.com/qinnnn/bleTool 使用方法 主要是项目中 utils/bleTool.js 的文件 ...

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

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

最新文章

  1. learning to rank_排序
  2. 利用MyEclipse开发一个调用webservice接口的程序
  3. Teradata QA Tester 职位
  4. win10显示隐藏文件_如何在Mac上显示隐藏文件?苹果mac显示隐藏文件夹方法
  5. 中国中老年化妆品行业消费需求现状与产销规模前景展望报告2022年
  6. 本地操作HDFS报错:java.net.ConnectException: Call From KevinWong/192.168.16.1 to hadoop000:9000
  7. string类的各种函数用法
  8. saltstack mysql_saltstack学习五:return及入库_MySQL
  9. C/C++ OpenCV设置感兴趣区域ROI
  10. linux-目录查询命令-目录内容查看-ls查询-tree查询-查询类容分类-不同颜色对应不同类型
  11. Oracle统计某一年中的1-12个月的数据总和
  12. vim批量删除与插入
  13. mcgscom口针脚定义_各种接口针脚定义
  14. SQL注入的严重危害之拖库实例
  15. python 等值线插值,CartoPy等值线的插值方法
  16. windows10的etc路径下没有hosts文件
  17. Java中一种容易被忽视的访问权限问题
  18. 2021年终总结 : 聚焦发展,稳步突破
  19. 修改源码刷步数、年会摇一摇数据
  20. Dungeon Master(poj2251,bfs)

热门文章

  1. 蓝牙UART和PCM接口传输的信号 硬件接口
  2. C语言 求10的阶乘
  3. java手游+纹章,龙之谷手游纹章怎么获得 纹章获取途径一览
  4. 洛基开放文化实验室---荐书”项目首批TOP5,
  5. java毕业设计OA办公系统(附源码、数据库)
  6. 思科网络学院-网络互联-第五章
  7. U盘便携式hexo博客搭建极速纯净低bug主题推荐部署到codingSEO优化搜索
  8. 国培计算机培训收获,国培科学心得体会
  9. java反射(暴力破解)
  10. 安装VS2003时IIS下面缺少FrontPage2000服务器扩展的解决办法