藍牙列印
藍牙列印熱敏紙是我作業第一個小程式專案時用到的,當時啃下這塊硬骨頭是真的感覺好難,這篇文章我將監聽藍牙特征這部分刪減掉了,減少難度,而且我個人使用中感覺也不是那么必須,
微信開發者檔案-藍牙
app.js
// 全域變數,藍牙物件模板,但是在初始化藍牙之前需要洗掉資料,
globalData: {
btDevices: [
{
id: 1,
rssi: -40, // 信號
name: 'BT626', // 藍牙設備名稱
devId: '0', // 藍牙序列id
img: '/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.length
for (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.length
that.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("藍牙未開啟!下拉!");
}
})
}
})
}
})
},
第二階段
這是藍牙列印最終重要的三個資料,
// 藍牙設備id
devId: openid.devId,
// 藍牙列印設備服務id
writeServiceId: '',
//藍牙列印設備特征值id
writeCharacteristicId: '',
// 藍牙列印設備服務序列
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].uuid
complete: 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里去,通過字串操作賦值,
//將字串轉成arrayBuffer
base64Str = 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.byteLength
var size = 100
var 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 % size
let buffer = null
let dataView_temp2 = null
if (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";
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/256402.html
標籤:其他
