650添加应答,app图标添加

This commit is contained in:
liub
2025-09-15 11:55:44 +08:00
parent 6ee45f6868
commit 9c9c684ab6
27 changed files with 31504 additions and 7864 deletions

View File

@ -240,6 +240,8 @@
import gbk from '@/utils/gbk.js';
import TextToHexV1 from '@/components/TextToHex/TextToHexV1.vue';
import Common from '../../../utils/Common';
import BleHelper from '../../../utils/BleHelper';
var ble=BleHelper.getBleTool();
export default {
components: {
TextToHexV1
@ -251,7 +253,7 @@
canvasTop: '-1000px',
canvasLeft: '-1000px',
inteval: 100,
inteval: 1000,
isLoading: false,
isBluetoothOpen: false,
isSearching: false,
@ -270,7 +272,7 @@
logList: [],
packetCount: 1,
currentPage: 'deviceList',
targetDeviceName: 'JQZM-EF4651',
targetDeviceName: 'JQZM-EF4651',//FB_Site_UART
currentTab: 'mode',
brightness: 50,
brightnessTimer: null,
@ -287,7 +289,7 @@
totalPackets: 100,
imageWidth: 0,
imageHeight: 0,
textLines: ['下雨了', '天亮了', '连接成功'],
textLines: ['那个', '你看到我的小熊', '了吗'],
textProgress: 0,
currentTextPacket: 0,
totalTextPackets: 4,
@ -308,6 +310,7 @@
}
},
onLoad() {
this.initBluetoothAdapter();
},
methods: {
@ -335,32 +338,41 @@
},
// 蓝牙初始化
initBluetoothAdapter() {
ble.addDeviceFound(this.onDeviceFound);
console.log('开始初始化蓝牙适配器');
uni.openBluetoothAdapter({
success: (res) => {
this.isBluetoothOpen = true;
this.bluetoothStatus = '已开启';
console.log('蓝牙适配器初始化成功');
this.getBluetoothAdapterState();
// 自动开始搜索设备
this.startSearch();
},
fail: (err) => {
this.bluetoothStatus = '初始化失败';
console.log(`蓝牙适配器初始化失败: ${err.errMsg}`);
if (err.errCode === 10001) {
uni.onBluetoothAdapterStateChange((res) => {
if (res.available) {
this.isBluetoothOpen = true;
this.bluetoothStatus = '已开启';
console.log('蓝牙适配器已开启');
this.startSearch();
}
});
}
}
ble.OpenBlue().then((res)=>{
this.isBluetoothOpen = true;
this.bluetoothStatus = '已开启';
ble.StartSearch();
}).catch((err)=>{
this.isBluetoothOpen = true;
this.bluetoothStatus = '已开启';
});
// uni.openBluetoothAdapter({
// success: (res) => {
// this.isBluetoothOpen = true;
// this.bluetoothStatus = '已开启';
// console.log('蓝牙适配器初始化成功');
// this.getBluetoothAdapterState();
// // 自动开始搜索设备
// this.startSearch();
// },
// fail: (err) => {
// this.bluetoothStatus = '初始化失败';
// console.log(`蓝牙适配器初始化失败: ${err.errMsg}`);
// if (err.errCode === 10001) {
// uni.onBluetoothAdapterStateChange((res) => {
// if (res.available) {
// this.isBluetoothOpen = true;
// this.bluetoothStatus = '已开启';
// console.log('蓝牙适配器已开启');
// this.startSearch();
// }
// });
// }
// }
// });
},
// 获取蓝牙适配器状态
@ -418,39 +430,46 @@
this.deviceList = [];
this.searchTips = '搜索中...';
console.log('开始搜索蓝牙设备');
uni.startBluetoothDevicesDiscovery({
services: ["0xFFE0"],
allowDuplicatesKey: false,
success: (res) => {
console.log('开始搜索蓝牙设备成功');
this.onDeviceFound();
},
fail: (err) => {
this.isSearching = false;
this.searchTips = '搜索失败';
console.log(`搜索蓝牙设备失败: ${err.errMsg}`);
}
ble.StartSearch().then(()=>{
console.log('开始搜索蓝牙设备成功');
}).catch((err)=>{
this.isSearching = false;
this.searchTips = '搜索失败';
console.log(`搜索蓝牙设备失败: ${err.errMsg}`);
});
// uni.startBluetoothDevicesDiscovery({
// allowDuplicatesKey: false,
// success: (res) => {
// console.log('开始搜索蓝牙设备成功');
// this.onDeviceFound();
// },
// fail: (err) => {
// this.isSearching = false;
// this.searchTips = '搜索失败';
// console.log(`搜索蓝牙设备失败: ${err.errMsg}`);
// }
// });
},
// 停止搜索蓝牙设备
stopSearch() {
this.isSearching = false;
this.searchTips = this.deviceList.length > 0 ? '搜索完成' : '未发现蓝牙设备';
uni.stopBluetoothDevicesDiscovery({
success: () => {
console.log('停止搜索蓝牙设备');
}
});
ble.StartSearch();
// uni.stopBluetoothDevicesDiscovery({
// success: () => {
// console.log('停止搜索蓝牙设备');
// }
// });
},
// 监听发现新设备
onDeviceFound() {
uni.onBluetoothDeviceFound((res) => {
onDeviceFound(res) {
// uni.onBluetoothDeviceFound((res) => {
// console.log("发现新设备",res);
var device = res.devices[0];
if (!device.name && !device.localName) return;
// if ((device.name || device.localName) === this.targetDeviceName) {
@ -467,24 +486,62 @@
this.deviceList[index].RSSI = device.RSSI;
}
// }
// 如果找到目标设备,自动连接
if ((device.name || device.localName) === this.targetDeviceName) {
console.log(`发现目标设备: ${JSON.stringify(device)}`);
this.connectDevice(device.deviceId, this.targetDeviceName,device);
}
});
// });
},
// 连接蓝牙设备
connectDevice(deviceId, deviceName,device) {
if (this.isConnected && this.connectedDeviceId === deviceId) return;
let gotoDetail=()=>{
ble.StopSearch();
uni.navigateTo({
url:"/pages/6155/deviceDetail",
success(res) {
res.eventChannel.emit('detailData', {
data: {deviceId:deviceId},
deviceType: '',
apiType: 'listA' //标识,根据这个参数,区分普通详情,分享跳转详情,查不一样的权限信息
});
},fail(ex){
console.log("跳转失败",ex);
}
});
}
if (this.isConnected && this.connectedDeviceId === deviceId) {
gotoDetail();
return;
};
this.isLoading = true;
if(device){
console.log("device=",device);
}
let promise=null;
if (deviceName == 'FB_Site_UART') {
promise=ble.LinkBlue(deviceId, '0000AE30-0000-1000-8000-00805F9B34FB',
'0000AE03-0000-1000-8000-00805F9B34FB', '0000AE02-0000-1000-8000-00805F9B34FB');
} else {
promise=ble.LinkBlue(deviceId, '0000FFE0-0000-1000-8000-00805F9B34FB',
'0000FFE1-0000-1000-8000-00805F9B34FB', '0000FFE2-0000-1000-8000-00805F9B34FB');
}
promise.then(res=>{
console.log("连接成功了");
gotoDetail();
this.isConnected=true;
}).catch(err=>{
console.log("连接失败了",err);
this.isConnected=false;
});
return;
console.log(`开始连接设备: ${deviceName}`);
var these = this;
uni.createBLEConnection({
@ -502,16 +559,20 @@
setTimeout(function() {
uni.setBLEMTU({
deviceId: deviceId,
mtu: 1024,
mtu: 512,
success: (info) => {
console.log("设置mtu成功",info)
these.getDeviceServices(deviceId);
},
fail: (ex) => {
console.log("设置mtu失败" + JSON.stringify(ex));
these.getDeviceServices(deviceId);
},finally(){
}
})
these.getDeviceServices(deviceId);
}, 2000)
// 获取服务和特征值
@ -648,7 +709,7 @@
deviceId: deviceId,
success: (res) => {
these.addLog("发现服务" + JSON.stringify(res.services));
console.log("发现服务" + JSON.stringify(res.services));
if (res.services.length == 0) {
setTimeout(function() {
these.getDeviceServices(deviceId);
@ -662,11 +723,11 @@
});
if (targetService) {
these.addLog(`找到目标服务: ${targetService.uuid}`);
console.log(`找到目标服务: ${targetService.uuid}`);
this.serviceId = targetService.uuid;
this.getDeviceCharacteristics(deviceId, targetService.uuid);
} else {
these.addLog("没找到目标服务");
console.log("没找到目标服务");
}
},
fail: (err) => {
@ -684,7 +745,7 @@
serviceId: serviceId,
success: (res) => {
console.log(`获取设备特征值成功,共发现 ${res.characteristics.length} 个特征值`);
these.addLog(`获取设备成功:${JSON.stringify(res.characteristics)}`);
console.log(`获取设备成功:${JSON.stringify(res.characteristics)}`);
// 查找可写特征值
var writeChar = res.characteristics.find(char => {
return char.uuid.indexOf("FFE1") > -1 || char.uuid.indexOf(
@ -818,7 +879,7 @@
"续航时间:"+this.receiveData.time
];
this.logList=[];
this.addLog(arr.join(","));
console.log(arr.join(","));
} catch (error) {
console.log('数据解析错误:',error);
}
@ -833,14 +894,14 @@
let arr=hexs.split(' ');
if(arr.length>=7){
this.receiveData.macAddress=arr.slice(1,7).join(":");
this.addLog("设备mac地址:"+this.receiveData.macAddress);
console.log("设备mac地址:"+this.receiveData.macAddress);
}else{
this.addLog("收到无效的数据");
console.log("收到无效的数据");
}
}
else {
this.addLog("收到无效的数据");
console.log("收到无效的数据");
}
}
@ -929,7 +990,7 @@
setMode(mode, type) {
this.logList = [];
if (!this.isConnected) {
this.addLog("未连接设备")
console.log("未连接设备")
uni.showToast({
title: '未连接设备',
icon: 'none'
@ -1195,10 +1256,33 @@
processAndSendImageData(pixels) {
return new Promise((resolve, reject) => {
// 创建RGB565格式的像素数据
var rgb565Data = this.convertToRGB565(pixels, 160, 80);
// console.log("rgb565Data=,",rgb565Data);
var arr = this.convertToRGB565(pixels, 160, 80);
var list = [];
let index = 0; // 用于追踪arr的当前位置
// 外层循环7个主要元素i从1到7
for (let i = 1; i < 8; i++) {
let secondLevel = [];
// 中层循环每个主要元素包含9个子数组j从1到9
for (let j = 1; j < 10; j++) {
// 确定当前子数组的长度前8个是254第9个是16
let length = (j < 9) ? 248 : 128;
let thirdLevel = [];
// 从arr中提取相应数量的元素
for (let k = 0; k < length && index < arr.length; k++) {
thirdLevel.push(arr[index]);
index++;
}
secondLevel.push(thirdLevel);
}
list.push(secondLevel);
}
// 分包发送
this.sendImagePackets(rgb565Data).then(resolve).catch(reject);
this.sendImagePackets(list).then(resolve).catch(reject);
});
},
@ -1221,8 +1305,11 @@
var b = pixels[i + 2];
//var rgb565 = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
var bgr565 = (b >> 3) | ((g & 0xFC) << 3) | ((r & 0xF8) << 8);
// var rgb565 = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
var bgr565 = ((b & 0xF8) << 8) | ((g & 0xFC) << 3) | (r >> 3);
result[index++] = bgr565;
}
@ -1239,68 +1326,61 @@
// 总数据包数
let currentPacket = 1;
let packetSize = 120;
let mode = imageData.length % packetSize;
let totalPackets = parseInt(imageData.length / packetSize) + (mode > 0 ? 1 : 0)
let totalPackets =7;
let childPacket=1;
let totalChildPacket=9;
let inteval = parseInt(this.inteval ? this.inteval : 100);
this.totalPackets = totalPackets;
this.totalPackets = 57;
// 发送单个数据包
var sendNextPacket = () => {
if (currentPacket > totalPackets) {
if (currentPacket> totalPackets) {
this.isSending = false;
resolve();
return;
}
var packetData = imageData[currentPacket-1][childPacket-1];
if(packetData.length==0){
this.isSending = false;
resolve();
return;
}
var start = 0;
var dataLength = 0;
var bufferSize = 0;
if (currentPacket == 1) {
bufferSize = packetSize * 2 + 5;
dataLength = packetSize * 2;
start = 5;
} else if (currentPacket == totalPackets) {
bufferSize = mode > 0 ? (mode * 2) : (packetSize * 2);
dataLength = bufferSize
} else {
bufferSize = packetSize * 2;
dataLength = bufferSize;
}
// 创建数据包
var startIndex = (currentPacket - 1) * packetSize;
var endIndex = Math.min(startIndex + packetSize, imageData.length);
if (startIndex > endIndex) {
return;
}
var packetData = imageData.slice(startIndex,
endIndex); // imageData.subarray(startIndex, endIndex);
// 构建数据包
var buffer = new ArrayBuffer(bufferSize);
var buffer = new ArrayBuffer(500);
var dataView = new DataView(buffer);
if (currentPacket == 1) { //第一包要填充头部
// 填充头部
dataView.setUint8(0, 0x70); // 帧头
dataView.setUint8(1, 0x65); // 帧头
dataView.setUint8(2, 0x0A); // 帧类型
dataView.setUint16(3, dataLength, false); //包长度
}
dataView.setUint8(0, 0xFB); // 帧头
dataView.setUint8(1, 0x09); // 帧头
dataView.setUint8(2, currentPacket); // 包序号
dataView.setUint8(3, childPacket); //子包序号
for (let i = 0; i < packetData.length; i++) {
dataView.setUint16(start + i * 2, packetData[i], false); //本包数据,大端字节序
dataView.setUint16(4 + i * 2, packetData[i], false); //本包数据,大端字节序
}
if(packetData.length<248){
for (var i = 4+packetData.length*2; i < 500; i++) {
dataView.setUint8(i, 0xFF);
}
}
//发送数据包
this.sendData(buffer).then(() => {
// 更新进度
this.currentPacket = currentPacket;
this.progress = Math.round((currentPacket / totalPackets) * totalPackets);
currentPacket++;
this.currentPacket =childPacket+ (currentPacket-1)*totalChildPacket;
this.progress = Math.round((this.currentPacket / this.totalPackets) * 100);
if(childPacket==9){
currentPacket++;
childPacket=1;
}else{
childPacket++;
}
setTimeout(sendNextPacket, inteval);
}).catch(err => {
@ -1332,7 +1412,7 @@
title: "请稍候....",
mask: true
});
this.addLog({
console.log({
date: Common.DateFormat(),
remark: '开始发送文字'
});
@ -1346,7 +1426,7 @@
let mode = rgbdata.length % packetSize; //最后一包的数量
let cnt = parseInt(rgbdata.length / packetSize) + (mode > 0 ? 1 : 0); //总包数量
let curr = 1; //当前包序号
this.addLog({
console.log({
date: Common.DateFormat(),
remark: '当前包' + curr
});
@ -1356,7 +1436,7 @@
resolve();
return;
}
this.addLog({
console.log({
date: Common.DateFormat(),
remark: "正在向设备发送" + curr + "/" + cnt
})
@ -1372,13 +1452,13 @@
let buffer = new ArrayBuffer(bufferSize);
let dataView = new DataView(buffer);
let startIndex = (curr - 1) * packetSize;
this.addLog({
console.log({
date: Common.DateFormat(),
remark: "分片大小" + bufferSize
})
let endIndex = Math.min(startIndex + packetSize, rgbdata.length);
if (startIndex > endIndex) {
this.addLog({
console.log({
date: Common.DateFormat(),
remark: "发送完成"
})
@ -1386,7 +1466,7 @@
}
let packetData = rgbdata.slice(startIndex, endIndex); //取一片数据发送
this.addLog({
console.log({
date: Common.DateFormat(),
remark: "获取到分片"
})
@ -1396,8 +1476,6 @@
dataView.setUint8(1, type);
dataView.setUint8(2, 0x01);
dataView.setUint8(3, 0x00);
// dataView.setUint16(2, str.length, false);
start = 4;
}
@ -1406,7 +1484,7 @@
}
dataView.setUint8(bufferSize - 1, 0xFF);
this.addLog({
console.log({
date: Common.DateFormat(),
remark: "数据准备就绪"
})
@ -1414,7 +1492,7 @@
//发送数据包
this.sendData(buffer).then(() => {
this.addLog({
console.log({
date: Common.DateFormat(),
remark: "发送成功,准备发送下一包"
})
@ -1422,7 +1500,7 @@
setTimeout(sendNext, inteval);
}).catch(err => {
console.log("err=", err);
this.addLog({
console.log({
date: Common.DateFormat(),
remark: "出现错误" + JSON.stringify(err)
});
@ -1438,7 +1516,7 @@
sendNext();
} catch (ex) {
console.log("ex=", ex);
this.addLog({
console.log({
date: Common.DateFormat(),
remark: "出现异常1" + JSON.stringify(ex)
})
@ -1450,12 +1528,12 @@
}
// this.addLog({
// console.log({
// date: Common.DateFormat(),
// remark: '正在取模'
// });
var result = await this.$refs.textToHex.drawAndGetPixels();
// this.addLog({
// console.log({
// date: Common.DateFormat(),
// remark: '取模成功'
// });
@ -1464,6 +1542,19 @@
});
// console.log("result=",result);
// var str1="00 00 0E 58 7C 4C 08 44 08 40 7F FE 08 60 08 64 0B 6C 7F 38 78 30 08 72 08 D2 0B 9E 38 0C 00 00 00 00 1F F8 18 08 1F F8 18 18 18 18 1F F8 00 00 7F FE 01 80 19 80 19 F8 3D 80 67 80 61 FE 00 00 00 00 01 80 01 80 3F FC 3F FC 21 84 21 84 21 84 21 84 3F FC 21 84 01 80 01 80 01 80 01 80 00 00 00 00 7F FE 7F FE 60 06 6F F6 61 86 61 86 6F E6 61 86 61 A6 61 A6 6F F6 60 06 7F FE 7F FE 00 00 00 00 01 80 01 80 01 80 01 80 01 80 01 80 03 C0 02 40 06 60 0C 30 0C 10 38 1C 70 0E 40 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
// var str2="00 00 3F FC 3F 00 19 98 7F FE 7F FE 66 06 1F F8 04 00 0F F8 0F F8 1A 30 31 E0 73 F0 4E 1C 00 00 00 00 7F FE 7F FE 60 06 6F F6 61 86 61 86 6F E6 61 86 61 A6 61 A6 6F F6 60 06 7F FE 7F FE 00 00 00 00 01 00 3F F6 03 0C 01 18 7F FE 01 C0 03 80 1F F8 78 08 08 08 0F F8 08 08 0F F8 0F F8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
// var str3="00 00 3F FC 3F FC 01 80 01 80 01 00 7F FE 03 80 03 C0 02 40 06 60 0C 30 18 18 30 0C 60 06 00 00 00 00 7F FE 7F FE 01 00 01 00 01 C0 01 E0 01 38 01 0C 01 00 01 00 01 00 01 00 01 00 01 00 00 00 00 00 33 00 13 00 1B 00 7F FC 7F FC 02 04 02 04 02 84 06 C4 04 64 0C 24 18 0C 30 0C 60 78 00 00 00 00 04 20 0C 20 08 30 18 18 10 18 31 0C 63 06 43 02 06 20 04 30 0C 10 18 18 3F F8 18 0C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
// let arr1=('0x'+(str1.split(' ').join(",0x"))).split(',');
// let arr2=('0x'+(str2.split(' ').join(",0x"))).split(',');
// let arr3=('0x'+(str3.split(' ').join(",0x"))).split(',');
// result=[arr1,arr2,arr3];
console.log("result=",result);
let h3dic = [0x06, 0x07, 0x08];
let pros = [];
let flag = true;
@ -1475,7 +1566,7 @@
let width = str.length * 16;
var rgb = result[i];
this.addLog({
console.log({
date: Common.DateFormat(),
remark: '开始发送第' + (i + 1)
});
@ -1509,39 +1600,62 @@
// 发送数据通用方法
sendData(buffer) {
this.addLog("设备就绪,正在握手");
console.log("设备就绪,正在握手");
return new Promise((resolve, reject) => {
this.addLog("握手成功,正在发送");
this.addLog("设备id:" + this.connectedDeviceId);
this.addLog("服务id:" + this.serviceId);
this.addLog("特征id:" + this.writeCharacteristicId);
uni.writeBLECharacteristicValue({
deviceId: this.connectedDeviceId,
serviceId: this.serviceId,
characteristicId: this.writeCharacteristicId,
writeType:plus.os.name=='Android'?'writeNoResponse':'write',
value: buffer,
success: () => {
this.addLog({
date: Common.DateFormat(),
remark: "发送数据成功"
})
resolve();
},
fail: (err) => {
this.addLog({
date: Common.DateFormat(),
remark: "发送数据失败" + JSON.stringify(err)
})
reject(err);
},
complete: () => {
this.addLog({
date: Common.DateFormat(),
remark: "向设备写入数据完成"
})
console.log("握手成功,正在发送");
console.log("设备id:" + this.connectedDeviceId);
console.log("服务id:" + this.serviceId);
console.log("特征id:" + this.writeCharacteristicId);
let promise1=new Promise((succ,err)=>{
uni.writeBLECharacteristicValue({
deviceId: this.connectedDeviceId,
serviceId: this.serviceId,
characteristicId: this.writeCharacteristicId,
writeType:plus.os.name=='Android'?'writeNoResponse':'write',
value: buffer,
success: () => {
console.log({
date: Common.DateFormat(),
remark: "发送数据成功"
})
succ();
},
fail: (err) => {
console.log({
date: Common.DateFormat(),
remark: "发送数据失败" + JSON.stringify(err)
})
err(err);
},
complete: () => {
console.log({
date: Common.DateFormat(),
remark: "向设备写入数据完成"
})
}
});
});
let promise2=new Promise((succ,err)=>{
setTimeout(()=>{
err({
code: -1,
errMsg: '超时了'
});
},50)
})
Promise.race([promise1, promise2]).then(resolve).catch((ex) => {
console.log("ex=", ex);
if (ex.code == -1) {
resolve(ex);
} else {
reject(ex);
}
}).finally(() => {
//console.log("完成了")
});
});
},