|
|
|
@ -33,7 +33,7 @@ export function openBlue() { |
|
|
|
//搜索蓝牙设备
|
|
|
|
export function startBluetoothDevicesDiscovery(option) { |
|
|
|
uniAsyncPromise('startBluetoothDevicesDiscovery', option).then((res) => { |
|
|
|
console.log('正在搜寻蓝牙设备', res); |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
@ -52,7 +52,7 @@ export function onfindBlueDevices(getDevices) { |
|
|
|
uni.onBluetoothDeviceFound((devices)=>{ |
|
|
|
//获取在蓝牙模块生效期间所有已发现的蓝牙设备
|
|
|
|
uniAsyncPromise('getBluetoothDevices').then((res) => { |
|
|
|
console.log('res这里',res.devices) |
|
|
|
|
|
|
|
getDevices && getDevices(res.devices); |
|
|
|
}); |
|
|
|
}); |
|
|
|
@ -65,7 +65,7 @@ export function onfindBlueDevices(getDevices) { |
|
|
|
export function stopBlueDevicesDiscovery() { |
|
|
|
//监听寻找到新设备的事件
|
|
|
|
return uniAsyncPromise('stopBluetoothDevicesDiscovery').then((res) => { |
|
|
|
console.log('停止搜寻蓝牙设备', res); |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
@ -85,13 +85,13 @@ export function createBLEConnection(deviceId, sucess, fail) { |
|
|
|
.then(res => { |
|
|
|
//连接成功可选择停止搜索蓝牙
|
|
|
|
//stopBlueDevicesDiscovery();
|
|
|
|
console.log('连接成功'); |
|
|
|
|
|
|
|
sucess && sucess({ |
|
|
|
res: res, |
|
|
|
}); |
|
|
|
}) |
|
|
|
.catch(res => { |
|
|
|
console.log('连接设备异常' + JSON.stringify(res)); |
|
|
|
|
|
|
|
fail && fail({ |
|
|
|
res: res, |
|
|
|
}); |
|
|
|
@ -104,31 +104,31 @@ export function closeBLEConnection(deviceId) { |
|
|
|
deviceId |
|
|
|
}) |
|
|
|
.then(res => { |
|
|
|
console.log('BLEDisconnect complete', res); |
|
|
|
|
|
|
|
}) |
|
|
|
.catch(res => { |
|
|
|
console.log('断开设备异常' + res); |
|
|
|
|
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
//四、连接成功后, 获取蓝牙设备的service服务
|
|
|
|
// uniAsyncPromise("getBLEDeviceServices",{deviceId:""}).then(res=>{})
|
|
|
|
export function getBLEDeviceServices(deviceId, success, fail) { |
|
|
|
console.log('获取ServiceId', deviceId); |
|
|
|
|
|
|
|
//加延迟避免取不到service
|
|
|
|
setTimeout(()=>{ |
|
|
|
uniAsyncPromise("getBLEDeviceServices", { |
|
|
|
deviceId: uni.getStorageSync('deviceId')?uni.getStorageSync('deviceId'):deviceId |
|
|
|
}) |
|
|
|
.then(res => { |
|
|
|
console.log('服务', res); |
|
|
|
|
|
|
|
success && success({ |
|
|
|
serviceId: res.services, |
|
|
|
}); |
|
|
|
}) |
|
|
|
.catch((res) => { |
|
|
|
//getBLEDeviceServices(deviceId, success, fail);
|
|
|
|
console.log('获取ServiceId异常' + res); |
|
|
|
|
|
|
|
fail && fail({ |
|
|
|
res: res, |
|
|
|
}); |
|
|
|
@ -147,10 +147,10 @@ export function getBLEDeviceServices(deviceId, success, fail) { |
|
|
|
*/ |
|
|
|
export function getDeviceCharacteristics(deviceId, services, success, fail) { |
|
|
|
//services = services.slice(0);
|
|
|
|
console.log('获取Characteristics', deviceId, services); |
|
|
|
|
|
|
|
if (services.length) { |
|
|
|
const serviceId = services.shift().uuid; |
|
|
|
console.log('ServceID ', serviceId); |
|
|
|
|
|
|
|
uniAsyncPromise('getBLEDeviceCharacteristics', { |
|
|
|
deviceId, |
|
|
|
serviceId, |
|
|
|
@ -197,15 +197,15 @@ export function getDeviceCharacteristics(deviceId, services, success, fail) { |
|
|
|
* @param {function} onChange 监听蓝牙设备传递数据回调函数 |
|
|
|
*/ |
|
|
|
export function onGetBLECharacteristicValueChange(options, onChange = function() {}) { |
|
|
|
console.log('deviceId ', options.deviceId); |
|
|
|
console.log('serviceId ', options.serviceId); |
|
|
|
console.log('characteristicId ', options.characteristicId); |
|
|
|
console.log('苹果手机看这里'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uniAsyncPromise('notifyBLECharacteristicValueChange', { |
|
|
|
state: true, |
|
|
|
...options, |
|
|
|
}).then((res) => { |
|
|
|
console.log('onBLECharacteristicValueChange '); |
|
|
|
|
|
|
|
uni.onBLECharacteristicValueChange(onChange); |
|
|
|
}); |
|
|
|
} |
|
|
|
@ -228,26 +228,26 @@ export function sendDataToDevice(options) { |
|
|
|
let byteLength = options.value.byteLength; |
|
|
|
//这里默认一次20个字节发送
|
|
|
|
const speed = options.onceLength; //20;
|
|
|
|
console.log(options); |
|
|
|
|
|
|
|
if (byteLength > 0) { |
|
|
|
uniAsyncPromise('writeBLECharacteristicValue', { |
|
|
|
...options, |
|
|
|
value: options.value.slice(0, byteLength > speed ? speed : byteLength), |
|
|
|
}) |
|
|
|
.then((res) => { |
|
|
|
console.log('发送数据成功了',res) |
|
|
|
|
|
|
|
if (byteLength > speed) { |
|
|
|
sendDataToDevice({ |
|
|
|
...options, |
|
|
|
value: options.value.slice(speed, byteLength), |
|
|
|
}); |
|
|
|
} else { |
|
|
|
console.log("打印完成") |
|
|
|
|
|
|
|
options.lasterSuccess && options.lasterSuccess(); |
|
|
|
} |
|
|
|
}) |
|
|
|
.catch((res) => { |
|
|
|
console.log('打印错误了',res) |
|
|
|
|
|
|
|
uni.showToast({ |
|
|
|
title: res, |
|
|
|
icon: 'none' |
|
|
|
@ -373,10 +373,10 @@ export function printImage(opt = {}, imageInfo = {}) { |
|
|
|
writeArray.push(new Uint8Array(tempArr)); |
|
|
|
} |
|
|
|
writeArray.push(new Uint8Array(enter)); |
|
|
|
//console.log(writeArray);
|
|
|
|
|
|
|
|
const print = (options, writeArray) => { |
|
|
|
if (writeArray.length) { |
|
|
|
console.log("send"); |
|
|
|
|
|
|
|
sendDataToDevice({ |
|
|
|
...options, |
|
|
|
value: writeArray.shift().buffer, |
|
|
|
@ -390,7 +390,7 @@ export function printImage(opt = {}, imageInfo = {}) { |
|
|
|
}); |
|
|
|
} |
|
|
|
}; |
|
|
|
console.log("start print"); |
|
|
|
|
|
|
|
print(opt, writeArray); |
|
|
|
} |
|
|
|
|
|
|
|
@ -434,7 +434,7 @@ export function CreatCPCLPage(width, height, printNum, rotation, offset = 0) { |
|
|
|
* content: 文字内容 |
|
|
|
*/ |
|
|
|
export function addCPCLText(x, y, fontName, fontSize, rotation, content) { |
|
|
|
//console.log(fontName,fontSize,rotation, content);
|
|
|
|
|
|
|
|
// alert(content);
|
|
|
|
var strCmd = ''; |
|
|
|
if (rotation == 1) { |
|
|
|
@ -582,7 +582,7 @@ export function addCPCLBACKGROUND(level) { |
|
|
|
* content: 文字内容 |
|
|
|
*/ |
|
|
|
export function addCPCLBKVText(x, y, fontName, fontSize, rotation, content) { |
|
|
|
//console.log(fontName,fontSize,rotation, content);
|
|
|
|
|
|
|
|
var strCmd = ''; |
|
|
|
if (rotation == 1) { |
|
|
|
strCmd += 'BKT90 '; |
|
|
|
@ -649,16 +649,16 @@ export function addCPCLImageCmd(x, y, data) { |
|
|
|
let myBitmapWidth = data.width, |
|
|
|
myBitmapHeight = data.height; |
|
|
|
let len = parseInt((myBitmapWidth + 7) / 8); //一行的数据长度
|
|
|
|
//console.log('len=',len);
|
|
|
|
//console.log('myBitmapWidth=',myBitmapWidth);
|
|
|
|
//console.log('myBitmapHeight=',myBitmapHeight);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let ndata = 0; |
|
|
|
let i = 0; |
|
|
|
let j = 0; |
|
|
|
let sendImageData = new ArrayBuffer(len * myBitmapHeight); |
|
|
|
sendImageData = new Uint8Array(sendImageData); |
|
|
|
let pix = data.imageData; |
|
|
|
console.log('pix=', pix); |
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < myBitmapHeight; i++) { |
|
|
|
for (j = 0; j < len; j++) { |
|
|
|
@ -672,14 +672,14 @@ export function addCPCLImageCmd(x, y, data) { |
|
|
|
} |
|
|
|
ndata += len; |
|
|
|
} |
|
|
|
//console.log('sendImageData=',sendImageData);
|
|
|
|
|
|
|
|
//CPCL指令图片数据
|
|
|
|
strImgCmd += 'EG ' + len + ' ' + myBitmapHeight + ' ' + x + ' ' + y + ' '; |
|
|
|
for (i = 0; i < sendImageData.length; i++) { |
|
|
|
strImgCmd += Hex2Str(sendImageData[i]); |
|
|
|
} |
|
|
|
strImgCmd += '\n'; |
|
|
|
//console.log(strImgCmd);
|
|
|
|
|
|
|
|
return strImgCmd; |
|
|
|
} |
|
|
|
/** |
|
|
|
|