CSS
/**index.wxss**/
page {
background-color: #EDF0F5;
}
.pages {
margin: 20px;
}
.printbtn {
margin-top: 20px;
width: 100%;
height: 80rpx;
border-radius: 66rpx;
background-color: #42C1AC;
color: white;
text-align: center;
line-height: 80rpx;
}
.text{
margin-top: 20px;
}
.input{
background-color: #fff;
height: 80rpx;
margin-top: 20px;
padding: 10px;
}
.device_list {
margin: 20px 5px;
border-radius: 5px;
width: auto;
}
.device_item {
border: 1px solid #AAA;
padding: 10px;
color: #666;
}
.device_item div{
display: block;
}
HTML
<!--index.wxml-->
<view class="pages">
<button class="printbtn" type="primary" bindtap="searchBluetooth" loading='{{isScanning}}'> 開始搜索 </button>
<view class="device_list">
<view wx:for="{{devicesList}}" wx:key="key" wx:for-index="index" wx:for-item="item" bindtap="connectBluetoothSettings" data-index="{{index}}" class="device_item">
<div class="deviceId">deviceId:{{item.deviceId}}</div>
<div class="name">name:{{item.name}}</div>
</view>
</view>
<button class="printbtn" type='primary' bindtap='labelTest' loading='{{isLabelSend}}' disabled='{{isLabelSend}}'>標簽測驗</button>
<view class="text">發送資料大小:</view>
<textarea class="input" bindinput="inputEvent" value="{{oneTimeData}}" />
<canvas canvas-id="edit_area_canvas"></canvas>
</view>
JS
//index.js
//獲取應用實體
const app = getApp();
let tsc = require('../../components/gprint/tsc.js')
let esc = require('../../components/gprint/esc.js')
Page({
data: {
devicesList: [],
services: [],
serviceId: 0,
writeCharacter: false,
readCharacter: false,
notifyCharacter: false,
isScanning: false,
looptime: 0,
currentTime: 1,
lastData: 0,
oneTimeData: 20,//發送資料大小,測驗正常,不能太多
returnResult: "returnResult",
canvasWidth: 80,
canvasHeight: 80,
printNum: 1,
currentPrint: 1,
isReceiptSend: false,
isLabelSend: false
},
/**
* 監聽資料輸入
*/
inputEvent: function(e) {
this.setData({
oneTimeData: e.detail.value
})
console.log('oneTimeData: ', this.data.oneTimeData)
},
/**
* 藍牙搜索
*/
searchBluetooth: function () {
var that = this
//判斷藍牙是否打開
wx.openBluetoothAdapter({
success: function (res) {
wx.getBluetoothAdapterState({
success: function (res) {
if (res.available) {
if (res.discovering) {
wx.stopBluetoothDevicesDiscovery({
success: function (res) {
console.log(res)
}
})
}
that.checkPemission()
} else {
wx.showModal({
title: '提示',
content: '請開啟手機藍牙后再試',
})
}
},
})
},
fail: function () {
wx.showModal({
title: '提示',
content: '藍牙初始化失敗,請打開藍牙',
})
}
})
},
/**
* android 6.0以上需授權地理位置權限
*/
checkPemission: function () {
var that = this;
var systemInfo = wx.getSystemInfoSync();
var platform = systemInfo.platform;
if (platform == "ios") {
that.getBluetoothDevices()
} else if (platform == "android") {
let system = systemInfo.system;
let system_no = system.replace('android', '');
system_no = system.replace('Android', '');
if (Number(system_no) > 5) {
wx.getSetting({
success: function (res) {
console.log(res)
if (!res.authSetting['scope.userLocation']) {
wx.authorize({
scope: 'scope.userLocation',
complete: function (res) {
that.getBluetoothDevices()
}
})
} else {
that.getBluetoothDevices()
}
}
})
}
}
},
/**
* 獲取藍牙設備資訊
*/
getBluetoothDevices: function () {
var that = this
console.log("start search")
wx.showLoading({
title: '正在加載',
})
that.setData({
isScanning: true
})
wx.startBluetoothDevicesDiscovery({
success: function (res) {
console.log(res)
setTimeout(function () {
wx.getBluetoothDevices({
success: function (res) {
var devices = []
var num = 0
for (var i = 0; i < res.devices.length; ++i) {
if (res.devices[i].name != "未知設備") {
devices[num] = res.devices[i]
num++
}
}
that.setData({
devicesList: devices,
isScanning: false
})
wx.hideLoading()
wx.stopPullDownRefresh()
},
})
}, 3000)
},
})
},
/**
* 開始連接藍牙設定
*/
connectBluetoothSettings: function (e) {
var that = this;
let index = e.currentTarget.dataset.index;
let deviceId = that.data.devicesList[index].deviceId;
wx.stopBluetoothDevicesDiscovery({
success: function (res) {
console.log(res)
},
})
that.setData({
serviceId: 0,
writeCharacter: false,
readCharacter: false,
notifyCharacter: false
})
console.log(deviceId)
wx.showLoading({
title: '正在連接',
})
wx.createBLEConnection({
deviceId: deviceId,
success: function (res) {
console.log(res)
app.globalData.bluetoothDeviceId = deviceId
that.getBLEDeviceServices();
},
fail: function (e) {
wx.showModal({
title: '提示',
content: '連接失敗',
})
console.log(e)
wx.hideLoading()
},
complete: function (e) {
console.log(e)
}
})
},
/**
* 獲取藍牙設備所有服務
*/
getBLEDeviceServices: function () {
var that = this
console.log(app.globalData.bluetoothDeviceId)
wx.getBLEDeviceServices({
deviceId: app.globalData.bluetoothDeviceId,
success: function (res) {
console.log(res)
that.setData({
services: res.services
})
that.getBLEDeviceCharacteristics()
},
fail: function (e) {
console.log(e)
},
complete: function (e) {
console.log(e)
}
})
},
/**
* 獲取藍牙設備某個服務中所有特征值
*/
getBLEDeviceCharacteristics: function () {
var that = this
var list = that.data.services
var num = that.data.serviceId
var write = that.data.writeCharacter
var read = that.data.readCharacter
var notify = that.data.notifyCharacter
wx.getBLEDeviceCharacteristics({
deviceId: app.globalData.bluetoothDeviceId,
serviceId: list[num].uuid,
success: function (res) {
console.log(res)
for (var i = 0; i < res.characteristics.length; ++i) {
var properties = res.characteristics[i].properties
var item = res.characteristics[i].uuid
if (!notify) {
if (properties.notify) {
app.globalData.notifyCharaterId = item
app.globalData.notifyServiceId = list[num].uuid
notify = true
}
}
if (!write) {
if (properties.write) {
app.globalData.writeCharaterId = item
app.globalData.writeServiceId = list[num].uuid
write = true
}
}
if (!read) {
if (properties.read) {
app.globalData.readCharaterId = item
app.globalData.readServiceId = list[num].uuid
read = true
}
}
}
if (!write || !notify || !read) {
num++
that.setData({
writeCharacter: write,
readCharacter: read,
notifyCharacter: notify,
serviceId: num
})
if (num == list.length) {
wx.showModal({
title: '提示',
content: '找不到該讀寫的特征值',
})
} else {
that.getBLEDeviceCharacteristics()
}
} else {
that.notifyBLECharacteristicValueChange()
}
},
fail: function (e) {
console.log(e)
},
complete: function (e) {
console.log("write:" + app.globalData.writeCharaterId)
console.log("read:" + app.globalData.readCharaterId)
console.log("notify:" + app.globalData.notifyCharaterId)
}
})
},
/**
* 啟用低功耗藍牙設備特征值變化時的 notify 功能
*/
notifyBLECharacteristicValueChange: function () {
console.log("deviceId:" + app.globalData.bluetoothDeviceId)
console.log("serviceId:" + app.globalData.notifyServiceId)
console.log("notifyCharaterId:" + app.globalData.notifyCharaterId)
wx.hideLoading();
wx.notifyBLECharacteristicValueChange({
deviceId: app.globalData.bluetoothDeviceId,
serviceId: app.globalData.notifyServiceId,
characteristicId: app.globalData.notifyCharaterId,
state: true,
success: function(res) {
wx.onBLECharacteristicValueChange(function(r) {
// console.log(`characteristic ${r.characteristicId} has changed, now is ${r}`)
console.log('onBLECharacteristicValueChange=', r);
})
},
fail: function(e) {
console.log('fail', e)
},
complete: function(e) {
console.log('complete', e)
}
})
},
/**
* 賬單模式
*/
sendData: function() {
var data = "好好學習,天天向上\n"
var that = this;
var command = tsc.jpPrinter.createNew()
command.setGap(0)
command.setCls()
command.setText(40, 60, "TSS24.BF2", 1, 1, data)
command.setPagePrint()
that.prepareSend(command.getData())
},
/**
* 標簽模式
*/
labelTest: function() {
var that = this;
var canvasWidth = that.data.canvasWidth
var canvasHeight = that.data.canvasHeight
var command = tsc.jpPrinter.createNew()
command.setSize(40, 60)
command.setGap(0)
command.setCls() //需要設定這個,不然內容和上一次重復
command.setQR(1, 120, "L", 5, "A", "poso2o.com")
command.setText(60, 90, "TSS24.BF2", 1, 1, "POSO2O列印機")
command.setText(170, 50, "TSS24.BF2", 1, 1, "小程式測驗")
command.setText(170, 90, "TSS24.BF2", 1, 1, "測驗數字12345678")
command.setText(170, 120, "TSS24.BF2", 1, 1, "測驗英文abcdefg")
command.setText(170, 150, "TSS24.BF2", 1, 1, "測驗符號/*-+!@#$")
wx.canvasGetImageData({
canvasId: 'edit_area_canvas',
x: 0,
y: 0,
width: canvasWidth,
height: canvasHeight,
success: function(res) {
command.setBitmap(60, 0, 0, res)
},
complete: function() {
command.setPagePrint()
that.setData({
isLabelSend: true
})
that.prepareSend(command.getData())
}
})
},
/**
* 準備發送資料
*/
prepareSend: function(buff) {
console.log('buff', buff)
var that = this
var time = that.data.oneTimeData
var looptime = parseInt(buff.length / time);
var lastData = parseInt(buff.length % time);
console.log(looptime + "---" + lastData);
that.setData({
looptime: looptime + 1,
lastData: lastData,
currentTime: 1,
})
that.Send(buff)
},
/**
* 查詢列印機狀態
*/
queryPrinterStatus: function() {
var command = esc.jpPrinter.Query();
command.getRealtimeStatusTransmission(1);
this.setData({
returnResult: "查詢成功"
})
},
/**
* 分包發送
*/
Send: function(buff) {
var that = this
var currentTime = that.data.currentTime;
var loopTime = that.data.looptime;
var lastData = that.data.lastData;
var onTimeData = that.data.oneTimeData;
var printNum = that.data.printNum; //列印多少份
var currentPrint = that.data.currentPrint;
var buf
var 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])
}
}
console.log("第" + currentTime + "次發送資料大小為:" + buf.byteLength);
console.log("deviceId:" + app.globalData.bluetoothDeviceId)
console.log("serviceId:" + app.globalData.writeServiceId)
console.log("characteristicId:" + app.globalData.writeCharaterId)
wx.writeBLECharacteristicValue({
deviceId: app.globalData.bluetoothDeviceId,
serviceId: app.globalData.writeServiceId,
characteristicId: app.globalData.writeCharaterId,
value: buf,
success: function(res) {
console.log('寫入成功', res)
},
fail: function(e) {
console.error('寫入失敗', e)
},
complete: function() {
currentTime++
if (currentTime <= loopTime) {
that.setData({
currentTime: currentTime
})
that.Send(buff)
} else {
wx.showToast({
title: '已列印第' + currentPrint + '張',
})
if (currentPrint == printNum) {
that.setData({
looptime: 0,
lastData: 0,
currentTime: 1,
isReceiptSend: false,
isLabelSend: false,
currentPrint: 1
})
} else {
currentPrint++
that.setData({
currentPrint: currentPrint,
currentTime: 1,
})
console.log("開始列印")
that.Send(buff)
}
}
}
})
},
onl oad: function(options) {
var that = this;
},
})
下載連接:
https://download.csdn.net/download/luolinyin/12846611
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/68880.html
標籤:其他
上一篇:位元組跳動總監知乎1716贊的AndroidFramework開發筆記助我修行,不吃透感覺都對不起他
下一篇:Setting Up a Capture Session(swift之AVCaptureSession AVCaptureDevice AVFoundation)
