蓝牙添加连接、断开、可用、不可用控制

This commit is contained in:
liub
2025-10-17 11:52:26 +08:00
parent 0ce4f4bef0
commit b114f6690d
13 changed files with 3024 additions and 407 deletions

View File

@ -34,6 +34,10 @@
<text class="lbl">充电状态</text>
<text class="value">{{formData.statu}}</text>
</view>
<view class="item">
<text class="lbl">蓝牙状态</text>
<text class="value" :class="formData.bleStatu?'green':'red'">{{formData.bleStatu?'已连接':'未连接'}}</text>
</view>
</view>
<view class="lamp">
<view class="title">
@ -158,7 +162,7 @@
var these = null;
var BrighInteval = null;
var recei = null;
var pagePath = "pages/7305/BJQ7305";
export default {
components: {
textToDotMatrix
@ -166,6 +170,7 @@
data() {
return {
Status: {
pageHide: false,
Pop: {
showPop: false, //是否显示弹窗
popType: 'custom',
@ -232,7 +237,8 @@
id: '',
deviceId: '',
textLines: ['', '', ''],
mode: ''
mode: '',
bleStatu: ''
},
inteval: 200,
device: {
@ -258,59 +264,18 @@
}
},
onUnload() {
ble.removeReceiveCallback(this);
ble.removeAllCallback(pagePath);
},
onLoad: function() {
these = this;
recei = BleReceive.getBleReceive();
ble = bleTool.getBleTool();
// let bleName = 'FB_Site_UART'; //JQZM-EF4651 FB_Site_UART
// let f = ble.data.LinkedList.find((v) => {
// if (v.name == bleName) {
// console.log("找到设备了", v);
// these.formData.deviceId = v.deviceId;
// return true;
// }
// return false;
// });
// let link = () => {
// if (bleName == 'FB_Site_UART') {
// ble.LinkBlue(f.deviceId, '0000AE30-0000-1000-8000-00805F9B34FB',
// '0000AE03-0000-1000-8000-00805F9B34FB', '0000AE02-0000-1000-8000-00805F9B34FB');
// } else {
// ble.LinkBlue(f.deviceId, '0000FFE0-0000-1000-8000-00805F9B34FB',
// '0000FFE1-0000-1000-8000-00805F9B34FB', '0000FFE2-0000-1000-8000-00805F9B34FB');
// }
// }
// if (!f) {
// ble.addDeviceFound((res) => {
// // console.log("发现新设备", res);
// f = res.devices.find((v) => {
// return v.name == bleName;
// });
// if (f) {
// console.log("找到目标设备了", f);
// these.formData.deviceId = f.deviceId;
// link();
// ble.StopSearch();
// }
// });
// ble.StartSearch();
// } else {
// link();
// }
console.log("6155");
ble.addReceiveCallback(these.bleValueNotify);
ble.addReceiveCallback(these.bleValueNotify, pagePath);
ble.addStateBreakCallback(these.bleStateBreak, pagePath);
ble.addStateRecoveryCallback(these.bleStateRecovry,pagePath);
ble.addDisposeCallback(these.deviceDispose,pagePath);
ble.addRecoveryCallback(these.deviceRecovry,pagePath);
let eventChannel = this.getOpenerEventChannel();
eventChannel.on('detailData', function(data) {
@ -346,7 +311,10 @@
these.formData.img = device.devicePic;
these.formData.id = device.id;
these.formData.deviceId = f.deviceId;
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId);
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res=>{
console.log("连接成功")
these.formData.bleStatu=true;
});
these.setBleFormData();
these.getDetail();
@ -355,12 +323,11 @@
},
onHide: function() {
this.Status.pageHide = true;
console.log("页面隐藏");
},
onBackPress(e) {
ble.removeReceiveCallback();
onShow: function() {
this.Status.pageHide = false;
},
computed: {
RSSIRemark: function() {
@ -410,6 +377,76 @@
}
},
methods: {
deviceRecovry(res){
if(this.Status.pageHide){
return;
}
if(res.deviceId==these.formData.deviceId){
this.formData.bleStatu=true;
setTimeout(()=>{
hideLoading(these,1000);
});
uni.showToast({
icon:'success',
title:'蓝牙连接成功'
});
}
},
deviceDispose(res){
if(this.Status.pageHide){
return;
}
if(res.deviceId==these.formData.deviceId){
this.formData.bleStatu=false;
setTimeout(()=>{
hideLoading(these,1000);
});
uni.showToast({
icon:'fail',
title:'蓝牙连接已断开'
});
}
},
bleStateBreak() {
if(this.Status.pageHide){
return;
}
//蓝牙适配器不可用
this.formData.bleStatu = false;
uni.showToast({
icon: 'fail',
title: '蓝牙已不可用'
})
hideLoading(this);
},
bleStateRecovry() {
console.log("蓝牙可用");
if(this.Status.pageHide){
return;
}
console.log("蓝牙可用");
//蓝牙适配器再次可用,尝试恢复连接
showLoading(this, {
text: "蓝牙恢复可用,正在连接设备"
});
ble.LinkBlue(these.formData.deviceId).then(() => {
these.formData.bleStatu = true;
updateLoading(these,{
text:'连接成功'
});
}).catch(ex => {
updateLoading(these, {
text: ex.msg
})
}).finally(() => {
setTimeout(() => {
hideLoading(these);
}, 1000);
});
},
previewImg(img) {
if (!img) {
return;
@ -429,19 +466,21 @@
return f;
},
bleValueNotify: function(receive, device, path) {
let json = recei.ReceiveData(receive, device, path);
if(!json){
if (this.Status.pageHide) {
return;
}
let keys=Object.keys(json);
keys.forEach((key)=>{
if(key in these.formData){
these.formData[key] = json[key];
}
let json = recei.ReceiveData(receive, device, path);
if (!json) {
return;
}
let keys = Object.keys(json);
keys.forEach((key) => {
if (key in these.formData) {
these.formData[key] = json[key];
}
});
if (this.formData.battary <= 20) {
if ('battary' in json && this.formData.battary <= 20) {
this.showPop({
message: "设备电量低",
iconUrl: "/static/images/6155/DeviceDetail/uploadErr.png",
@ -491,6 +530,7 @@
events: {
BindOver: function(data) {
console.log(data)
these.formData.bleStatu=true;
}
},
success: function(res) {
@ -521,37 +561,37 @@
// 分包发送图片数据
var sendImagePackets = function(packetData) {
return new Promise((resolve, reject) => {
// 总数据包数
const totalPackets = 3;
let currentPacket = 1;
// 发送单个数据包
const sendNextPacket = () => {
if (currentPacket > totalPackets) {
setTimeout(() => {
hideLoading(these);
these.Status.BottomMenu.show = false;
these.showPop({
message: "上传成功",
iconUrl: "/static/images/6155/DeviceDetail/uploadSuccess.png",
borderColor: '#BBE600',
buttonBgColor: '#BBE600'
});
resolve();
}, 0)
return;
}
let start = 0;
let bufferSize = 343; //总共1029字节3包正好每包343字节
let bufferSize = 343; //总共1029字节3包正好每包343字节
//FA 09 04 00 1024字节 FF
var buffer = new ArrayBuffer(bufferSize);
var dataView = new DataView(buffer);
@ -560,37 +600,37 @@
dataView.setUint8(1, 0x09); // 帧头
dataView.setUint8(2, 0x04); // 帧头
dataView.setUint8(3, 0x00); // 帧头
for (let i = 0; i < packetData.length; i++) {
dataView.setUint8(4 + i, parseInt(packetData[i],16));
dataView.setUint8(4 + i, parseInt(packetData[i], 16));
}
}
if (currentPacket == totalPackets) {
if (currentPacket == totalPackets) {
dataView.setUint8(bufferSize - 1, 0xFF);
}
//发送数据包
ble.sendData(f.deviceId, buffer, f.writeServiceId, f.wirteCharactId,
30)
.then(() => {
updateLoading(these, {
text: "正在发送" + currentPacket + "/"+totalPackets
text: "正在发送" + currentPacket + "/" + totalPackets
})
currentPacket++;
setTimeout(sendNextPacket, 100);
}).catch(err => {
if (err.code == 10007) {
setTimeout(sendNextPacket, 100);
return;
}
console.log("发送数据包失败了", err);
these.Status.BottomMenu.show = false;
these.showPop({
message: "发送数据包失败了" + err.msg,
@ -598,14 +638,14 @@
borderColor: "#e034344d",
buttonBgColor: "#E03434",
});
hideLoading(these);
reject(err);
});
}
// 开始发送数据
sendNextPacket();
});
}
@ -620,7 +660,7 @@
url: "/pages/6155/ImgCrop",
events: {
ImgCutOver: function(data) {
//将8位的二进制数组转换成16进制数据
let binaryToHex = (binaryArray) => {
if (!Array.isArray(binaryArray) || binaryArray
.length !== 8) {
@ -652,19 +692,14 @@
let imageData = data.piexls;
let arr = [];
for (let x = 0; x < imageData.length; x += 4) {
let R = imageData[x];
let G = imageData[x + 1];
let B = imageData[x + 2];
let A = imageData[x + 3];
if (A < 128) {
arr.push(0);
continue;
}
for (let currIndex = 0; currIndex < imageData
.length; currIndex += 4) {
let R = imageData[currIndex];
let G = imageData[currIndex + 1];
let B = imageData[currIndex + 2];
let A = imageData[currIndex + 3];
let grayVal = Math.floor(R * 0.299 + G * 0.587 + B *
0.114);
// console.log("RGBA", R + "," + G + "," + B + "," + A +
// ',' + grayVal);
arr.push(grayVal < 128 ? 1 : 0);
}
@ -732,6 +767,8 @@
sendImagePackets(matrix).catch((
ex) => {
console.log("出现异常", ex);
hideLoading(these);
});
}, 0)
@ -741,7 +778,8 @@
ev.eventChannel.emit('checkImg', {
data: res.tempFiles[0].path,
width: 64,
height: 32
height: 32,
times: 10
})
},
fail(ex) {
@ -1199,7 +1237,14 @@
if (f) {
// 发送数据
ble.sendData(f.deviceId, buffer, f.writeServiceId, f.wirteCharactId, 100);
ble.sendData(f.deviceId, buffer, f.writeServiceId, f.wirteCharactId, 100).catch(ex=>{
these.showPop({
message: "发送失败," + ex.msg,
iconUrl: "/static/images/6155/DeviceDetail/uploadErr.png",
borderColor: "#e034344d",
buttonBgColor: "#E03434",
});
});
}
@ -1241,7 +1286,14 @@
if (f) {
// 发送数据
ble.sendData(f.deviceId, buffer, f.writeServiceId, f.wirteCharactId, 100);
ble.sendData(f.deviceId, buffer, f.writeServiceId, f.wirteCharactId, 100).catch(ex=>{
these.showPop({
message: "发送失败," + ex.msg,
iconUrl: "/static/images/6155/DeviceDetail/uploadErr.png",
borderColor: "#e034344d",
buttonBgColor: "#E03434",
});
});
}
@ -1429,6 +1481,7 @@
letter-spacing: 0.07px;
text-align: left;
}
.lamp {
margin-top: 24rpx;