11
This commit is contained in:
parent
20ae23b88a
commit
55399cd5c1
|
@ -241,32 +241,32 @@ export default {
|
|||
rings() {
|
||||
console.log('准备发送响铃命令');
|
||||
|
||||
if(!this.hasConnectedDevice || !this.selectedDevice) {
|
||||
uni.showToast({
|
||||
title: '请先连接设备',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 先获取服务列表
|
||||
my.getBLEDeviceServices({
|
||||
deviceId: this.selectedDevice.deviceId,
|
||||
success: (res) => {
|
||||
console.log('设备的所有服务:', res.services);
|
||||
// 发送响铃命令
|
||||
xBlufi.notifySendCustomData({
|
||||
customData: "11play1@"
|
||||
});
|
||||
},
|
||||
fail: (error) => {
|
||||
console.error('获取服务列表失败:', error);
|
||||
uni.showToast({
|
||||
title: '获取服务失败',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
});
|
||||
if (!this.hasConnectedDevice || !this.selectedDevice) {
|
||||
uni.showToast({
|
||||
title: '请先连接设备',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
console.log(this.selectedDevice.deviceId,'this.selectedDevice.deviceId');
|
||||
// 先获取服务列表
|
||||
my.getBLEDeviceServices({
|
||||
deviceId: this.selectedDevice.deviceId,
|
||||
success: (res) => {
|
||||
console.log('设备的所有服务:', res.services);
|
||||
// 发送响铃命令
|
||||
xBlufi.notifySendCustomData({
|
||||
customData: "11play1@"
|
||||
});
|
||||
},
|
||||
fail: (error) => {
|
||||
console.error('获取服务列表失败:', error);
|
||||
uni.showToast({
|
||||
title: '获取服务失败',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
|
||||
|
|
|
@ -41,14 +41,12 @@
|
|||
确认添加
|
||||
</view>
|
||||
<view class="bot_btn">
|
||||
|
||||
<view class="btn1" @click="Binddevice()">
|
||||
蓝牙链接
|
||||
</view>
|
||||
<view class="btn1" @click="ring()" v-if="carstause">
|
||||
响铃寻车
|
||||
</view>
|
||||
|
||||
<view class="btn1" @click="open()" v-if="carstause">
|
||||
蓝牙开锁
|
||||
</view>
|
||||
|
@ -58,9 +56,6 @@
|
|||
<view class="btn1" @click="reboot()" v-if="carstause">
|
||||
设备重启
|
||||
</view>
|
||||
<view class="btn1" @click="Binddevice()" v-if="isbands">
|
||||
接口响铃
|
||||
</view>
|
||||
</view>
|
||||
<u-mask :show="showbind" :z-index='100' />
|
||||
<view class="tip_box" v-if="showbind">
|
||||
|
@ -95,128 +90,265 @@
|
|||
</template>
|
||||
|
||||
<script>
|
||||
const app = getApp();
|
||||
var xBlufi = require("@/utils/blufi/xBlufi.js");
|
||||
let _this = null;
|
||||
import xBlufi from '@/utils/blufi/xBlufi.js'
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
devicesList: [
|
||||
// {name:'110000',
|
||||
// mac:'11111'}
|
||||
],
|
||||
searching: false,
|
||||
texts: '正在扫描蓝牙设备...',
|
||||
btnflag: true,
|
||||
tishiflag: false,
|
||||
option: '',
|
||||
bluthlist: [], //蓝牙数组
|
||||
// status: 'loading',
|
||||
statusflag: false,
|
||||
Bluetoothmac: '',
|
||||
// 蓝牙相关数据
|
||||
devices: [],
|
||||
isSearching: false,
|
||||
isConnecting: false,
|
||||
bluetoothStatus: '未初始化',
|
||||
selectedDevice: null,
|
||||
hasConnectedDevice: false,
|
||||
serviceId: '000000FF-0000-1000-8000-00805F9B34FB',
|
||||
characteristicId: '0000FF01-0000-1000-8000-00805F9B34FB',
|
||||
|
||||
// 原有的业务数据
|
||||
mac: '',
|
||||
ishave: false,
|
||||
ver_data: null,
|
||||
deviceInfoss: {},
|
||||
gps: {},
|
||||
isband: false,
|
||||
// status: false,
|
||||
deviceIds: '',
|
||||
name: '',
|
||||
orderinfo: {},
|
||||
dl: 0,
|
||||
czmoney: true,
|
||||
iscz: true,
|
||||
bgc: {
|
||||
backgroundColor: "#F7FAFE",
|
||||
},
|
||||
sn: '',
|
||||
carstause: true,
|
||||
isbands: false,
|
||||
carstause: false,
|
||||
show: false,
|
||||
list: [],
|
||||
hardwareInfo: {},
|
||||
showbind: false,
|
||||
|
||||
|
||||
|
||||
|
||||
bgc: {
|
||||
backgroundColor: "#F7FAFE",
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
onLoad(e) {
|
||||
this.mac = e.mac
|
||||
this.Binddevice()
|
||||
this.isbands = false
|
||||
this.carstause = false
|
||||
this.initBluetooth()
|
||||
this.getlist()
|
||||
|
||||
// 添加自动连接
|
||||
if (this.mac) {
|
||||
setTimeout(() => {
|
||||
this.Binddevice()
|
||||
}, 1000) // 延迟1秒执行,确保蓝牙初始化完成
|
||||
}
|
||||
// 添加蓝牙状态监听
|
||||
my.onBluetoothAdapterStateChange((res) => {
|
||||
console.log('蓝牙适配器状态:', res);
|
||||
if (!res.available) {
|
||||
this.searching = false;
|
||||
this.isSearching = false;
|
||||
uni.showToast({
|
||||
title: '蓝牙已关闭',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
});
|
||||
my.onBLEConnectionStateChanged(function(res) {
|
||||
console.log('蓝牙连接状态变化:', res);
|
||||
if (!res.connected) {
|
||||
// this.carstause = false
|
||||
// // 断开连接后的处理
|
||||
// let obj = {
|
||||
// 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_CLOSE_CONNECTED,
|
||||
// 'result': false,
|
||||
// 'data': ''
|
||||
// };
|
||||
// mDeviceEvent.notifyDeviceMsgEvent(obj);
|
||||
}else{
|
||||
// this.carstause = true
|
||||
}
|
||||
});
|
||||
},
|
||||
onShow() {
|
||||
this.getlist()
|
||||
},
|
||||
onUnload: function () {
|
||||
xBlufi.listenDeviceMsgEvent(false, this.funListenDeviceMsgEvent);
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': false
|
||||
|
||||
my.onBLEConnectionStateChanged((res) => {
|
||||
console.log('蓝牙连接状态变化:', res);
|
||||
if (!res.connected) {
|
||||
this.carstause = false;
|
||||
this.hasConnectedDevice = false;
|
||||
} else {
|
||||
this.carstause = true;
|
||||
this.hasConnectedDevice = true;
|
||||
}
|
||||
});
|
||||
},
|
||||
onUnload() {
|
||||
this.cleanupBluetooth()
|
||||
},
|
||||
|
||||
onHide() {
|
||||
xBlufi.listenDeviceMsgEvent(false, this.funListenDeviceMsgEvent);
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': false
|
||||
});
|
||||
},
|
||||
onBeforeUnmount() {
|
||||
xBlufi.listenDeviceMsgEvent(false, this.funListenDeviceMsgEvent);
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': false
|
||||
});
|
||||
},
|
||||
watch: {
|
||||
|
||||
|
||||
|
||||
this.cleanupBluetooth()
|
||||
},
|
||||
methods: {
|
||||
confirm(e) {
|
||||
// console.log(e);
|
||||
uni.setStorageSync('hardwareVersionId', e[0].value);
|
||||
this.hardwareVersionId = e[0].value
|
||||
const hardwareVersionId = this.hardwareVersionId
|
||||
initBluetooth() {
|
||||
xBlufi.initXBlufi(xBlufi.XMQTT_SYSTEM.Alis);
|
||||
|
||||
if (hardwareVersionId) {
|
||||
const matchedVersion = this.Versionlist.find(version => version.id ===
|
||||
hardwareVersionId);
|
||||
if (matchedVersion) {
|
||||
this.hardwareInfo = matchedVersion;
|
||||
xBlufi.listenDeviceMsgEvent(true, (res) => {
|
||||
console.log('设备消息:', res);
|
||||
switch (res.type) {
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
|
||||
if (res.result && res.data) {
|
||||
console.log("搜索到设备:", res.data);
|
||||
const bbleDevices = res.data.filter(device => {
|
||||
const name = device.name || device.deviceName || '';
|
||||
return name.startsWith('BBLE');
|
||||
});
|
||||
this.devices = bbleDevices;
|
||||
}
|
||||
break;
|
||||
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
|
||||
if (res.result) {
|
||||
this.carstause = true;
|
||||
this.hasConnectedDevice = true;
|
||||
uni.showToast({
|
||||
title: '连接成功',
|
||||
icon: 'success',
|
||||
duration: 1000
|
||||
});
|
||||
} else {
|
||||
this.carstause = false;
|
||||
this.hasConnectedDevice = false;
|
||||
uni.showToast({
|
||||
title: '连接断开',
|
||||
icon: 'none',
|
||||
duration: 1000
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_SEND_CUSTOM_DATA_RESULT:
|
||||
console.log('发送自定义数据结果:', res);
|
||||
if (res.result) {
|
||||
uni.showToast({
|
||||
title: '命令发送成功',
|
||||
icon: 'success',
|
||||
duration: 1000
|
||||
});
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '命令发送失败',
|
||||
icon: 'none',
|
||||
duration: 1000
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// 蓝牙连接方法
|
||||
Binddevice() {
|
||||
uni.showLoading({
|
||||
title: '连接中..'
|
||||
});
|
||||
|
||||
this.devices = [];
|
||||
this.isSearching = true;
|
||||
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': true
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': false
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
uni.hideLoading();
|
||||
if (this.devices.length == 0) {
|
||||
uni.showToast({
|
||||
title: '暂无发现对应设备,请靠近设备',
|
||||
icon: 'none'
|
||||
});
|
||||
} else {
|
||||
const matchedDevice = this.devices.find(device => {
|
||||
const macFromName = device.name.substring(device.name.length - 12);
|
||||
return macFromName === this.mac;
|
||||
});
|
||||
|
||||
if (matchedDevice) {
|
||||
console.log('找到匹配设备:', matchedDevice);
|
||||
this.selectedDevice = matchedDevice;
|
||||
|
||||
// 连接设备
|
||||
xBlufi.notifyConnectBle({
|
||||
connect: true,
|
||||
deviceId: matchedDevice.deviceId
|
||||
});
|
||||
|
||||
// 初始化设备
|
||||
xBlufi.notifyInitBleEsp32({
|
||||
deviceId: matchedDevice.deviceId
|
||||
});
|
||||
} else {
|
||||
console.log('未找到匹配设备');
|
||||
uni.showToast({
|
||||
title: '未找到对应设备',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
}
|
||||
}, 200);
|
||||
}, 2000);
|
||||
},
|
||||
|
||||
// 响铃寻车
|
||||
ring() {
|
||||
if (!this.hasConnectedDevice || !this.selectedDevice) {
|
||||
uni.showToast({
|
||||
title: '请先连接设备',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
my.getBLEDeviceServices({
|
||||
deviceId: this.selectedDevice.deviceId,
|
||||
success: (res) => {
|
||||
console.log('设备的所有服务:', res.services);
|
||||
xBlufi.notifySendCustomData({
|
||||
customData: "11play1@"
|
||||
});
|
||||
},
|
||||
fail: (error) => {
|
||||
console.error('获取服务列表失败:', error);
|
||||
uni.showToast({
|
||||
title: '获取服务失败',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// 蓝牙开锁
|
||||
open() {
|
||||
if (!this.hasConnectedDevice || !this.selectedDevice) {
|
||||
uni.showToast({
|
||||
title: '请先连接设备',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
xBlufi.notifySendCustomData({
|
||||
customData: "11open"
|
||||
});
|
||||
},
|
||||
|
||||
// 蓝牙关锁
|
||||
close() {
|
||||
if (!this.hasConnectedDevice || !this.selectedDevice) {
|
||||
uni.showToast({
|
||||
title: '请先连接设备',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
xBlufi.notifySendCustomData({
|
||||
customData: "11close"
|
||||
});
|
||||
},
|
||||
|
||||
// 设备重启
|
||||
reboot() {
|
||||
if (!this.hasConnectedDevice || !this.selectedDevice) {
|
||||
uni.showToast({
|
||||
title: '请先连接设备',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
xBlufi.notifySendCustomData({
|
||||
customData: "11reboot"
|
||||
});
|
||||
},
|
||||
|
||||
// 获取硬件版本列表
|
||||
getlist() {
|
||||
this.$u.get('/app/hardwareVersion/list').then((res) => {
|
||||
if (res.code === 200) {
|
||||
|
@ -225,8 +357,7 @@ export default {
|
|||
|
||||
if (hardwareVersionId) {
|
||||
this.hardwareVersionId = hardwareVersionId
|
||||
const matchedVersion = this.Versionlist.find(version => version.id ===
|
||||
hardwareVersionId);
|
||||
const matchedVersion = this.Versionlist.find(version => version.id === hardwareVersionId);
|
||||
if (matchedVersion) {
|
||||
this.hardwareInfo = matchedVersion;
|
||||
}
|
||||
|
@ -245,21 +376,59 @@ export default {
|
|||
}
|
||||
})
|
||||
},
|
||||
send() {
|
||||
this.$u.post('/app/device/ring?sn=' + this.sn).then((res) => {
|
||||
if (res.code === 200) {
|
||||
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: res.msg,
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
// 版本选择确认
|
||||
confirm(e) {
|
||||
uni.setStorageSync('hardwareVersionId', e[0].value);
|
||||
this.hardwareVersionId = e[0].value
|
||||
|
||||
if (this.hardwareVersionId) {
|
||||
const matchedVersion = this.Versionlist.find(version => version.id === this.hardwareVersionId);
|
||||
if (matchedVersion) {
|
||||
this.hardwareInfo = matchedVersion;
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
changeBind() {
|
||||
this.$u.post(`/appVerify/updateSn?sn=` + this.sn + '&mac=' + this.mac + '&hardwareVersionId=' + this.hardwareVersionId).then((res) => {
|
||||
|
||||
// 绑定设备
|
||||
bind() {
|
||||
if (!this.sn || this.sn.length !== 7 || !/^\d+$/.test(this.sn)) {
|
||||
uni.showToast({
|
||||
title: '请输入正确的SN码',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
this.$u.post(`/appVerify/band?sn=${this.sn}&mac=${this.mac}&hardwareVersionId=${this.hardwareVersionId}`).then((res) => {
|
||||
if (res.code === 200) {
|
||||
uni.showToast({
|
||||
title: '绑定成功',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
setTimeout(() => {
|
||||
uni.navigateTo({
|
||||
url: '/pages_admin/controlDevice?sn=' + this.sn
|
||||
})
|
||||
}, 2000)
|
||||
} else {
|
||||
if (res.msg === '该MAC号已经存在') {
|
||||
this.showbind = true;
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: res.msg,
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// 修改绑定
|
||||
changeBind() {
|
||||
this.$u.post(`/appVerify/updateSn?sn=${this.sn}&mac=${this.mac}&hardwareVersionId=${this.hardwareVersionId}`).then((res) => {
|
||||
if (res.code === 200) {
|
||||
uni.showToast({
|
||||
title: '绑定成功',
|
||||
|
@ -267,9 +436,6 @@ export default {
|
|||
duration: 2000
|
||||
});
|
||||
setTimeout(() => {
|
||||
// uni.navigateBack({
|
||||
// delta: 1 // delta值为1时表示返回的页面层数
|
||||
// });
|
||||
uni.navigateTo({
|
||||
url: '/pages_admin/controlDevice?sn=' + this.sn
|
||||
})
|
||||
|
@ -283,54 +449,13 @@ export default {
|
|||
}
|
||||
})
|
||||
},
|
||||
backpage() {
|
||||
uni.navigateBack({
|
||||
delta: 1 // delta值为1时表示返回的页面层数
|
||||
});
|
||||
},
|
||||
bind() {
|
||||
if (this.sn != '' && this.sn.length === 7 && /^\d+$/.test(this.sn)) {
|
||||
this.$u.post(`/appVerify/band?sn=` + this.sn + '&mac=' + this.mac + '&hardwareVersionId=' + this.hardwareVersionId).then((res) => {
|
||||
|
||||
if (res.code == 200) {
|
||||
|
||||
uni.showToast({
|
||||
title: '绑定成功',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
setTimeout(() => {
|
||||
uni.navigateTo({
|
||||
url: '/pages_admin/controlDevice?sn=' + this.sn
|
||||
})
|
||||
// uni.navigateBack({
|
||||
// delta: 1 // delta值为1时表示返回的页面层数
|
||||
// });
|
||||
}, 2000)
|
||||
|
||||
} else {
|
||||
if (res.msg == '该MAC号已经存在') {
|
||||
this.showbind = true
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: res.msg,
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
},
|
||||
// 扫描二维码
|
||||
qrcode() {
|
||||
uni.scanCode({
|
||||
onlyFromCamera: true,
|
||||
scanType: ['qrCode'],
|
||||
success: res => {
|
||||
|
||||
let sn = null;
|
||||
let queryParams = res.result.split('?')[1];
|
||||
if (queryParams) {
|
||||
|
@ -343,12 +468,13 @@ export default {
|
|||
});
|
||||
}
|
||||
|
||||
this.sn = sn
|
||||
console.log(res.result);
|
||||
if (this.sn != '') {
|
||||
|
||||
this.sn = sn;
|
||||
if (!this.sn) {
|
||||
uni.showToast({
|
||||
title: '无效的二维码',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
|
||||
},
|
||||
fail: err => {
|
||||
console.error('扫描失败:', err);
|
||||
|
@ -360,430 +486,33 @@ export default {
|
|||
});
|
||||
},
|
||||
|
||||
Binddevice() {
|
||||
uni.showLoading({
|
||||
title: '连接中..'
|
||||
})
|
||||
xBlufi.initXBlufi(1);
|
||||
console.log("xBlufi", xBlufi.XMQTT_SYSTEM);
|
||||
xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': true
|
||||
});
|
||||
|
||||
|
||||
// 两秒后停止蓝牙搜索
|
||||
setTimeout(() => {
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': false
|
||||
});
|
||||
setTimeout(() => {
|
||||
uni.hideLoading()
|
||||
if (this.devicesList.length == 0) {
|
||||
uni.showToast({
|
||||
title: '暂无发现对应设备,请靠近设备',
|
||||
icon: 'none'
|
||||
});
|
||||
} else {
|
||||
let uniqueDevicesList = Array.from(new Set(this.devicesList));
|
||||
|
||||
// 将去重后的数组重新赋值给 this.devicesList
|
||||
this.devicesList = uniqueDevicesList;
|
||||
// console.log(this.devicesList,'this.devicesListthis.devicesList');
|
||||
this.devicesList.forEach(device => {
|
||||
// 从设备名称中提取 MAC 地址(假设 MAC 地址是设备名称的后6个字符)
|
||||
let macFromName = device.name.substring(device.name.length - 12);
|
||||
console.log(macFromName, 'macFromNamemacFromName');
|
||||
// 与 this.mac 进行比较
|
||||
if (macFromName == this.mac) {
|
||||
// 如果相同,则将 this.ishave 设置为 true
|
||||
console.log(device);
|
||||
this.deviceInfoss = device
|
||||
// this.ishave = true;
|
||||
this.createBLEConnection(device)
|
||||
|
||||
console.log('对比正确1111111111');
|
||||
} else {
|
||||
console.log('对比错误');
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
}, 200)
|
||||
// 判断是否存在浇花器设备
|
||||
|
||||
// this.status = true
|
||||
}, 2000)
|
||||
|
||||
},
|
||||
reboot() {
|
||||
let vm = this; // 将外部的 this 绑定到 vm 上
|
||||
uni.getNetworkType({
|
||||
success(res) {
|
||||
if (res.networkType !== 'none') {
|
||||
uni.getConnectedBluetoothDevices({
|
||||
success(res) {
|
||||
console.log('已连接的蓝牙设备信息:', res);
|
||||
// vm.dl = (vm.orderinfo.deviceAmount / vm.orderinfo.unitPrice).toFixed(2); // 使用 vm 访问 Vue 实例的属性
|
||||
xBlufi.notifySendCustomData({
|
||||
customData: "11reboot"
|
||||
});
|
||||
// uni.hideLoading()
|
||||
// vm.remakr()
|
||||
},
|
||||
fail(err) {
|
||||
uni.hideLoading()
|
||||
console.error('获取已连接蓝牙设备信息失败:', err);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.log('手机未连接网络');
|
||||
}
|
||||
}
|
||||
// 返回上一页
|
||||
backpage() {
|
||||
uni.navigateBack({
|
||||
delta: 1
|
||||
});
|
||||
},
|
||||
open() {
|
||||
let vm = this; // 将外部的 this 绑定到 vm 上
|
||||
uni.getNetworkType({
|
||||
success(res) {
|
||||
if (res.networkType !== 'none') {
|
||||
uni.getConnectedBluetoothDevices({
|
||||
success(res) {
|
||||
console.log('已连接的蓝牙设备信息:', res);
|
||||
// vm.dl = (vm.orderinfo.deviceAmount / vm.orderinfo.unitPrice).toFixed(2); // 使用 vm 访问 Vue 实例的属性
|
||||
xBlufi.notifySendCustomData({
|
||||
customData: "11open"
|
||||
});
|
||||
// uni.hideLoading()
|
||||
// vm.remakr()
|
||||
},
|
||||
fail(err) {
|
||||
uni.hideLoading()
|
||||
console.error('获取已连接蓝牙设备信息失败:', err);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.log('手机未连接网络');
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
close() {
|
||||
let vm = this; // 将外部的 this 绑定到 vm 上
|
||||
uni.getNetworkType({
|
||||
success(res) {
|
||||
if (res.networkType !== 'none') {
|
||||
uni.getConnectedBluetoothDevices({
|
||||
success(res) {
|
||||
console.log('已连接的蓝牙设备信息:', res);
|
||||
// vm.dl = (vm.orderinfo.deviceAmount / vm.orderinfo.unitPrice).toFixed(2); // 使用 vm 访问 Vue 实例的属性
|
||||
xBlufi.notifySendCustomData({
|
||||
customData: "11close"
|
||||
});
|
||||
// uni.hideLoading()
|
||||
// vm.remakr()
|
||||
},
|
||||
fail(err) {
|
||||
uni.hideLoading()
|
||||
console.error('获取已连接蓝牙设备信息失败:', err);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.log('手机未连接网络');
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
ring() {
|
||||
let vm = this; // 将外部的 this 绑定到 vm 上
|
||||
uni.getNetworkType({
|
||||
success(res) {
|
||||
if (res.networkType !== 'none') {
|
||||
uni.getConnectedBluetoothDevices({
|
||||
success(res) {
|
||||
console.log('已连接的蓝牙设备信息:', res);
|
||||
// vm.dl = (vm.orderinfo.deviceAmount / vm.orderinfo.unitPrice).toFixed(2); // 使用 vm 访问 Vue 实例的属性
|
||||
xBlufi.notifySendCustomData({
|
||||
customData: "11play1@"
|
||||
});
|
||||
// uni.hideLoading()
|
||||
// vm.remakr()
|
||||
},
|
||||
fail(err) {
|
||||
uni.hideLoading()
|
||||
console.error('获取已连接蓝牙设备信息失败:', err);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.log('手机未连接网络');
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
btnyc() {
|
||||
this.titleflag = false
|
||||
},
|
||||
|
||||
// 解析自定义数据
|
||||
|
||||
funListenDeviceMsgEvent: function (options) {
|
||||
switch (options.type) {
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
|
||||
if (options.result) {
|
||||
let devicesarr = options.data
|
||||
// this.devicesList = options.data
|
||||
|
||||
devicesarr.forEach(device => {
|
||||
// 生命周期钩子和清理方法
|
||||
|
||||
|
||||
// this.$u.get(`/app/device/${mac}/isBind`, data).then((res) => {
|
||||
// if (res.data == false) {
|
||||
this.devicesList.push(device);
|
||||
|
||||
let uniqueDevicesList = Array.from(new Set(this.devicesList));
|
||||
|
||||
// 将去重后的数组重新赋值给 this.devicesList
|
||||
this.devicesList = uniqueDevicesList;
|
||||
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
|
||||
console.log("连接回调:" + JSON.stringify(options));
|
||||
if (options.result) {
|
||||
uni.hideLoading();
|
||||
uni.showToast({
|
||||
title: '连接成功',
|
||||
icon: 'none'
|
||||
}); {
|
||||
|
||||
xBlufi.notifyInitBleEsp32({
|
||||
deviceId: options.data.deviceId
|
||||
});
|
||||
|
||||
xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvents);
|
||||
|
||||
this.deviceIds = options.data.deviceId
|
||||
this.name = options.data.name
|
||||
// 连接成功绑定设备
|
||||
// let params = {
|
||||
// mac: this.Bluetoothmac,
|
||||
// userId: this.$store.state.user.userId
|
||||
// }
|
||||
let systemInfo = uni.getSystemInfoSync();
|
||||
if (systemInfo.platform === 'android') {
|
||||
// 当前设备是 Android
|
||||
|
||||
} else if (systemInfo.platform === 'ios') {
|
||||
// 当前设备是 iOS
|
||||
// uni.navigateTo({
|
||||
// url: '/pages/wifilist/index?deviceId=' + options
|
||||
// .data.deviceId + '&name=' + options.data.name
|
||||
// })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
|
||||
|
||||
// this.ver_data = this.parseCustomData(options.data)
|
||||
if (options.data) {
|
||||
this.carstause = true
|
||||
}
|
||||
// console.log("1收到设备发来的自定义数据结果:", this.ver_data);
|
||||
break;
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP:
|
||||
if (options.result) {
|
||||
let uniqueDevicesList = Array.from(new Set(this.devicesList));
|
||||
|
||||
// 过滤出名称字段的前五个字母为 "CTPOW" 的项
|
||||
let filteredDevices = uniqueDevicesList.filter(device => device.name.substring(0, 4) ===
|
||||
"BBLE");
|
||||
|
||||
// 将过滤后的数组重新赋值给 this.devicesList
|
||||
this.devicesList = filteredDevices;
|
||||
|
||||
// console.log('蓝牙停止搜索ok',this.devicesList);
|
||||
} else {
|
||||
//蓝牙停止搜索失败
|
||||
// console.log('蓝牙停止搜索失败');
|
||||
}
|
||||
this.searching = false
|
||||
// _this.setData({
|
||||
// searching: false
|
||||
// });
|
||||
|
||||
break;
|
||||
}
|
||||
},
|
||||
createBLEConnection(e) {
|
||||
this.searching = false;
|
||||
// 清理蓝牙相关资源
|
||||
cleanupBluetooth() {
|
||||
xBlufi.listenDeviceMsgEvent(false);
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': false
|
||||
});
|
||||
|
||||
// uni.showLoading({
|
||||
// title: '连接设备中...'
|
||||
// });
|
||||
|
||||
const deviceId = e.deviceId;
|
||||
this.Bluetoothmac = e.localName.substring(5);
|
||||
|
||||
xBlufi.notifyConnectBle({
|
||||
connect: true,
|
||||
deviceId: deviceId,
|
||||
name: e.name || ''
|
||||
});
|
||||
},
|
||||
|
||||
//4、建立连接
|
||||
// createBLEConnection(e) {
|
||||
// console.log('调用了');
|
||||
// xBlufi.notifyStartDiscoverBle({
|
||||
// 'isStart': false
|
||||
// });
|
||||
// console.log(e, '蓝牙信息')
|
||||
// const deviceId = e.deviceId
|
||||
// this.Bluetoothmac = e.localName.substring(5) //从第七位开始截取 只取后面的mac号
|
||||
// console.log(this.Bluetoothmac, '11111');
|
||||
// let name = e.name;
|
||||
// console.log('点击了,蓝牙准备连接的deviceId:' + e.deviceId);
|
||||
// xBlufi.notifyConnectBle({
|
||||
// isStart: true,
|
||||
// deviceId: e.deviceId,
|
||||
// name
|
||||
// });
|
||||
// // for (var i = 0; i < this.devicesList.length; i++) {
|
||||
// // if (e.deviceId === this.devicesList[i].deviceId) {
|
||||
|
||||
// // uni.showLoading({
|
||||
// // title: '连接蓝牙设备中...'
|
||||
// // });
|
||||
// // }
|
||||
// // }
|
||||
// },
|
||||
// 点击重新搜索
|
||||
Search() {
|
||||
if (this.status) {
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': true
|
||||
if (this.hasConnectedDevice && this.selectedDevice) {
|
||||
xBlufi.notifyConnectBle({
|
||||
connect: false,
|
||||
deviceId: this.selectedDevice.deviceId
|
||||
});
|
||||
// 重新搜索清空蓝牙数组
|
||||
this.bluthlist = []
|
||||
this.devicesList = []
|
||||
// 重新搜索
|
||||
// this.startBluetoothDevicesDiscovery()
|
||||
this.statusflag = true
|
||||
this.status = false
|
||||
this.texts = '正在扫描蓝牙设备...'
|
||||
setTimeout(() => {
|
||||
this.statusflag = false
|
||||
this.status = true
|
||||
if (this.searching) {
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': false
|
||||
});
|
||||
// 判断是否存在浇花器设备
|
||||
if (this.devicesList.length == 0) {
|
||||
uni.showToast({
|
||||
title: '暂无发现对应设备,请靠近设备',
|
||||
icon: 'none'
|
||||
});
|
||||
} else {
|
||||
this.texts = '扫描到以下设备,请点击连接!'
|
||||
}
|
||||
} else {
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': true
|
||||
});
|
||||
}
|
||||
}, 2000)
|
||||
}
|
||||
|
||||
|
||||
},
|
||||
funListenDeviceMsgEvents: function (options) {
|
||||
let that = this;
|
||||
switch (options.type) {
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED: {
|
||||
console.log('状态', options.result)
|
||||
if (options.result == false) {
|
||||
this.carstause = false
|
||||
uni.showToast({
|
||||
title: '设备断开链接,请重新点击蓝牙链接',
|
||||
icon: 'none'
|
||||
});
|
||||
// uni.showModal({
|
||||
// title: '很抱歉提醒你!',
|
||||
// content: '小程序与设备异常断开',
|
||||
// showCancel: false,
|
||||
// success: function(res) {
|
||||
// // uni.navigateBack({
|
||||
// // url: '../search/search'
|
||||
// // });
|
||||
// }
|
||||
// });
|
||||
// this.statusflag = false
|
||||
uni.hideLoading();
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_MY_DATA:
|
||||
this.loadPercent = options.data;
|
||||
this.loadText = '文件读取中'
|
||||
console.log("文件读取中", options.data);
|
||||
break;
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
|
||||
// this.isband = true
|
||||
// this.ver_data = this.parseCustomData(options.data)
|
||||
console.log("1收到设备发来的自定义数据结果:", options.data);
|
||||
if (options.data) {
|
||||
this.carstause = true
|
||||
}
|
||||
break;
|
||||
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT:
|
||||
uni.hideLoading();
|
||||
if (options.result) {
|
||||
console.log('初始化成功');
|
||||
} else {
|
||||
console.log('初始化失败');
|
||||
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
},
|
||||
// 点击隐藏没有设备提示
|
||||
btnhd() {
|
||||
this.tishiflag = false
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss">
|
||||
page {
|
||||
overflow-x: hidden;
|
||||
|
|
|
@ -73,619 +73,378 @@
|
|||
</template>
|
||||
|
||||
<script>
|
||||
const app = getApp();
|
||||
var xBlufi = require("@/utils/blufi/xBlufi.js");
|
||||
let _this = null;
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
titleflag: false, //提示隐藏
|
||||
bgc: {
|
||||
backgroundColor: "#F7FAFE",
|
||||
},
|
||||
devicesList: [
|
||||
// {name:'110000',
|
||||
// mac:'11111'}
|
||||
],
|
||||
devicesLists: [],
|
||||
searching: false,
|
||||
texts: '正在扫描蓝牙设备...',
|
||||
btnflag: true,
|
||||
tishiflag: false,
|
||||
option: '',
|
||||
bluthlist: [], //蓝牙数组
|
||||
status: 'loading',
|
||||
statusflag: false,
|
||||
Bluetoothmac: '',
|
||||
gps: {},
|
||||
mac: '',
|
||||
status: false,
|
||||
showConfirmModal: false, // 控制弹窗显示的标志
|
||||
deviceinfo: null,
|
||||
sn: ''
|
||||
}
|
||||
const app = getApp();
|
||||
var xBlufi = require("@/utils/blufi/xBlufi.js");
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
titleflag: false,
|
||||
bgc: {
|
||||
backgroundColor: "#F7FAFE",
|
||||
},
|
||||
devicesList: [], // 最终显示的设备列表
|
||||
devicesLists: [], // 临时存储扫描到的设备
|
||||
searching: false,
|
||||
texts: '正在扫描蓝牙设备...',
|
||||
btnflag: true,
|
||||
tishiflag: false,
|
||||
option: '',
|
||||
bluthlist: [],
|
||||
statusflag: false,
|
||||
Bluetoothmac: '',
|
||||
gps: {},
|
||||
mac: '',
|
||||
status: false,
|
||||
showConfirmModal: false,
|
||||
deviceinfo: null,
|
||||
sn: '',
|
||||
hasConnectedDevice: false,
|
||||
selectedDevice: null,
|
||||
serviceId: '000000FF-0000-1000-8000-00805F9B34FB',
|
||||
characteristicId: '0000FF01-0000-1000-8000-00805F9B34FB'
|
||||
}
|
||||
},
|
||||
|
||||
onLoad(e) {
|
||||
if (e.sn) {
|
||||
this.sn = e.sn;
|
||||
}
|
||||
},
|
||||
|
||||
onShow() {
|
||||
this.initBluetooth();
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
this.cleanupBluetooth();
|
||||
},
|
||||
|
||||
onHide() {
|
||||
this.cleanupBluetooth();
|
||||
},
|
||||
|
||||
methods: {
|
||||
// 页面跳转相关方法
|
||||
replay() {
|
||||
uni.reLaunch({
|
||||
url: '/pages_admin/worke/bind_sn'
|
||||
})
|
||||
},
|
||||
onLoad(e) {
|
||||
if (e.sn) {
|
||||
this.sn = e.sn
|
||||
}
|
||||
|
||||
backPage() {
|
||||
uni.redirectTo({
|
||||
url: '/pages_admin/admin_worke'
|
||||
})
|
||||
},
|
||||
onShow: function() {
|
||||
this.bluthlist = [];
|
||||
this.devicesList = [];
|
||||
this.devicesLists = [];
|
||||
|
||||
// 强制视图更新
|
||||
this.$forceUpdate();
|
||||
|
||||
// 后续的蓝牙初始化和扫描逻辑
|
||||
xBlufi.initXBlufi(1);
|
||||
xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
|
||||
xBlufi.notifyStartDiscoverBle({ 'isStart': true });
|
||||
|
||||
setTimeout(() => {
|
||||
xBlufi.notifyStartDiscoverBle({ 'isStart': false });
|
||||
if (this.devicesList.length == 0) {
|
||||
this.tishiflag = true;
|
||||
this.texts = '扫描完毕,暂无发现电动车设备';
|
||||
} else {
|
||||
this.texts = '扫描到以下设备,请点击连接!';
|
||||
}
|
||||
this.status = true;
|
||||
|
||||
// 再次强制更新
|
||||
this.$forceUpdate();
|
||||
}, 2000);
|
||||
|
||||
toControl() {
|
||||
uni.redirectTo({
|
||||
url: '/pages_admin/controlDevice'
|
||||
})
|
||||
},
|
||||
onUnload: function() {
|
||||
|
||||
// 蓝牙初始化
|
||||
initBluetooth() {
|
||||
// 重置设备列表
|
||||
this.bluthlist = [];
|
||||
this.devicesList = [];
|
||||
this.devicesLists = [];
|
||||
|
||||
// 初始化蓝牙
|
||||
xBlufi.initXBlufi(xBlufi.XMQTT_SYSTEM.Alis);
|
||||
xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
|
||||
|
||||
// 开始搜索设备
|
||||
this.startSearch();
|
||||
},
|
||||
|
||||
// 清理蓝牙连接
|
||||
cleanupBluetooth() {
|
||||
xBlufi.listenDeviceMsgEvent(false, this.funListenDeviceMsgEvent);
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': false
|
||||
});
|
||||
|
||||
xBlufi.notifyStartDiscoverBle({ 'isStart': false });
|
||||
},
|
||||
onHide() {
|
||||
xBlufi.listenDeviceMsgEvent(false, this.funListenDeviceMsgEvent);
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': false
|
||||
});
|
||||
|
||||
},
|
||||
onBeforeUnmount() {
|
||||
xBlufi.listenDeviceMsgEvent(false, this.funListenDeviceMsgEvent);
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': false
|
||||
});
|
||||
},
|
||||
mounted() {
|
||||
// this.videoContext = uni.createVideoContext('myVideo', this)
|
||||
},
|
||||
methods: {
|
||||
replay(){
|
||||
uni.reLaunch({
|
||||
url:'/pages_admin/worke/bind_sn'
|
||||
})
|
||||
},
|
||||
backPage(){
|
||||
uni.redirectTo({
|
||||
url:'/pages_admin/admin_worke'
|
||||
})
|
||||
},
|
||||
toControl(){
|
||||
uni.redirectTo({
|
||||
url:'/pages_admin/controlDevice'
|
||||
})
|
||||
},
|
||||
fetchDevicesBoundStatus() {
|
||||
// 提取所有设备的 MAC 地址,并通过 Set 去重
|
||||
let uniqueDevices = Array.from(new Set(this.devicesList.map(item => item.localName.substring(5))));
|
||||
// 开始搜索设备
|
||||
startSearch() {
|
||||
this.searching = true;
|
||||
this.texts = '正在扫描蓝牙设备...';
|
||||
xBlufi.notifyStartDiscoverBle({ 'isStart': true });
|
||||
|
||||
// 重新构建去重后的 devicesList
|
||||
this.devicesList = uniqueDevices.map(mac => {
|
||||
return this.devicesList.find(item => item.localName.substring(5) === mac);
|
||||
});
|
||||
|
||||
// 拼接成字符串
|
||||
const macs = uniqueDevices.join(',');
|
||||
|
||||
// 向批量接口请求录入状态
|
||||
this.$u.get(`app/getDeviceInfoByMacList?macList=${macs}`).then((res) => {
|
||||
const resultList = res.data; // 获取接口返回的数据数组
|
||||
|
||||
// 遍历 devicesList 并对比 mac 地址
|
||||
this.devicesList.forEach(device => {
|
||||
const mac = device.localName.substring(5);
|
||||
|
||||
// 查找返回结果中是否有与该设备 MAC 地址相同的项
|
||||
const matchingResult = resultList.find(result => result.mac === mac);
|
||||
|
||||
// 如果找到了匹配的 MAC 地址项,则更新设备的 sn 和 isBand
|
||||
if (matchingResult) {
|
||||
device.sn = matchingResult.sn;
|
||||
device.isBand = matchingResult.isBand;
|
||||
}
|
||||
});
|
||||
|
||||
// 对 devicesList 进行排序,将 isBand 为 false 的设备排在前面
|
||||
this.devicesList.sort((a, b) => {
|
||||
return (a.isBand === false ? -1 : 1) - (b.isBand === false ? -1 : 1);
|
||||
});
|
||||
|
||||
this.$forceUpdate(); // 强制更新视图
|
||||
console.log(this.devicesList, 'Updated devicesList');
|
||||
}).catch((error) => {
|
||||
console.error('批量获取设备录入状态失败', error);
|
||||
});
|
||||
},
|
||||
choose(e) {
|
||||
console.log(e);
|
||||
let Bluetoothmac = e.name.substring(5)
|
||||
uni.redirectTo({
|
||||
url: '/pages_admin/worke/bind_mac?mac=' + Bluetoothmac
|
||||
})
|
||||
},
|
||||
send() {
|
||||
this.$u.post('/app/device/ring?sn=' + this.sn).then((res) => {
|
||||
if (res.code === 200) {
|
||||
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: res.msg,
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
}
|
||||
})
|
||||
},
|
||||
openConfirmModal(e) {
|
||||
this.deviceinfo = e
|
||||
this.showConfirmModal = true;
|
||||
},
|
||||
|
||||
closeConfirmModal() {
|
||||
this.showConfirmModal = false;
|
||||
},
|
||||
confirmBind() {
|
||||
this.closeConfirmModal();
|
||||
this.createBLEConnection(this.deviceinfo)
|
||||
},
|
||||
btnyc() {
|
||||
this.titleflag = false
|
||||
},
|
||||
sing() {
|
||||
let vm = this; // 将外部的 this 绑定到 vm 上
|
||||
uni.getNetworkType({
|
||||
success(res) {
|
||||
if (res.networkType !== 'none') {
|
||||
uni.getConnectedBluetoothDevices({
|
||||
success(res) {
|
||||
console.log('已连接的蓝牙设备信息:', res);
|
||||
// vm.dl = (vm.orderinfo.deviceAmount / vm.orderinfo.unitPrice).toFixed(2); // 使用 vm 访问 Vue 实例的属性
|
||||
xBlufi.notifySendCustomData({
|
||||
customData: "11play1@"
|
||||
});
|
||||
uni.hideLoading()
|
||||
vm.remakr()
|
||||
},
|
||||
fail(err) {
|
||||
uni.hideLoading()
|
||||
console.error('获取已连接蓝牙设备信息失败:', err);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.log('手机未连接网络');
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
remakr() {
|
||||
let vm = this; // 将外部的 this 绑定到 vm 上
|
||||
uni.getNetworkType({
|
||||
success(res) {
|
||||
if (res.networkType !== 'none') {
|
||||
uni.getConnectedBluetoothDevices({
|
||||
success(res) {
|
||||
console.log('已连接的蓝牙设备信息:', res);
|
||||
// vm.dl = (vm.orderinfo.deviceAmount / vm.orderinfo.unitPrice).toFixed(2); // 使用 vm 访问 Vue 实例的属性
|
||||
xBlufi.notifySendCustomData({
|
||||
customData: "11reboot"
|
||||
});
|
||||
uni.navigateBack({
|
||||
delta: 1 // delta值为1时表示返回的页面层数
|
||||
});
|
||||
|
||||
},
|
||||
fail(err) {
|
||||
console.error('获取已连接蓝牙设备信息失败:', err);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.log('手机未连接网络');
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
funListenDeviceMsgEvent: function(options) {
|
||||
switch (options.type) {
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
|
||||
if (options.result) {
|
||||
let devicesarr = options.data
|
||||
// this.devicesList = options.data
|
||||
// console.log(devicesarr, 'devicesarrdevicesarr');
|
||||
devicesarr.forEach(device => {
|
||||
const mac = device.name.substring(6);
|
||||
|
||||
if (device.name.indexOf("BBLE") != -1) {
|
||||
// this.$u.get(`/app/device/${mac}/isBind`).then((res) => {
|
||||
// if (res.data == false) {
|
||||
this.devicesLists.push(device);
|
||||
let uniqueDevicesList = Array.from(new Set(this.devicesLists));
|
||||
|
||||
// 将去重后的数组重新赋值给 this.devicesList
|
||||
this.devicesLists = uniqueDevicesList;
|
||||
// }
|
||||
// })
|
||||
|
||||
}
|
||||
// getisband(mac).then(res => {
|
||||
// console.log(res);
|
||||
// if (res.data == true) {
|
||||
// console.log(`MAC 地址 ${mac} 已经绑定`);
|
||||
// } else {
|
||||
// this.devicesList.push(device)
|
||||
// }
|
||||
// }).catch(error => {
|
||||
// console.error(`检查 MAC 地址 ${mac} 是否绑定的过程中出错:`, error);
|
||||
// });
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
|
||||
console.log("连接回调:" + JSON.stringify(options));
|
||||
if (options.result) {
|
||||
uni.hideLoading();
|
||||
uni.showToast({
|
||||
title: '连接成功',
|
||||
icon: 'none'
|
||||
}); {
|
||||
console.log("连接回调options.data.deviceId:" + options.data.deviceId,
|
||||
"连接回调options.data.name:" + options.data.name);
|
||||
// 连接成功绑定设备
|
||||
// let params = {
|
||||
// mac: this.Bluetoothmac,
|
||||
// userId: this.$store.state.user.userId
|
||||
// }
|
||||
xBlufi.notifyInitBleEsp32({
|
||||
deviceId: options.data.deviceId
|
||||
});
|
||||
|
||||
// xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvents);
|
||||
// setTimeout(() => {
|
||||
// this.sing()
|
||||
// }, 3000)
|
||||
|
||||
let systemInfo = uni.getSystemInfoSync();
|
||||
if (systemInfo.platform === 'android') {
|
||||
// 当前设备是 Android
|
||||
// uni.navigateTo({
|
||||
// url: '/pages/wifilist/index?deviceId=' + options
|
||||
// .data.deviceId + '&name=' + options.data.name
|
||||
// })
|
||||
} else if (systemInfo.platform === 'ios') {
|
||||
// 当前设备是 iOS
|
||||
// uni.navigateTo({
|
||||
// url: '/pages/wifilist/index?deviceId=' + options
|
||||
// .data.deviceId + '&name=' + options.data.name
|
||||
// })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
|
||||
if (!options.result) {
|
||||
console.log("蓝牙未开启", options);
|
||||
uni.showToast({
|
||||
title: '蓝牙未开启,如有疑问请查看查看常见问题',
|
||||
icon: 'none',
|
||||
duration: 3000
|
||||
});
|
||||
if (this.devicesList.length == 0) {
|
||||
this.tishiflag = true
|
||||
this.texts = '扫描完毕,暂无发现电动车设备'
|
||||
} else {
|
||||
this.texts = '扫描到以下设备,请点击连接!'
|
||||
}
|
||||
} else {
|
||||
this.searching = true
|
||||
//蓝牙搜索开始
|
||||
// _this.setData({
|
||||
// searching: true
|
||||
// });
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP:
|
||||
if (options.result) {
|
||||
let uniqueDevicesList = Array.from(new Set(this.devicesLists));
|
||||
|
||||
// 将去重后的数组重新赋值给 this.devicesList
|
||||
this.devicesLists = uniqueDevicesList;
|
||||
let list = []
|
||||
uniqueDevicesList.forEach(device => {
|
||||
// 从设备名称中提取 MAC 地址(假设 MAC 地址是设备名称的后6个字符)
|
||||
let macFromName = device.name.substring(device.name.length - 12);
|
||||
// console.log(macFromName);
|
||||
// 与 this.mac 进行比较
|
||||
list.push(device)
|
||||
// this.$u.get(`/app/device/${macFromName}/isBind`).then((res) => {
|
||||
// if (res.data == false) {
|
||||
// list.push(device)
|
||||
// } else {
|
||||
|
||||
// }
|
||||
// })
|
||||
|
||||
});
|
||||
setTimeout(() => {
|
||||
this.devicesList = list
|
||||
let uniqueDevicesList = Array.from(new Set(this.devicesList));
|
||||
|
||||
// 将去重后的数组重新赋值给 this.devicesList
|
||||
this.devicesList = uniqueDevicesList;
|
||||
this.fetchDevicesBoundStatus();
|
||||
}, 200)
|
||||
|
||||
console.log('蓝牙停止搜索ok');
|
||||
} else {
|
||||
//蓝牙停止搜索失败
|
||||
console.log('蓝牙停止搜索失败');
|
||||
}
|
||||
this.searching = false
|
||||
// _this.setData({
|
||||
// searching: false
|
||||
// });
|
||||
|
||||
break;
|
||||
setTimeout(() => {
|
||||
xBlufi.notifyStartDiscoverBle({ 'isStart': false });
|
||||
if (this.devicesList.length == 0) {
|
||||
this.tishiflag = true;
|
||||
this.texts = '扫描完毕,暂无发现电动车设备';
|
||||
} else {
|
||||
this.texts = '扫描到以下设备,请点击连接!';
|
||||
}
|
||||
},
|
||||
funListenDeviceMsgEvents: function(options) {
|
||||
let that = this;
|
||||
switch (options.type) {
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED: {
|
||||
// console.log('状态', options.result)
|
||||
if (options.result == false) {
|
||||
// uni.showModal({
|
||||
// title: '很抱歉提醒你!',
|
||||
// content: '小程序与设备异常断开',
|
||||
// showCancel: false,
|
||||
// success: function(res) {
|
||||
// // uni.navigateBack({
|
||||
// // url: '../search/search'
|
||||
// // });
|
||||
// }
|
||||
// });
|
||||
this.statusflag = false
|
||||
uni.hideLoading();
|
||||
}
|
||||
this.searching = false;
|
||||
}, 5000);
|
||||
},
|
||||
|
||||
// 选择设备
|
||||
choose(device) {
|
||||
console.log('选择设备:', device);
|
||||
let Bluetoothmac = device.name.substring(5);
|
||||
uni.redirectTo({
|
||||
url: '/pages_admin/worke/bind_mac?mac=' + Bluetoothmac
|
||||
});
|
||||
},
|
||||
|
||||
// 设备消息监听处理
|
||||
funListenDeviceMsgEvent(options) {
|
||||
switch (options.type) {
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
|
||||
if (options.result && options.data) {
|
||||
let devicesarr = options.data;
|
||||
devicesarr.forEach(device => {
|
||||
// 只处理BBLE开头的设备
|
||||
if (device.name && device.name.startsWith('BBLE')) {
|
||||
// 检查是否已存在相同设备
|
||||
const existingDevice = this.devicesLists.find(d =>
|
||||
d.deviceId === device.deviceId
|
||||
);
|
||||
|
||||
if (!existingDevice) {
|
||||
this.devicesLists.push(device);
|
||||
|
||||
// 更新最终显示的设备列表
|
||||
this.devicesList = [...this.devicesLists];
|
||||
|
||||
// 获取设备绑定状态
|
||||
this.fetchDevicesBoundStatus();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
|
||||
this.handleConnectionStatus(options);
|
||||
break;
|
||||
|
||||
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
|
||||
|
||||
console.log("1收到设备发来的自定义数据结果:", options.data);
|
||||
break;
|
||||
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT:
|
||||
uni.hideLoading();
|
||||
if (options.result) {
|
||||
console.log('初始化成功');
|
||||
} else {
|
||||
console.log('初始化失败');
|
||||
uni.showModal({
|
||||
title: '温馨提示',
|
||||
content: `设备初始化失败`,
|
||||
showCancel: false,
|
||||
//是否显示取消按钮
|
||||
success: function(res) {
|
||||
// uni.redirectTo({
|
||||
// url: '../search/search'
|
||||
// });
|
||||
}
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
//4、建立连接
|
||||
createBLEConnection(e) {
|
||||
this.mac = e.localName.substring(5)
|
||||
let that = this
|
||||
that.$u.post(`/appVerify/band?sn=` + that.sn + '&mac=' + that.mac).then((res) => {
|
||||
uni.showLoading({
|
||||
title: '绑定中'
|
||||
})
|
||||
if (res.code == 200) {
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
|
||||
if (!options.result) {
|
||||
console.log("蓝牙未开启", options);
|
||||
uni.showToast({
|
||||
title: '绑定成功',
|
||||
title: '蓝牙未开启,如有疑问请查看常见问题',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
duration: 3000
|
||||
});
|
||||
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': false
|
||||
});
|
||||
|
||||
const deviceId = that.deviceinfo.deviceId
|
||||
that.Bluetoothmac = that.deviceinfo.localName.substring(5) //从第七位开始截取 只取后面的mac号
|
||||
|
||||
|
||||
let name = that.deviceinfo.name;
|
||||
console.log(that.deviceinfo.deviceId, 'that.deviceinfo.deviceId');
|
||||
xBlufi.notifyConnectBle({
|
||||
isStart: true,
|
||||
deviceId: that.deviceinfo.deviceId,
|
||||
name
|
||||
});
|
||||
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: res.msg,
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
setTimeout(() => {
|
||||
uni.navigateBack({
|
||||
delta: 1 // delta值为1时表示返回的页面层数
|
||||
});
|
||||
}, 2000)
|
||||
|
||||
this.searching = true;
|
||||
}
|
||||
})
|
||||
break;
|
||||
|
||||
// uni.getLocation({
|
||||
// type: 'wgs84',
|
||||
// success: function(lb) {
|
||||
// console.log('位置信息', lb);
|
||||
// console.log('当前位置的经度:' + lb.longitude);
|
||||
// console.log('当前位置的纬度:' + lb.latitude);
|
||||
// that.gps.lat = lb.latitude;
|
||||
// that.gps.lon = lb.longitude;
|
||||
// that.$u.put(`/app/device/bind/${that.mac}`, that.gps).then((res) => {
|
||||
// if (res.code == 200) {
|
||||
// uni.showToast({
|
||||
// title: '绑定成功',
|
||||
// icon: 'none',
|
||||
// duration: 2000
|
||||
// });
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP:
|
||||
this.searching = false;
|
||||
break;
|
||||
|
||||
// uni.removeStorageSync('mac');
|
||||
// // setTimeout(()=>{
|
||||
// // uni.switchTab({
|
||||
// // url:'/pages/index/index'
|
||||
// // })
|
||||
// // },)
|
||||
// } else {
|
||||
// uni.showToast({
|
||||
// title:res.msg,
|
||||
// icon: 'none',
|
||||
// duration: 2000
|
||||
// });
|
||||
// // setTimeout(()=>{
|
||||
// // uni.switchTab({
|
||||
// // url:'/pages/index/index'
|
||||
// // })
|
||||
// // },)
|
||||
// }
|
||||
// })
|
||||
|
||||
// },
|
||||
// fail: function(error) {
|
||||
// console.error('获取位置信息失败:', error);
|
||||
// that.gps.lat = 0;
|
||||
// that.gps.lon = 0;
|
||||
// that.$u.put(`/app/device/bind/${that.mac}`, that.gps).then((res) => {
|
||||
// if (res.code == 200) {
|
||||
// uni.showToast({
|
||||
// title: '绑定成功',
|
||||
// icon: 'none',
|
||||
// duration: 2000
|
||||
// });
|
||||
// uni.removeStorageSync('mac');
|
||||
// xBlufi.notifyStartDiscoverBle({
|
||||
// 'isStart': false
|
||||
// });
|
||||
|
||||
// const deviceId = that.deviceinfo.deviceId
|
||||
// that.Bluetoothmac = that.deviceinfo.localName.substring(6) //从第七位开始截取 只取后面的mac号
|
||||
|
||||
|
||||
// let name = that.deviceinfo.name;
|
||||
// xBlufi.notifyConnectBle({
|
||||
// isStart: true,
|
||||
// deviceId: that.deviceinfo.deviceId,
|
||||
// name
|
||||
// });
|
||||
// // setTimeout(()=>{
|
||||
// // uni.switchTab({
|
||||
// // url:'/pages/index/index'
|
||||
// // })
|
||||
// // },)
|
||||
// } else {
|
||||
// uni.showToast({
|
||||
// title:res.msg,
|
||||
// icon: 'none',
|
||||
// duration: 2000
|
||||
// });
|
||||
// // setTimeout(()=>{
|
||||
// // uni.switchTab({
|
||||
// // url:'/pages/index/index'
|
||||
// // })
|
||||
// // },)
|
||||
// }
|
||||
// })
|
||||
// // 在这里处理获取位置信息失败的情况
|
||||
// }
|
||||
|
||||
// })
|
||||
|
||||
|
||||
|
||||
},
|
||||
// 点击重新搜索
|
||||
Search() {
|
||||
if (this.status) {
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': true
|
||||
});
|
||||
// 重新搜索清空蓝牙数组
|
||||
this.bluthlist = []
|
||||
this.devicesList = []
|
||||
this.devicesLists = []
|
||||
// 重新搜索
|
||||
// this.startBluetoothDevicesDiscovery()
|
||||
this.statusflag = true
|
||||
this.texts = '正在扫描蓝牙设备...'
|
||||
setTimeout(() => {
|
||||
this.statusflag = false
|
||||
if (this.searching) {
|
||||
// 判断是否存在浇花器设备
|
||||
if (this.devicesList.length == 0) {
|
||||
this.tishiflag = true
|
||||
this.texts = '扫描完毕,暂无发现电动车设备'
|
||||
} else {
|
||||
this.texts = '扫描到以下设备,请点击连接!'
|
||||
}
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': false
|
||||
});
|
||||
|
||||
|
||||
} else {
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': true
|
||||
});
|
||||
}
|
||||
}, 2000)
|
||||
}
|
||||
},
|
||||
// 点击隐藏没有设备提示
|
||||
btnhd() {
|
||||
this.tishiflag = false
|
||||
case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT:
|
||||
if (options.result) {
|
||||
console.log('设备初始化成功');
|
||||
this.hasConnectedDevice = true;
|
||||
} else {
|
||||
console.error('设备初始化失败');
|
||||
uni.showModal({
|
||||
title: '温馨提示',
|
||||
content: '设备初始化失败',
|
||||
showCancel: false
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
// 处理连接状态
|
||||
handleConnectionStatus(res) {
|
||||
if (res.result) {
|
||||
uni.showToast({
|
||||
title: '连接成功',
|
||||
icon: 'success'
|
||||
});
|
||||
|
||||
// 初始化设备
|
||||
xBlufi.notifyInitBleEsp32({
|
||||
deviceId: res.data.deviceId
|
||||
});
|
||||
|
||||
// 更新设备连接状态
|
||||
const index = this.devicesList.findIndex(d => d.deviceId === res.data.deviceId);
|
||||
if (index !== -1) {
|
||||
this.$set(this.devicesList[index], 'connected', true);
|
||||
this.hasConnectedDevice = true;
|
||||
this.selectedDevice = this.devicesList[index];
|
||||
}
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '连接断开',
|
||||
icon: 'none'
|
||||
});
|
||||
this.hasConnectedDevice = false;
|
||||
this.selectedDevice = null;
|
||||
}
|
||||
},
|
||||
|
||||
// 响铃功能
|
||||
rings() {
|
||||
if (!this.hasConnectedDevice || !this.selectedDevice) {
|
||||
uni.showToast({
|
||||
title: '请先连接设备',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('准备发送响铃命令');
|
||||
my.getBLEDeviceServices({
|
||||
deviceId: this.selectedDevice.deviceId,
|
||||
success: (res) => {
|
||||
console.log('设备的所有服务:', res.services);
|
||||
xBlufi.notifySendCustomData({
|
||||
customData: "11play1@"
|
||||
});
|
||||
},
|
||||
fail: (error) => {
|
||||
console.error('获取服务列表失败:', error);
|
||||
uni.showToast({
|
||||
title: '获取服务失败',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// 重新搜索
|
||||
Search() {
|
||||
if (this.status) {
|
||||
// 重置设备列表
|
||||
this.bluthlist = [];
|
||||
this.devicesList = [];
|
||||
this.devicesLists = [];
|
||||
|
||||
this.statusflag = true;
|
||||
this.texts = '正在扫描蓝牙设备...';
|
||||
|
||||
// 开始新的搜索
|
||||
xBlufi.notifyStartDiscoverBle({ 'isStart': true });
|
||||
|
||||
setTimeout(() => {
|
||||
this.statusflag = false;
|
||||
if (this.searching) {
|
||||
if (this.devicesList.length == 0) {
|
||||
this.tishiflag = true;
|
||||
this.texts = '扫描完毕,暂无发现电动车设备';
|
||||
} else {
|
||||
this.texts = '扫描到以下设备,请点击连接!';
|
||||
}
|
||||
xBlufi.notifyStartDiscoverBle({ 'isStart': false });
|
||||
}
|
||||
}, 2000);
|
||||
}
|
||||
},
|
||||
|
||||
// 获取设备绑定状态
|
||||
fetchDevicesBoundStatus() {
|
||||
// 提取所有设备的 MAC 地址并去重
|
||||
let uniqueDevices = Array.from(new Set(this.devicesList.map(item => item.name.substring(5))));
|
||||
|
||||
// 重新构建去重后的 devicesList
|
||||
this.devicesList = uniqueDevices.map(mac => {
|
||||
return this.devicesList.find(item => item.name.substring(5) === mac);
|
||||
});
|
||||
|
||||
// 拼接MAC地址字符串
|
||||
const macs = uniqueDevices.join(',');
|
||||
|
||||
// 批量查询设备绑定状态
|
||||
this.$u.get(`app/getDeviceInfoByMacList?macList=${macs}`).then((res) => {
|
||||
const resultList = res.data;
|
||||
|
||||
// 更新设备列表的绑定状态
|
||||
this.devicesList.forEach(device => {
|
||||
const mac = device.name.substring(5);
|
||||
const matchingResult = resultList.find(result => result.mac === mac);
|
||||
|
||||
if (matchingResult) {
|
||||
device.sn = matchingResult.sn;
|
||||
device.isBand = matchingResult.isBand;
|
||||
}
|
||||
});
|
||||
|
||||
// 未绑定的设备排在前面
|
||||
this.devicesList.sort((a, b) => {
|
||||
return (a.isBand === false ? -1 : 1) - (b.isBand === false ? -1 : 1);
|
||||
});
|
||||
|
||||
this.$forceUpdate();
|
||||
}).catch((error) => {
|
||||
console.error('获取设备绑定状态失败', error);
|
||||
});
|
||||
},
|
||||
|
||||
// 打开确认弹窗
|
||||
openConfirmModal(device) {
|
||||
this.deviceinfo = device;
|
||||
this.showConfirmModal = true;
|
||||
},
|
||||
|
||||
// 关闭确认弹窗
|
||||
closeConfirmModal() {
|
||||
this.showConfirmModal = false;
|
||||
},
|
||||
|
||||
// 确认绑定
|
||||
confirmBind() {
|
||||
this.closeConfirmModal();
|
||||
this.createBLEConnection(this.deviceinfo);
|
||||
},
|
||||
|
||||
// 建立蓝牙连接
|
||||
createBLEConnection(device) {
|
||||
this.mac = device.name.substring(5);
|
||||
let that = this;
|
||||
|
||||
that.$u.post(`/appVerify/band?sn=${that.sn}&mac=${that.mac}`).then((res) => {
|
||||
uni.showLoading({
|
||||
title: '绑定中'
|
||||
});
|
||||
|
||||
if (res.code == 200) {
|
||||
uni.showToast({
|
||||
title: '绑定成功',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
|
||||
xBlufi.notifyStartDiscoverBle({
|
||||
'isStart': false
|
||||
});
|
||||
|
||||
xBlufi.notifyConnectBle({
|
||||
isStart: true,
|
||||
deviceId: device.deviceId,
|
||||
name: device.name
|
||||
});
|
||||
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: res.msg,
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
setTimeout(() => {
|
||||
uni.navigateBack({
|
||||
delta: 1
|
||||
});
|
||||
}, 2000);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss">
|
||||
page {
|
||||
background-color: #F7FAFE !important;
|
||||
|
|
Loading…
Reference in New Issue
Block a user