添加蓝牙通讯握手
This commit is contained in:
@ -144,8 +144,7 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import ble from '../../api/6155/BlueHelper';
|
||||
import { deviceReName } from '../../api/common';
|
||||
import ble from '@/api/6155/BlueHelper';
|
||||
|
||||
export default {
|
||||
data() {
|
||||
@ -230,7 +229,7 @@ import { deviceReName } from '../../api/common';
|
||||
RSSI: '-30',
|
||||
statu: '运行中',
|
||||
liangDu: '50',
|
||||
id:''
|
||||
id: ''
|
||||
},
|
||||
cEdit: {
|
||||
mode: ''
|
||||
@ -242,7 +241,11 @@ import { deviceReName } from '../../api/common';
|
||||
id: 'HSXF01061'
|
||||
},
|
||||
device: {
|
||||
deviceId: ''
|
||||
deviceId: '',
|
||||
writeServiceId: '',
|
||||
wirteCharactId: '',
|
||||
notifyServiceid:'',
|
||||
notifyCharactId:''
|
||||
}
|
||||
}
|
||||
},
|
||||
@ -253,20 +256,20 @@ import { deviceReName } from '../../api/common';
|
||||
|
||||
eventChannel.on('detailData', function(data) {
|
||||
|
||||
|
||||
|
||||
console.log("收到父页面的参数:" + JSON.stringify(data));
|
||||
var device = data.data;
|
||||
these.device.deviceId = device.deviceMac;
|
||||
these.formData.name=device.bluetoothName?device.bluetoothName:device.deviceName;
|
||||
these.formData.img=device.devicePic;
|
||||
these.formData.id=device.id;
|
||||
these.formData.name = device.bluetoothName ? device.bluetoothName : device.deviceName;
|
||||
these.formData.img = device.devicePic;
|
||||
these.formData.id = device.id;
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
ble.featrueValueCallback = these.bleValueNotify;
|
||||
|
||||
|
||||
|
||||
|
||||
},
|
||||
onHide: function() {
|
||||
|
||||
@ -299,7 +302,7 @@ import { deviceReName } from '../../api/common';
|
||||
content: '是否断开与设备的蓝牙连接?',
|
||||
success: function(res) {
|
||||
if (res.confirm) {
|
||||
disconnectDevice(these.device.deviceId);
|
||||
ble.disconnectDevice(these.device.deviceId);
|
||||
|
||||
}
|
||||
uni.navigateBack();
|
||||
@ -332,6 +335,49 @@ import { deviceReName } from '../../api/common';
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
getDevice: function() {
|
||||
var these = this;
|
||||
|
||||
if (this.device.deviceId && this.device.writeServiceId && this.device.wirteCharactId) {
|
||||
return this.device;
|
||||
}
|
||||
|
||||
let key = "linkedDevices";
|
||||
var store = uni.getStorageInfoSync();
|
||||
var f = store.keys.find(function(v) {
|
||||
return v == key;
|
||||
});
|
||||
// console.log("倒计时:5");
|
||||
var linkedDevices = [];
|
||||
if (f) {
|
||||
var str = uni.getStorageSync(key);
|
||||
if (str) {
|
||||
linkedDevices = JSON.parse(str);
|
||||
}
|
||||
|
||||
}
|
||||
// console.log("倒计时:4");
|
||||
if (linkedDevices && linkedDevices.length && linkedDevices.length > 0) {
|
||||
// console.log("倒计时:3");
|
||||
f = linkedDevices.find(function(v) {
|
||||
if(v.deviceId == these.device.deviceId){
|
||||
these.device.writeServiceId=v.writeServiceId;
|
||||
these.device.wirteCharactId=v.wirteCharactId;
|
||||
these.device.notifyServiceid=v.notifyServiceid;
|
||||
these.device.notifyCharactId=v.notifyCharactId;
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
|
||||
});
|
||||
|
||||
} else {
|
||||
f = null;
|
||||
}
|
||||
return f;
|
||||
},
|
||||
bleValueNotify: function(data) {
|
||||
console.log("读取到设备发送的数据:" + JSON.stringify(data));
|
||||
// data.characteristicId
|
||||
@ -343,9 +389,9 @@ import { deviceReName } from '../../api/common';
|
||||
},
|
||||
|
||||
proParam: function() {
|
||||
|
||||
|
||||
uni.showToast({
|
||||
title:'敬请期待'
|
||||
title: '敬请期待'
|
||||
})
|
||||
},
|
||||
handRemark: function() {
|
||||
@ -404,139 +450,114 @@ import { deviceReName } from '../../api/common';
|
||||
// 分包发送图片数据
|
||||
var sendImagePackets = function(imageData) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// this.isSending = true;
|
||||
// this.progress = 0;
|
||||
// this.currentPacket = 0;
|
||||
|
||||
// 总数据包数
|
||||
const totalPackets = 52;
|
||||
let currentPacket = 1;
|
||||
|
||||
let key = "linkedDevices";
|
||||
var store = uni.getStorageInfoSync();
|
||||
var f = store.keys.find(function(v) {
|
||||
return v == key;
|
||||
});
|
||||
// console.log("倒计时:5");
|
||||
var linkedDevices = [];
|
||||
f = these.getDevice();
|
||||
if (f) {
|
||||
var str = uni.getStorageSync(key);
|
||||
if (str) {
|
||||
linkedDevices = JSON.parse(str);
|
||||
}
|
||||
|
||||
}
|
||||
// console.log("倒计时:4");
|
||||
if (linkedDevices && linkedDevices.length && linkedDevices.length > 0) {
|
||||
// console.log("倒计时:3");
|
||||
f = linkedDevices.find(function(v) {
|
||||
return v.deviceId == these.device.deviceId;
|
||||
|
||||
});
|
||||
if (f) {
|
||||
// 发送单个数据包
|
||||
const sendNextPacket = () => {
|
||||
if (currentPacket > totalPackets) {
|
||||
uni.hideLoading();
|
||||
these.Status.BottomMenu.show = false;
|
||||
these.Status.Progress.show = false;
|
||||
these.Status.Pop.showPop = true;
|
||||
these.Status.Pop.message = "上传成功";
|
||||
these.Status.Pop.iconUrl =
|
||||
"/static/images/6155/DeviceDetail/uploadSuccess.png";
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
// 计算当前包的数据
|
||||
let packetSize = 250;
|
||||
if (currentPacket <= 51) {
|
||||
packetSize = 250; // 前51个包每个500字节
|
||||
} else {
|
||||
packetSize = 50; // 最后一个包100字节
|
||||
}
|
||||
|
||||
// 创建数据包
|
||||
const startIndex = (currentPacket - 1) * packetSize;
|
||||
const endIndex = Math.min(startIndex + packetSize, imageData
|
||||
.length);
|
||||
if (startIndex > endIndex) {
|
||||
return;
|
||||
}
|
||||
const packetData = imageData.slice(startIndex,
|
||||
endIndex); // imageData.subarray(startIndex, endIndex);
|
||||
console.log("imageData.length=" + imageData.length +
|
||||
",startIndex=" +
|
||||
startIndex +
|
||||
",endIndex=" + endIndex + ",数据包长度" + (endIndex -
|
||||
startIndex) +
|
||||
',packetData.length=' + packetData.length);
|
||||
// 构建数据包
|
||||
const bufferSize = 5 + packetData.length * 2; // 头部5字节 + 数据部分
|
||||
const buffer = new ArrayBuffer(bufferSize);
|
||||
const dataView = new DataView(buffer);
|
||||
|
||||
// 填充头部
|
||||
dataView.setUint8(0, 0x55); // 帧头
|
||||
dataView.setUint8(1, 0x02); // 帧类型:开机画面
|
||||
dataView.setUint8(2, '0x' + currentPacket.toString(16).padStart(2,
|
||||
'0')); // 包序号
|
||||
// if(packetData.length==250)
|
||||
// {
|
||||
// dataView.setUint8(3, 0x01);
|
||||
// dataView.setUint8(4, 0xFF);
|
||||
// }
|
||||
|
||||
if (packetData.length == 250) {
|
||||
dataView.setUint8(3, 0x01);
|
||||
dataView.setUint8(4, 0xF4);
|
||||
} else {
|
||||
dataView.setUint8(3, 0x00);
|
||||
dataView.setUint8(4, 0x64);
|
||||
}
|
||||
|
||||
// 填充数据(每个RGB565值占2字节)
|
||||
for (let i = 0; i < packetData.length; i++) {
|
||||
dataView.setUint16(5 + i * 2, packetData[i], false); // 大端字节序
|
||||
}
|
||||
console.log(
|
||||
`发送数据包${currentPacket}/${totalPackets},${dataView.getUint8(0)} ${dataView.getUint8(1)} ${dataView.getUint8(2)}`
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
//发送数据包
|
||||
ble.sendDataNew(f.deviceId, f.writeServiceId, f.wirteCharactId, buffer)
|
||||
.then(() => {
|
||||
console.log("发送一个包完成了");
|
||||
these.Status.Progress.curr = currentPacket;
|
||||
currentPacket++;
|
||||
|
||||
setTimeout(sendNextPacket, 100);
|
||||
}).catch(err => {
|
||||
console.log("发送数据包失败了" + JSON.stringify(err));
|
||||
// this.isSending = false;
|
||||
these.Status.BottomMenu.show = false;
|
||||
these.Status.Progress.show = false;
|
||||
these.alert("发送数据包失败了");
|
||||
uni.hideLoading();
|
||||
reject(err);
|
||||
});
|
||||
// 发送单个数据包
|
||||
const sendNextPacket = () => {
|
||||
if (currentPacket > totalPackets) {
|
||||
uni.hideLoading();
|
||||
these.Status.BottomMenu.show = false;
|
||||
these.Status.Progress.show = false;
|
||||
these.Status.Pop.showPop = true;
|
||||
these.Status.Pop.message = "上传成功";
|
||||
these.Status.Pop.iconUrl =
|
||||
"/static/images/6155/DeviceDetail/uploadSuccess.png";
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// 开始发送数据
|
||||
sendNextPacket();
|
||||
// 计算当前包的数据
|
||||
let packetSize = 250;
|
||||
if (currentPacket <= 51) {
|
||||
packetSize = 250; // 前51个包每个500字节
|
||||
} else {
|
||||
packetSize = 50; // 最后一个包100字节
|
||||
}
|
||||
|
||||
// 创建数据包
|
||||
const startIndex = (currentPacket - 1) * packetSize;
|
||||
const endIndex = Math.min(startIndex + packetSize, imageData
|
||||
.length);
|
||||
if (startIndex > endIndex) {
|
||||
return;
|
||||
}
|
||||
const packetData = imageData.slice(startIndex,
|
||||
endIndex); // imageData.subarray(startIndex, endIndex);
|
||||
console.log("imageData.length=" + imageData.length +
|
||||
",startIndex=" +
|
||||
startIndex +
|
||||
",endIndex=" + endIndex + ",数据包长度" + (endIndex -
|
||||
startIndex) +
|
||||
',packetData.length=' + packetData.length);
|
||||
// 构建数据包
|
||||
const bufferSize = 5 + packetData.length * 2; // 头部5字节 + 数据部分
|
||||
const buffer = new ArrayBuffer(bufferSize);
|
||||
const dataView = new DataView(buffer);
|
||||
|
||||
// 填充头部
|
||||
dataView.setUint8(0, 0x55); // 帧头
|
||||
dataView.setUint8(1, 0x02); // 帧类型:开机画面
|
||||
dataView.setUint8(2, '0x' + currentPacket.toString(16).padStart(2,
|
||||
'0')); // 包序号
|
||||
// if(packetData.length==250)
|
||||
// {
|
||||
// dataView.setUint8(3, 0x01);
|
||||
// dataView.setUint8(4, 0xFF);
|
||||
// }
|
||||
|
||||
if (packetData.length == 250) {
|
||||
dataView.setUint8(3, 0x01);
|
||||
dataView.setUint8(4, 0xF4);
|
||||
} else {
|
||||
dataView.setUint8(3, 0x00);
|
||||
dataView.setUint8(4, 0x64);
|
||||
}
|
||||
|
||||
// 填充数据(每个RGB565值占2字节)
|
||||
for (let i = 0; i < packetData.length; i++) {
|
||||
dataView.setUint16(5 + i * 2, packetData[i], false); // 大端字节序
|
||||
}
|
||||
console.log(
|
||||
`发送数据包${currentPacket}/${totalPackets},${dataView.getUint8(0)} ${dataView.getUint8(1)} ${dataView.getUint8(2)}`
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
//发送数据包
|
||||
ble.sendDataNew(f.deviceId, f.writeServiceId, f.wirteCharactId, buffer)
|
||||
.then(() => {
|
||||
console.log("发送一个包完成了");
|
||||
these.Status.Progress.curr = currentPacket;
|
||||
currentPacket++;
|
||||
|
||||
setTimeout(sendNextPacket, 100);
|
||||
}).catch(err => {
|
||||
console.log("发送数据包失败了" + JSON.stringify(err));
|
||||
// this.isSending = false;
|
||||
these.Status.BottomMenu.show = false;
|
||||
these.Status.Progress.show = false;
|
||||
these.alert("发送数据包失败了");
|
||||
uni.hideLoading();
|
||||
reject(err);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 开始发送数据
|
||||
sendNextPacket();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@ -572,7 +593,7 @@ import { deviceReName } from '../../api/common';
|
||||
})
|
||||
},
|
||||
fail(ex) {
|
||||
console.log("跳转页面失败"+JSON.stringify(ex));
|
||||
console.log("跳转页面失败" + JSON.stringify(ex));
|
||||
}
|
||||
});
|
||||
|
||||
@ -594,8 +615,7 @@ import { deviceReName } from '../../api/common';
|
||||
switch (type) {
|
||||
case "main":
|
||||
title = '主灯模式';
|
||||
items = [
|
||||
{
|
||||
items = [{
|
||||
text: '强光',
|
||||
icon: '/static/images/6155/DeviceDetail/qiang.png'
|
||||
},
|
||||
@ -682,24 +702,26 @@ import { deviceReName } from '../../api/common';
|
||||
const dataView = new DataView(buffer);
|
||||
|
||||
dataView.setUint8(0, 0x4A); // 帧头
|
||||
dataView.setUint8(1, 0x51);
|
||||
dataView.setUint8(2, 0x4F);
|
||||
dataView.setUint8(3, 0x43);
|
||||
|
||||
|
||||
if(this.Status.BottomMenu.type=='main'){
|
||||
dataView.setUint8(1, 0x51);
|
||||
dataView.setUint8(2, 0x4F);
|
||||
dataView.setUint8(3, 0x43);
|
||||
|
||||
|
||||
if (this.Status.BottomMenu.type == 'main') {
|
||||
//主灯模式
|
||||
dataView.setUint8(4, 0x04); // 帧类型
|
||||
}else{
|
||||
} else {
|
||||
//副灯模式
|
||||
dataView.setUint8(4, 0x05); // 帧类型
|
||||
}
|
||||
}
|
||||
dataView.setUint8(5, dataValue); // 数据
|
||||
dataView.setUint16(6, '9C41');
|
||||
|
||||
let f = this.getDevice();
|
||||
// 发送数据
|
||||
ble.LinkBlue(this.device.deviceId, () => {
|
||||
ble.LinkBlue(f.deviceId, () => {
|
||||
setTimeout(() => {
|
||||
ble.sendData(this.device.deviceId, buffer);
|
||||
ble.sendDataNew(f.deviceId, f.writeServiceId, f.wirteCharactId, buffer);
|
||||
}, 1000);
|
||||
|
||||
});
|
||||
@ -800,28 +822,33 @@ import { deviceReName } from '../../api/common';
|
||||
|
||||
|
||||
console.log("dataView.length=" + dataView.byteLength)
|
||||
// 发送数据包
|
||||
ble.LinkBlue(these.device.deviceId, () => {
|
||||
setTimeout(() => {
|
||||
ble.sendData(these.device.deviceId, buffer).then(() => {
|
||||
// 更新进度
|
||||
console.log(
|
||||
`发送文字数据包 ${currentPacket}/${totalPackets}: ${text}`
|
||||
);
|
||||
|
||||
// 发送下一个包
|
||||
currentPacket++;
|
||||
setTimeout(sendNextPacket, 100);
|
||||
}).catch(err => {
|
||||
// this.isSendingText = false;
|
||||
uni.showToast({
|
||||
title: '文字发送失败',
|
||||
icon: 'none'
|
||||
let f = these.getDevice();
|
||||
if (f) {
|
||||
// 发送数据包
|
||||
ble.LinkBlue(f.deviceId, () => {
|
||||
setTimeout(() => {
|
||||
ble.sendDataNew(f.deviceId, f.writeServiceId, f
|
||||
.wirteCharactId, buffer).then(() => {
|
||||
// 更新进度
|
||||
console.log(
|
||||
`发送文字数据包 ${currentPacket}/${totalPackets}: ${text}`
|
||||
);
|
||||
|
||||
// 发送下一个包
|
||||
currentPacket++;
|
||||
setTimeout(sendNextPacket, 100);
|
||||
}).catch(err => {
|
||||
// this.isSendingText = false;
|
||||
uni.showToast({
|
||||
title: '文字发送失败',
|
||||
icon: 'none'
|
||||
});
|
||||
});
|
||||
});
|
||||
}, 0);
|
||||
}, 0);
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// }).catch(err => {
|
||||
// this.isSendingText = false;
|
||||
@ -868,41 +895,43 @@ import { deviceReName } from '../../api/common';
|
||||
dataView.setUint8(3, 0x00); // 数据长度
|
||||
dataView.setUint8(4, 0x01); // 数据长度
|
||||
dataView.setUint8(5, data); // 数据
|
||||
let f = this.getDevice();
|
||||
if (f) {
|
||||
// 发送数据
|
||||
ble.LinkBlue(f.deviceId, () => {
|
||||
setTimeout(() => {
|
||||
ble.sendDataNew(f.deviceId, f.writeServiceId, f.wirteCharactId, buffer);
|
||||
}, 1000)
|
||||
|
||||
// 发送数据
|
||||
ble.LinkBlue(this.device.deviceId, () => {
|
||||
setTimeout(() => {
|
||||
ble.sendData(this.device.deviceId, buffer);
|
||||
}, 1000)
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
},
|
||||
//获取当前设备状态
|
||||
GetDeviceStatu:function(){
|
||||
GetDeviceStatu: function() {
|
||||
//数据头 命令码 数据 校验码
|
||||
//JQOC 0B 00 9C41
|
||||
|
||||
|
||||
//硬件响应
|
||||
//数据头 命令码 主灯模式 副灯模式 亮度(%) 电量(%) 充电状态 校验码
|
||||
//JQOC 0B 01 01 64 85 01 9C41
|
||||
this.sendCmd('0x0B');
|
||||
},
|
||||
//设备重启
|
||||
DeviceReStart:function(){
|
||||
DeviceReStart: function() {
|
||||
//数据头 命令码 数据 校验码
|
||||
//JQOC 0C 01 9C412
|
||||
this.sendCmd('0x0C');
|
||||
},
|
||||
//恢复出厂设置
|
||||
DeviceReSet:function(){
|
||||
DeviceReSet: function() {
|
||||
//数据头 命令码 数据 校验码
|
||||
//JQOC 0D 01 9C412
|
||||
this.sendCmd('0x0D');
|
||||
},
|
||||
sendCmd:function(type,callback){
|
||||
sendCmd: function(type, callback) {
|
||||
//数据头 命令码 数据 校验码
|
||||
|
||||
|
||||
const buffer = new ArrayBuffer(7);
|
||||
const dataView = new DataView(buffer);
|
||||
let data = '0x' + parseInt(this.formData.liangDu).toString(16);
|
||||
@ -911,16 +940,19 @@ import { deviceReName } from '../../api/common';
|
||||
dataView.setUint8(1, type); // 帧类型:亮度调节
|
||||
dataView.setUint8(2, '0x01'); // 包序号
|
||||
dataView.setUint8(3, '9C41'); // 数据长度
|
||||
|
||||
// 发送数据
|
||||
ble.LinkBlue(this.device.deviceId, () => {
|
||||
setTimeout(() => {
|
||||
ble.sendData(this.device.deviceId, buffer);
|
||||
}, 100)
|
||||
});
|
||||
let f = this.getDevice();
|
||||
if (f) {
|
||||
// 发送数据
|
||||
ble.LinkBlue(f.deviceId, () => {
|
||||
setTimeout(() => {
|
||||
ble.sendDataNew(f.deviceId, f.writeServiceId, f.wirteCharactId, buffer);
|
||||
}, 100)
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
Reference in New Issue
Block a user