diff --git a/page_user/bule-test.vue b/page_user/bule-test.vue index 405e505..6cc5bcd 100644 --- a/page_user/bule-test.vue +++ b/page_user/bule-test.vue @@ -69,228 +69,265 @@ import xBlufi from '@/utils/blufi/xBlufi.js' export default { - data() { - return { - devices: [], - isSearching: false, - isConnecting: false, - bluetoothStatus: '未初始化', - showModal: false, - selectedDevice: null, - hasConnectedDevice: false, - bgc: { - backgroundColor: "#F7FAFE", - }, - serviceId: '000000FF-0000-1000-8000-00805F9B34FB', // 修改为正确的 service UUID - characteristicId: '0000FF01-0000-1000-8000-00805F9B34FB' // 修改为正确的写特征值 UUID - } - }, + data() { + return { + devices: [], + isSearching: false, + isConnecting: false, + bluetoothStatus: '未初始化', + showModal: false, + selectedDevice: null, + hasConnectedDevice: false, + bgc: { + backgroundColor: "#F7FAFE", + }, + serviceId: '0000FEE0-0000-1000-8000-00805F9B34FB', + characteristicId: '0000FEE1-0000-1000-8000-00805F9B34FB' + } + }, - onLoad() { - this.initBluetooth() - }, + onLoad() { + this.initBluetooth() + }, - methods: { - initBluetooth() { - xBlufi.initXBlufi(xBlufi.XMQTT_SYSTEM.Alis) + methods: { + initBluetooth() { + xBlufi.initXBlufi(xBlufi.XMQTT_SYSTEM.Alis) - 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'); - }); + 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.map(device => ({ - deviceId: device.deviceId, - name: device.name || device.deviceName || '未知设备', - mac: device.deviceId, - RSSI: device.RSSI || 0, - connected: false - })); - } - break; + this.devices = bbleDevices.map(device => ({ + deviceId: device.deviceId, + name: device.name || device.deviceName || '未知设备', + mac: device.deviceId, + RSSI: device.RSSI || 0, + connected: false + })); + } + break; - case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED: - this.handleConnectionStatus(res); - break; + case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED: + this.handleConnectionStatus(res); + break; - case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT: - this.handleInitResult(res); - break; + case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT: + this.handleInitResult(res); + break; - case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START: - console.log("开始搜索设备"); - this.devices = []; - break; + case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START: + console.log("开始搜索设备"); + this.devices = []; + break; - case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP: - console.log("停止搜索设备"); - this.isSearching = false; - break; - } - }); + case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP: + console.log("停止搜索设备"); + this.isSearching = false; + break; + } + }); - this.bluetoothStatus = '已初始化' - }, + this.bluetoothStatus = '已初始化' + }, - toggleBluetooth() { - if (this.isSearching) { - xBlufi.notifyStartDiscoverBle({ - isStart: false - }); - this.isSearching = false; - } else { - this.devices = []; - this.isSearching = true; - xBlufi.notifyStartDiscoverBle({ - isStart: true - }); + toggleBluetooth() { + if (this.isSearching) { + xBlufi.notifyStartDiscoverBle({ + isStart: false + }); + this.isSearching = false; + } else { + this.devices = []; + this.isSearching = true; + xBlufi.notifyStartDiscoverBle({ + isStart: true + }); - setTimeout(() => { - if (this.isSearching) { - xBlufi.notifyStartDiscoverBle({ - isStart: false - }); - this.isSearching = false; - } - }, 10000); - } - }, + setTimeout(() => { + if (this.isSearching) { + xBlufi.notifyStartDiscoverBle({ + isStart: false + }); + this.isSearching = false; + } + }, 10000); + } + }, - handleDeviceClick(device) { - if (device.connected) { - uni.showModal({ - title: '断开连接', - content: '是否断开与该设备的连接?', - success: (res) => { - if (res.confirm) { - this.disconnectDevice(device); - } - } - }); - } else { - this.selectedDevice = device; - this.showModal = true; - } - }, + handleDeviceClick(device) { + if (device.connected) { + uni.showModal({ + title: '断开连接', + content: '是否断开与该设备的连接?', + success: (res) => { + if (res.confirm) { + this.disconnectDevice(device); + } + } + }); + } else { + this.selectedDevice = device; + this.showModal = true; + } + }, - async confirmConnect() { - if (!this.selectedDevice) return; + async confirmConnect() { + if (!this.selectedDevice) return; - this.showModal = false; - this.isConnecting = true; + this.showModal = false; + this.isConnecting = true; - try { - xBlufi.notifyConnectBle({ - connect: true, - deviceId: this.selectedDevice.deviceId - }); + try { + xBlufi.notifyConnectBle({ + connect: true, + deviceId: this.selectedDevice.deviceId + }); - xBlufi.notifyInitBleEsp32({ - deviceId: this.selectedDevice.deviceId - }); + xBlufi.notifyInitBleEsp32({ + deviceId: this.selectedDevice.deviceId + }); - } catch (error) { - console.error('连接失败:', error); - uni.showToast({ - title: '连接失败', - icon: 'none' - }); - } finally { - this.isConnecting = false; - } - }, + } catch (error) { + console.error('连接失败:', error); + uni.showToast({ + title: '连接失败', + icon: 'none' + }); + } finally { + this.isConnecting = false; + } + }, - disconnectDevice(device) { - xBlufi.notifyConnectBle({ - connect: false, - deviceId: device.deviceId - }); - }, + disconnectDevice(device) { + xBlufi.notifyConnectBle({ + connect: false, + deviceId: device.deviceId + }); + }, - handleConnectionStatus(res) { - const index = this.devices.findIndex(d => d.deviceId === res.data.deviceId); - if (index !== -1) { - this.$set(this.devices[index], 'connected', res.result); - if (res.result) { - this.selectedDevice = this.devices[index]; - this.hasConnectedDevice = true; - } else { - this.hasConnectedDevice = false; - this.selectedDevice = null; - } - } + handleConnectionStatus(res) { + const index = this.devices.findIndex(d => d.deviceId === res.data.deviceId); + if (index !== -1) { + this.$set(this.devices[index], 'connected', res.result); + if (res.result) { + this.selectedDevice = this.devices[index]; + this.hasConnectedDevice = true; + } else { + this.hasConnectedDevice = false; + this.selectedDevice = null; + } + } - if (res.result) { - uni.showToast({ - title: '连接成功', - icon: 'success' - }); - } else { - uni.showToast({ - title: '连接断开', - icon: 'none' - }); - } - }, - rings() { - console.log('准备发送响铃命令'); + if (res.result) { + uni.showToast({ + title: '连接成功', + icon: 'success' + }); + } else { + 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' - }); - } - }); - }, + rings() { + if(!this.selectedDevice || !this.hasConnectedDevice) { + uni.showToast({ + title: '设备未连接', + icon: 'none' + }); + return; + } + + my.getBluetoothAdapterState({ + success: (res) => { + if(!res.available) { + uni.showToast({ + title: '蓝牙不可用', + icon: 'none' + }); + return; + } + this.sendRingCommand(); + } + }); + }, + sendRingCommand() { + my.getBLEDeviceServices({ + deviceId: this.selectedDevice.deviceId, + success: (res) => { + my.getBLEDeviceCharacteristics({ + deviceId: this.selectedDevice.deviceId, + serviceId: this.serviceId, + success: (res) => { + if(res.characteristics.some(c => c.properties.write)) { + my.writeBLECharacteristicValue({ + deviceId: this.selectedDevice.deviceId, + serviceId: this.serviceId, + characteristicId: this.characteristicId, + value: this.string2buffer("11play1@"), + success: (res) => { + console.log('发送响铃命令成功:', res); + uni.showToast({ + title: '发送成功', + icon: 'success' + }); + }, + fail: (err) => { + console.error('发送响铃命令失败:', err); + uni.showToast({ + title: '发送失败', + icon: 'none' + }); + // 失败后1秒后重试一次 + setTimeout(() => { + this.sendRingCommand(); + }, 1000); + } + }); + } + } + }); + } + }); + }, + string2buffer(str) { + let val = new ArrayBuffer(str.length); + let dataView = new DataView(val); + for (let i = 0; i < str.length; i++) { + dataView.setUint8(i, str.charCodeAt(i)); + } + return val; + }, + handleInitResult(res) { + if (res.result) { + console.log('设备初始化成功'); + } else { + console.error('设备初始化失败'); + uni.showToast({ + title: '设备初始化失败', + icon: 'none' + }); + } + }, - - - handleInitResult(res) { - if (res.result) { - console.log('设备初始化成功'); - } else { - console.error('设备初始化失败'); - uni.showToast({ - title: '设备初始化失败', - icon: 'none' - }); - } - }, - - closeModal() { - this.showModal = false; - this.selectedDevice = null; - } - } + closeModal() { + this.showModal = false; + this.selectedDevice = null; + } + } } diff --git a/pages/index/index.vue b/pages/index/index.vue index 7baa827..fb5e24c 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -2286,84 +2286,113 @@ export default { }, Binddevice() { - uni.getBluetoothAdapterState({ - success: function (res) { - console.log('蓝牙状态:', res.available) - }, - fail: function (res) { - console.log('获取蓝牙状态失败') - } - }) - this.devicesList = [] - this.maskepage = 4 - this.buletxt = '蓝牙连接中' - // uni.showLoading({ - // title: '蓝牙扫描中..' - // }) - xBlufi.initXBlufi(1); - console.log("xBlufi", xBlufi.XMQTT_SYSTEM); - xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent); - xBlufi.notifyStartDiscoverBle({ - 'isStart': true - }); + console.log('Binddevice'); - - // 两秒后停止蓝牙搜索 - setTimeout(() => { - xBlufi.notifyStartDiscoverBle({ - 'isStart': false - }); - setTimeout(() => { - // uni.hideLoading() - if (this.devicesList.length == 0) { - - this.maskepage = 5 - } else { - let uniqueDevicesList = Array.from(new Set(this.devicesList)); - - // 将去重后的数组重新赋值给 this.devicesList - this.devicesList = uniqueDevicesList; - console.log(this.devicesList, 'this.devicesListthis.devicesList'); - let istrue = false - 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; - istrue = true - this.createBLEConnection(device) - - console.log('对比正确1111111111'); - } else { - console.log('对比错误'); - - } + // 检查蓝牙状态 + my.getBluetoothAdapterState({ + success: (res) => { + console.log('蓝牙状态:', res.available); + if (!res.available) { + uni.showToast({ + title: '请打开手机蓝牙', + icon: 'none' }); - setTimeout(() => { - if (!istrue) { - this.buletxt = '蓝牙连接失败' - - - setTimeout(() => { - this.maskepage = 5 - }, 500) - } - - }, 500) + return; } + // 初始化扫描参数 + this.maskloading = true; + this.devicesList = []; + this.maskepage = 4; + this.buletxt = '蓝牙连接中'; - }, 200) - // 判断是否存在浇花器设备 + // 初始化蓝牙模块 + xBlufi.initXBlufi(0); + console.log("xBlufi", xBlufi.XMQTT_SYSTEM); - // this.status = true - }, 5000) + // 监听设备消息 + xBlufi.listenDeviceMsgEvent(true, (res) => { + console.log("设备消息:", res); + if (res.type === xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED) { + if (res.result) { + this.carstause = true; + if (this.maskloading && this.maskepage === 4) { + this.buletxt = '蓝牙连接成功!'; + } + } else { + this.carstause = false; + if (this.maskepage === 4) { + this.buletxt = '设备连接失败'; + setTimeout(() => { + this.maskepage = 5; + }, 800); + } + } + } + }); + // 开始扫描设备 + xBlufi.notifyStartDiscoverBle({ + 'isStart': true + }); + + // 5秒后停止扫描 + setTimeout(() => { + xBlufi.notifyStartDiscoverBle({ + 'isStart': false + }); + + setTimeout(() => { + if (this.devicesList.length === 0) { + this.maskepage = 5; + this.buletxt = '暂无发现对应设备,请靠近设备'; + return; + } + + // 过滤并去重设备列表 + let uniqueDevicesList = Array.from(new Set(this.devicesList)); + this.devicesList = uniqueDevicesList; + console.log(this.devicesList, 'devicesList'); + + // 查找匹配的设备 + let istrue = false; + this.devicesList.forEach(device => { + let macFromName = device.name.substring(device.name.length - 12); + console.log(macFromName, 'macFromName'); + + if (macFromName === this.mac) { + console.log('找到匹配设备:', device); + this.deviceInfoss = device; + istrue = true; + + // 连接设备 + xBlufi.notifyConnectBle({ + connect: true, + deviceId: device.deviceId, + name: device.name + }); + } + }); + + // 处理未找到设备的情况 + setTimeout(() => { + if (!istrue) { + this.buletxt = '蓝牙连接失败'; + setTimeout(() => { + this.maskepage = 5; + }, 500); + } + }, 500); + }, 200); + }, 5000); + }, + fail: () => { + uni.showToast({ + title: '获取蓝牙状态失败', + icon: 'none' + }); + } + }); }, cheackgps() { let that = this @@ -2866,7 +2895,7 @@ export default { longitude: parseFloat(item.longitude), width: 40, height: 28, - areaId:item.areaId, + areaId: item.areaId, iconPath: this.defaultMarkerIconPath, isDeviceMarker: true // 区分为设备标记 })); diff --git a/pages_admin/order/device_detail.vue b/pages_admin/order/device_detail.vue index 1598aad..7736201 100644 --- a/pages_admin/order/device_detail.vue +++ b/pages_admin/order/device_detail.vue @@ -22,24 +22,24 @@ - + - + - SN:{{deviceInfos.sn}} + SN:{{ deviceInfos.sn }} - MAC:{{deviceInfos.mac}} + MAC:{{ deviceInfos.mac }} - 车牌号:{{deviceInfos.vehicleNum}} + 车牌号:{{ deviceInfos.vehicleNum }} -- 车辆型号: - {{deviceInfos.model}} + {{ deviceInfos.model }} -- @@ -80,14 +80,14 @@ 电池电压: - {{deviceInfos.voltage}}V + {{ deviceInfos.voltage }}V -- 4G信号: - {{deviceInfos.satellites}} + {{ deviceInfos.satellites }} -- - + @@ -112,7 +112,8 @@ - + 最后订单信息 @@ -120,27 +121,27 @@ 最后用户姓名: - {{ deviceInfos.etOrders[0].realName }} + {{ deviceInfos.etOrders[0].realName + }} -- 最后用户电话: - {{ deviceInfos.etOrders[0].phonenumber }} + {{ + deviceInfos.etOrders[0].phonenumber }} -- 最后订单编号: - {{ deviceInfos.etOrders[0].orderNo }} + {{ deviceInfos.etOrders[0].orderNo + }} -- 订单开始时间: - {{ deviceInfos.etOrders[0].createTime }} + {{ + deviceInfos.etOrders[0].createTime }} -- @@ -621,8 +622,9 @@ export default { }) console.log('点击了....1'); this.$u.post('/appVerify/admin/unlocking?sn=' + this.sn).then((res) => { - + if (res.code == 200) { + uni.hideLoading() // 处理接口返回的数据,将边界数据转换为地图组件需要的折线结构 this.deviceInfo() uni.showToast({ @@ -837,89 +839,145 @@ export default { Binddevice() { - uni.getBluetoothAdapterState({ - success: function (res) { - console.log('蓝牙状态:', res.available) - }, - fail: function (res) { - console.log('获取蓝牙状态失败') - } - }) - this.maskloading = true - this.devicesList = [] - this.maskepage = 4 - this.buletxt = '蓝牙连接中' - // uni.showLoading({ - // title: '蓝牙扫描中..' - // }) - xBlufi.initXBlufi(1); - console.log("xBlufi", xBlufi.XMQTT_SYSTEM); - xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent); - xBlufi.notifyStartDiscoverBle({ - 'isStart': true + console.log('Binddevice'); + + // 检查蓝牙状态 + // my.getBluetoothAdapterState({ + // success: (res) => { + // console.log('蓝牙状态:', res.available); + // if (!res.available) { + // this.maskepage = 5; + // this.buletxt = '请打开手机蓝牙'; + // return; + // } + + // }, + // fail: () => { + // this.maskepage = 5; + // this.buletxt = '获取蓝牙状态失败'; + // } + // }); + + // 初始化蓝牙模块并开始扫描 + this.maskloading = true; + this.devicesList = []; + this.maskepage = 4; + this.buletxt = '蓝牙连接中'; + + // 初始化蓝牙模块 + xBlufi.initXBlufi(0); + xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent); + + // 开始扫描设备 + xBlufi.notifyStartDiscoverBle({ + 'isStart': true + }); + + // 设置扫描超时 + setTimeout(() => { + xBlufi.notifyStartDiscoverBle({ + 'isStart': false + }); + + setTimeout(() => { + if (this.devicesList.length === 0) { + this.maskepage = 5; + this.buletxt = '未发现设备'; + return; + } + + // 过滤并去重设备列表 + let uniqueDevicesList = Array.from(new Set(this.devicesList)); + this.devicesList = uniqueDevicesList.filter(device => + device.name && device.name.startsWith('BBLE') + ); + + // 查找目标设备 + const targetDevice = this.devicesList.find(device => { + const macFromName = device.name.substring(device.name.length - 12); + return macFromName === this.mac; + }); + + if (targetDevice) { + this.deviceInfoss = targetDevice; + // 连接设备 + // xBlufi.notifyConnectBle({ + // connect: true, + // deviceId: targetDevice.deviceId, + // name: targetDevice.name + // }); + this.connectToDevice(targetDevice); + } else { + this.buletxt = '蓝牙连接失败'; + setTimeout(() => { + this.maskepage = 5; + }, 500); + } + }, 200); + }, 5000); + + }, + + + + + + // 连接到设备 + connectToDevice(device) { + xBlufi.notifyConnectBle({ + connect: true, + deviceId: device.deviceId, + name: device.name }); + // 监听连接状态 + xBlufi.listenDeviceMsgEvent(true, (res) => { + if (res.type === xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED) { + if (res.result) { + this.carstause = true; + if (this.maskloading && this.maskepage === 4) { + if (!this.buleclose && !this.buleopen && !this.bulerebort && !this.bulering) { + this.buletxt = '蓝牙连接成功!'; + } - // 两秒后停止蓝牙搜索 - setTimeout(() => { - xBlufi.notifyStartDiscoverBle({ - 'isStart': false - }); - setTimeout(() => { - // uni.hideLoading() - if (this.devicesList.length == 0) { - // uni.showToast({ - // title: '暂无发现对应设备,请靠近设备', - // icon: 'none', - // duration: 1500 - // }); - this.maskepage = 5 + // 处理各种蓝牙操作 + if (this.buleclose) { + this.buleclose = false; + this.close(); + } + if (this.buleopen) { + + + this.buleopen = false; + this.open(); + } + if (this.bulerebort) { + this.bulerebort = false; + this.reboot(); + } + if (this.bulering) { + this.bulering = false; + this.ring(); + } + } } else { - let uniqueDevicesList = Array.from(new Set(this.devicesList)); - - // 将去重后的数组重新赋值给 this.devicesList - this.devicesList = uniqueDevicesList; - console.log(this.devicesList, 'this.devicesListthis.devicesList'); - let istrue = false - 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; - istrue = true - this.createBLEConnection(device) - - console.log('对比正确1111111111'); - } else { - console.log('对比错误'); - - } - }); - setTimeout(() => { - if (!istrue) { - this.buletxt = '蓝牙连接失败' - - - setTimeout(() => { - this.maskepage = 5 - }, 500) - } - - }, 500) + this.carstause = false; + if (this.maskepage === 4) { + this.buletxt = '设备连接失败'; + setTimeout(() => { + this.maskepage = 5; + }, 800); + } } + } + }); - - }, 200) - // 判断是否存在浇花器设备 - - // this.status = true - }, 5000) - + // 初始化设备 + setTimeout(() => { + xBlufi.notifyInitBleEsp32({ + deviceId: device.deviceId + }); + }, 1000); }, @@ -962,7 +1020,10 @@ export default { xBlufi.notifySendCustomData({ customData: "11open" }); - vm.maskloading = false + setTimeout(() => { + vm.maskloading = false + }, 1000); + vm.deviceInfo() // uni.hideLoading() // vm.remakr() @@ -1067,15 +1128,7 @@ export default { 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 @@ -1100,18 +1153,7 @@ export default { }, 800) } - // 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_CUSTON_DATA: @@ -1183,22 +1225,7 @@ export default { this.maskepage = 5 }, 800) } - // 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; @@ -1382,7 +1409,7 @@ export default { padding: 10, display: 'ALWAYS' } - + } console.log('创建的marker:', marker) // 添加日志 return marker @@ -1627,7 +1654,7 @@ export default { isParking: true }); }); - console.log(this.markers,'aaaaaaaaaaaaaaa'); + console.log(this.markers, 'aaaaaaaaaaaaaaa'); this.$set(this, 'markers', [...this.markers, ...newMarkers]); const validBoundaries1 = type1Data.map(row => row.boundaryStr).filter(boundary => typeof boundary === 'string' && boundary.trim() !== ''); @@ -1641,7 +1668,7 @@ export default { typeof boundary === 'string' && boundary.trim() !== ''); const polylines3 = this.convertBoundaryToPolylines(validBoundaries3, 3); // 将处理后的数据添加到 this.polyline 中 - + this.polyline = this.polyline.concat(polylines1, polylines2, polylines3); this.parkingList = res.rows // console.log(this.polyline); diff --git a/utils/bluetooth.js b/utils/bluetooth.js deleted file mode 100644 index 8602019..0000000 --- a/utils/bluetooth.js +++ /dev/null @@ -1,342 +0,0 @@ -export class BluetoothManager { - constructor() { - this.isInitialized = false - this.deviceFoundCallback = null - this.onReceiveDataCallback = null - this.isConnected = false - this.retryTimes = 3 // 重试次数 - this.retryDelay = 1000 // 重试延迟(ms) - - // 添加常用的服务和特征值UUID - this.SERVICE_UUID = "000000FF-0000-1000-8000-00805F9B34FB" - this.WRITE_CHARACTERISTIC_UUID = "0000FF01-0000-1000-8000-00805F9B34FB" - this.NOTIFY_CHARACTERISTIC_UUID = "0000FF02-0000-1000-8000-00805F9B34FB" - } - - // 延迟函数 - delay(ms) { - return new Promise(resolve => setTimeout(resolve, ms)) - } - - // 带重试的异步操作 - async withRetry(operation, errorMessage) { - let lastError - for(let i = 0; i < this.retryTimes; i++) { - try { - return await operation() - } catch(error) { - console.log(`${errorMessage} 尝试第 ${i + 1} 次失败:`, error) - lastError = error - if(i < this.retryTimes - 1) { - await this.delay(this.retryDelay) - } - } - } - throw lastError - } - - async init() { - if (this.isInitialized) return - - try { - // 先关闭蓝牙适配器再重新打开 - try { - await my.closeBluetoothAdapter() - await this.delay(1000) - } catch(e) { - console.log('关闭蓝牙适配器失败,可能本来就是关闭状态') - } - - await this.withRetry( - () => this.openBluetoothAdapter(), - '初始化蓝牙适配器失败' - ) - - this.isInitialized = true - - // 监听蓝牙连接状态变化 - my.onBLEConnectionStateChanged((res) => { - this.isConnected = res.connected - if (!res.connected) { - console.log('蓝牙连接已断开') - } - }) - - // 监听特征值变化 - my.onBLECharacteristicValueChange((res) => { - if(this.onReceiveDataCallback) { - const value = this.ab2str(res.value) - this.onReceiveDataCallback(value) - } - }) - - } catch (error) { - this.isInitialized = false - throw new Error('蓝牙初始化失败: ' + error.message) - } - } - - async openBluetoothAdapter() { - return new Promise((resolve, reject) => { - my.openBluetoothAdapter({ - success: () => { - resolve() - }, - fail: (error) => { - if(error.error === 10015) { - reject(new Error('蓝牙未打开,请打开手机蓝牙')) - } else { - reject(error) - } - } - }) - }) - } - - // ArrayBuffer转字符串 - ab2str(buf) { - return String.fromCharCode.apply(null, new Uint8Array(buf)) - } - - // 字符串转ArrayBuffer - str2ab(str) { - let buf = new ArrayBuffer(str.length) - let bufView = new Uint8Array(buf) - for (let i = 0, strLen = str.length; i < strLen; i++) { - bufView[i] = str.charCodeAt(i) - } - return buf - } - - async startSearch() { - if(!this.isInitialized) { - await this.init() - } - - await this.withRetry(async () => { - return new Promise((resolve, reject) => { - my.startBluetoothDevicesDiscovery({ - allowDuplicatesKey: false, - success: () => { - this.startListeningForDevices() - resolve() - }, - fail: (error) => { - reject(error) - } - }) - }) - }, '开始搜索设备失败') - } - - startListeningForDevices() { - my.onBluetoothDeviceFound((res) => { - if (this.deviceFoundCallback) { - res.devices.forEach(device => { - const deviceName = device.name || device.localName - // 只处理前缀为BBLE的设备 - if (deviceName && deviceName.startsWith('BBLE')) { - this.deviceFoundCallback({ - name: deviceName, - deviceId: device.deviceId, - RSSI: device.RSSI, - advertisData: device.advertisData, - connected: false, - mac: deviceName.substring(4) - }) - } - }) - } - }) - } - - async stopSearch() { - return new Promise((resolve, reject) => { - my.stopBluetoothDevicesDiscovery({ - success: () => { - resolve() - }, - fail: (error) => { - reject(error) - } - }) - }) - } - - async connect(deviceId) { - console.log('开始连接设备:', deviceId) - try { - // 先断开可能存在的连接 - try { - await this.disconnect(deviceId) - await this.delay(1000) - } catch(e) { - console.log('断开旧连接失败,可能本来就未连接') - } - - // 重试连接流程 - await this.withRetry(async () => { - // 1. 建立连接 - await this.createConnection(deviceId) - - // 2. 获取服务 - const services = await this.getServices(deviceId) - const targetService = services.find(s => s.uuid.toLowerCase() === this.SERVICE_UUID.toLowerCase()) - if (!targetService) throw new Error('未找到目标服务') - - // 3. 获取特征值 - const characteristics = await this.getCharacteristics(deviceId, targetService.uuid) - - // 4. 启用通知 - await this.enableNotify(deviceId, targetService.uuid, this.NOTIFY_CHARACTERISTIC_UUID) - - this.isConnected = true - return true - }, '连接设备失败') - - } catch (error) { - this.isConnected = false - console.error('连接失败:', error) - throw error - } - } - - async createConnection(deviceId) { - return new Promise((resolve, reject) => { - my.connectBLEDevice({ - deviceId, - timeout: 15000, - success: (res) => { - resolve(res) - }, - fail: (error) => { - if(error.error === 10015) { - reject(new Error('蓝牙连接失败,请检查设备是否在范围内')) - } else { - reject(error) - } - } - }) - }) - } - - async getServices(deviceId) { - return new Promise((resolve, reject) => { - my.getBLEDeviceServices({ - deviceId, - success: (res) => { - console.log('获取服务成功:', res.services) - resolve(res.services) - }, - fail: (error) => { - console.error('获取服务失败:', error) - reject(error) - } - }) - }) - } - - async getCharacteristics(deviceId, serviceId) { - return new Promise((resolve, reject) => { - my.getBLEDeviceCharacteristics({ - deviceId, - serviceId, - success: (res) => { - console.log('获取特征值成功:', res.characteristics) - resolve(res.characteristics) - }, - fail: (error) => { - console.error('获取特征值失败:', error) - reject(error) - } - }) - }) - } - - async enableNotify(deviceId, serviceId, characteristicId) { - return new Promise((resolve, reject) => { - my.notifyBLECharacteristicValueChange({ - deviceId, - serviceId, - characteristicId, - state: true, - success: (res) => { - resolve(res) - }, - fail: (error) => { - reject(error) - } - }) - }) - } - - async disconnect(deviceId) { - return new Promise((resolve, reject) => { - my.disconnectBLEDevice({ - deviceId, - success: () => { - this.isConnected = false - resolve() - }, - fail: (error) => { - reject(error) - } - }) - }) - } - - // 发送数据 - async sendData(deviceId, data) { - if (!this.isConnected) { - throw new Error('蓝牙未连接') - } - - const buffer = this.str2ab(data) - return this.writeBLECharacteristicValue( - deviceId, - this.SERVICE_UUID, - this.WRITE_CHARACTERISTIC_UUID, - buffer - ) - } - - async writeBLECharacteristicValue(deviceId, serviceId, characteristicId, value) { - return new Promise((resolve, reject) => { - my.writeBLECharacteristicValue({ - deviceId, - serviceId, - characteristicId, - value, - success: () => { - resolve() - }, - fail: (error) => { - reject(error) - } - }) - }) - } - - onDeviceFound(callback) { - this.deviceFoundCallback = callback - } - - // 注册数据接收回调 - onReceiveData(callback) { - this.onReceiveDataCallback = callback - } - - async destroy() { - if (this.isInitialized) { - try { - await this.stopSearch() - await this.delay(1000) - await my.closeBluetoothAdapter() - this.isInitialized = false - this.isConnected = false - } catch (error) { - console.error('销毁蓝牙实例失败:', error) - } - } - } -} \ No newline at end of file