fix: 代码功能优化。
This commit is contained in:
@@ -45,8 +45,6 @@
|
||||
</view>
|
||||
|
||||
|
||||
|
||||
|
||||
<!-- Device List -->
|
||||
<view class="device-list">
|
||||
<view class="list-header">
|
||||
@@ -267,7 +265,7 @@ const openBluetoothAdapter = () => {
|
||||
console.log('蓝牙适配器初始化成功', res)
|
||||
bluetoothEnabled.value = true
|
||||
uni.showToast({
|
||||
title: '蓝牙已开启',
|
||||
title: 'Bluetooth Enabled',
|
||||
icon: 'success'
|
||||
})
|
||||
|
||||
@@ -284,8 +282,8 @@ const openBluetoothAdapter = () => {
|
||||
if (err.errCode === 10001) {
|
||||
// 蓝牙未开启
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '请在手机设置中开启蓝牙',
|
||||
title: 'Notice',
|
||||
content: 'Please enable Bluetooth in phone settings',
|
||||
showCancel: false
|
||||
})
|
||||
} else if (err.errCode === -1) {
|
||||
@@ -569,7 +567,7 @@ const tryAutoReconnectLastDevice = async () => {
|
||||
uni.hideLoading()
|
||||
|
||||
uni.showToast({
|
||||
title: '搜索失败,请手动操作',
|
||||
title: 'Search Failed, Manual Operation Required',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
@@ -585,7 +583,7 @@ const tryAutoReconnectLastDevice = async () => {
|
||||
const startBluetoothSearch = async () => {
|
||||
if (!bluetoothEnabled.value) {
|
||||
uni.showToast({
|
||||
title: '请先开启蓝牙',
|
||||
title: 'Please Enable Bluetooth First',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
@@ -610,7 +608,7 @@ const startBluetoothSearch = async () => {
|
||||
success: (res) => {
|
||||
console.log('开始搜索蓝牙设备', res)
|
||||
uni.showToast({
|
||||
title: '开始搜索设备',
|
||||
title: 'Start Searching Devices',
|
||||
icon: 'success'
|
||||
})
|
||||
// 监听设备发现
|
||||
@@ -621,14 +619,14 @@ const startBluetoothSearch = async () => {
|
||||
isSearching.value = false
|
||||
|
||||
// 根据错误码提供详细提示
|
||||
let errorMsg = '搜索失败,请重试'
|
||||
let errorMsg = 'Search Failed, Please Retry'
|
||||
|
||||
if (err.errCode === 10001) {
|
||||
errorMsg = '搜索失败:未开启蓝牙适配器'
|
||||
errorMsg = 'Search Failed: Bluetooth Adapter Not Enabled'
|
||||
} else if (err.errCode === 10002) {
|
||||
errorMsg = '搜索失败:未找到蓝牙设备'
|
||||
errorMsg = 'Search Failed: No Bluetooth Device Found'
|
||||
} else if (err.errCode === 10004) {
|
||||
errorMsg = '搜索失败:请检查是否已开启位置服务(GPS)'
|
||||
errorMsg = 'Search Failed: Please Check if Location Service (GPS) is Enabled'
|
||||
// 可能是GPS未开启导致的
|
||||
guideToEnableGPS()
|
||||
return
|
||||
@@ -650,7 +648,7 @@ const stopBluetoothSearch = () => {
|
||||
console.log('停止搜索蓝牙设备', res)
|
||||
isSearching.value = false
|
||||
uni.showToast({
|
||||
title: '已停止搜索',
|
||||
title: 'Search Stopped',
|
||||
icon: 'success'
|
||||
})
|
||||
},
|
||||
@@ -697,7 +695,7 @@ const connectDevice = (device) => {
|
||||
// 如果正在连接中,不允许再次连接
|
||||
if (connectingDeviceId.value) {
|
||||
uni.showToast({
|
||||
title: '正在连接中,请稍候',
|
||||
title: 'Connecting, Please Wait',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
@@ -706,7 +704,7 @@ const connectDevice = (device) => {
|
||||
// 如果已经连接了同一个设备,不需要重复连接
|
||||
if (connectedDeviceId.value === device.deviceId) {
|
||||
uni.showToast({
|
||||
title: '该设备已连接',
|
||||
title: 'Device Already Connected',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
@@ -898,10 +896,20 @@ const getBLEDeviceServices = async (deviceId) => {
|
||||
|
||||
// 计算所有服务的特征值总数
|
||||
let totalCount = 0
|
||||
let writeCharacteristicId = '' // 可写特征值
|
||||
let notifyCharacteristicId = '' // 可监听特征值
|
||||
let writeCharacteristicId = '' // 通用可写特征值
|
||||
let notifyCharacteristicId = '' // 通用可监听特征值
|
||||
let mainServiceId = '' // 主服务ID
|
||||
|
||||
// 🔧 收集所有协议特征值的128位UUID(用于mixer.vue)
|
||||
const protocolCharacteristics = {
|
||||
status: '', // 0xFF01 - 电机状态
|
||||
speed: '', // 0xFF02 - 电机速度
|
||||
direction: '', // 0xFF03 - 运行方向
|
||||
timer: '', // 0xFF04 - 定时设置
|
||||
remaining: '', // 0xFF05 - 剩余时间
|
||||
allProps: '' // 0xFF06 - 所有属性
|
||||
}
|
||||
|
||||
// 查找协议规定的服务ID (0x00FF)
|
||||
// 注意:不同设备可能返回不同格式的UUID,需要兼容匹配
|
||||
if (res.services.length > 0) {
|
||||
@@ -925,22 +933,45 @@ const getBLEDeviceServices = async (deviceId) => {
|
||||
|
||||
// 找出可以监听的特征值
|
||||
serviceChars.forEach(characteristic => {
|
||||
// 查找可写特征值(优先选择 write)
|
||||
const charUUID = characteristic.uuid.toUpperCase()
|
||||
|
||||
// 🔧 识别协议规定的特征值(通过包含匹配128位UUID)
|
||||
if (charUUID.includes('FF01') || charUUID.includes('0XFF01')) {
|
||||
protocolCharacteristics.status = characteristic.uuid
|
||||
console.log('✅ 找到电机状态特征值(0xFF01):', characteristic.uuid)
|
||||
} else if (charUUID.includes('FF02') || charUUID.includes('0XFF02')) {
|
||||
protocolCharacteristics.speed = characteristic.uuid
|
||||
console.log('✅ 找到电机速度特征值(0xFF02):', characteristic.uuid)
|
||||
} else if (charUUID.includes('FF03') || charUUID.includes('0XFF03')) {
|
||||
protocolCharacteristics.direction = characteristic.uuid
|
||||
console.log('✅ 找到运行方向特征值(0xFF03):', characteristic.uuid)
|
||||
} else if (charUUID.includes('FF04') || charUUID.includes('0XFF04')) {
|
||||
protocolCharacteristics.timer = characteristic.uuid
|
||||
console.log('✅ 找到定时设置特征值(0xFF04):', characteristic.uuid)
|
||||
} else if (charUUID.includes('FF05') || charUUID.includes('0XFF05')) {
|
||||
protocolCharacteristics.remaining = characteristic.uuid
|
||||
console.log('✅ 找到剩余时间特征值(0xFF05):', characteristic.uuid)
|
||||
} else if (charUUID.includes('FF06') || charUUID.includes('0XFF06')) {
|
||||
protocolCharacteristics.allProps = characteristic.uuid
|
||||
console.log('✅ 找到所有属性特征值(0xFF06):', characteristic.uuid)
|
||||
}
|
||||
|
||||
// 查找通用可写特征值(优先选择 write)
|
||||
if (characteristic.properties.write && !writeCharacteristicId) {
|
||||
writeCharacteristicId = characteristic.uuid
|
||||
if (!mainServiceId) mainServiceId = service.uuid
|
||||
console.log('找到可写特征值(write):', writeCharacteristicId)
|
||||
console.log('找到通用可写特征值(write):', writeCharacteristicId)
|
||||
} else if (characteristic.properties.writeNoResponse && !writeCharacteristicId) {
|
||||
writeCharacteristicId = characteristic.uuid
|
||||
if (!mainServiceId) mainServiceId = service.uuid
|
||||
console.log('找到可写特征值(writeNoResponse):', writeCharacteristicId)
|
||||
console.log('找到通用可写特征值(writeNoResponse):', writeCharacteristicId)
|
||||
}
|
||||
|
||||
// 查找可监听特征值
|
||||
if (characteristic.properties.notify || characteristic.properties.indicate) {
|
||||
if (!notifyCharacteristicId) {
|
||||
notifyCharacteristicId = characteristic.uuid
|
||||
console.log('找到可监听特征值:', notifyCharacteristicId)
|
||||
console.log('找到通用可监听特征值:', notifyCharacteristicId)
|
||||
}
|
||||
|
||||
notifyCharacteristics.value.push({
|
||||
@@ -957,13 +988,29 @@ const getBLEDeviceServices = async (deviceId) => {
|
||||
|
||||
totalCharacteristicsCount.value = totalCount
|
||||
console.log(`总共 ${totalCount} 个特征值`)
|
||||
|
||||
// 🔧 打印所有协议特征值
|
||||
console.log('📋 协议特征值UUID汇总:')
|
||||
console.log(' 0xFF01 (状态):', protocolCharacteristics.status || '未找到')
|
||||
console.log(' 0xFF02 (速度):', protocolCharacteristics.speed || '未找到')
|
||||
console.log(' 0xFF03 (方向):', protocolCharacteristics.direction || '未找到')
|
||||
console.log(' 0xFF04 (定时):', protocolCharacteristics.timer || '未找到')
|
||||
console.log(' 0xFF05 (剩余):', protocolCharacteristics.remaining || '未找到')
|
||||
console.log(' 0xFF06 (全部):', protocolCharacteristics.allProps || '未找到')
|
||||
}
|
||||
|
||||
// 返回蓝牙信息(用于跳转)
|
||||
// 返回蓝牙信息(用于跳转),包含所有协议特征值
|
||||
return {
|
||||
serviceId: mainServiceId,
|
||||
characteristicId: writeCharacteristicId,
|
||||
notifyCharacteristicId: notifyCharacteristicId
|
||||
notifyCharacteristicId: notifyCharacteristicId,
|
||||
// 🔧 添加所有协议特征值的128位UUID
|
||||
statusCharId: protocolCharacteristics.status,
|
||||
speedCharId: protocolCharacteristics.speed,
|
||||
directionCharId: protocolCharacteristics.direction,
|
||||
timerCharId: protocolCharacteristics.timer,
|
||||
remainingCharId: protocolCharacteristics.remaining,
|
||||
allPropsCharId: protocolCharacteristics.allProps
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('获取服务列表失败', err)
|
||||
@@ -989,9 +1036,6 @@ const getBLEDeviceCharacteristicsSync = (deviceId, serviceId) => {
|
||||
})
|
||||
}
|
||||
|
||||
// 预览控制页面(模拟设备数据)
|
||||
|
||||
|
||||
// 构造跳转到 mixer 页面的 URL(带蓝牙信息)
|
||||
const buildMixerPageUrl = (device, bleInfo) => {
|
||||
const params = {
|
||||
@@ -1010,6 +1054,28 @@ const buildMixerPageUrl = (device, bleInfo) => {
|
||||
params.notifyCharacteristicId = bleInfo.notifyCharacteristicId
|
||||
}
|
||||
|
||||
// 🔧 添加所有协议特征值的128位UUID
|
||||
if (bleInfo && bleInfo.statusCharId) {
|
||||
params.statusCharId = bleInfo.statusCharId
|
||||
}
|
||||
if (bleInfo && bleInfo.speedCharId) {
|
||||
params.speedCharId = bleInfo.speedCharId
|
||||
}
|
||||
if (bleInfo && bleInfo.directionCharId) {
|
||||
params.directionCharId = bleInfo.directionCharId
|
||||
}
|
||||
if (bleInfo && bleInfo.timerCharId) {
|
||||
params.timerCharId = bleInfo.timerCharId
|
||||
}
|
||||
if (bleInfo && bleInfo.remainingCharId) {
|
||||
params.remainingCharId = bleInfo.remainingCharId
|
||||
}
|
||||
if (bleInfo && bleInfo.allPropsCharId) {
|
||||
params.allPropsCharId = bleInfo.allPropsCharId
|
||||
}
|
||||
|
||||
console.log('🔗 跳转参数:', params)
|
||||
|
||||
// 拼接 URL
|
||||
const queryString = Object.keys(params)
|
||||
.map(key => `${key}=${params[key]}`)
|
||||
@@ -1065,7 +1131,7 @@ const onBLECharacteristicValueChange = () => {
|
||||
|
||||
// 显示接收到的数据
|
||||
uni.showToast({
|
||||
title: `收到数据: ${displayData.slice(0, 20)}${displayData.length > 20 ? '...' : ''}`,
|
||||
title: `Received Data: ${displayData.slice(0, 20)}${displayData.length > 20 ? '...' : ''}`,
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
@@ -1076,7 +1142,7 @@ const onBLECharacteristicValueChange = () => {
|
||||
const writeBLEData = async (data) => {
|
||||
if (!connectedDeviceId.value) {
|
||||
uni.showToast({
|
||||
title: '请先连接设备',
|
||||
title: 'Please Connect Device First',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
@@ -1084,14 +1150,14 @@ const writeBLEData = async (data) => {
|
||||
|
||||
if (!services.value || services.value.length === 0) {
|
||||
uni.showToast({
|
||||
title: '设备服务未就绪,请稍后重试',
|
||||
title: 'Device Service Not Ready, Please Retry Later',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
uni.showLoading({
|
||||
title: '正在发送数据...',
|
||||
title: 'Sending Data...',
|
||||
mask: true
|
||||
})
|
||||
|
||||
@@ -1129,8 +1195,8 @@ const writeBLEData = async (data) => {
|
||||
|
||||
if (!writeCharacteristic) {
|
||||
uni.showModal({
|
||||
title: '无法发送',
|
||||
content: '该设备没有可写入的特征值\n\n可能原因:\n1. 设备不支持写入操作\n2. 设备固件限制\n\n建议:查看设备文档或联系设备厂商',
|
||||
title: 'Unable to Send',
|
||||
content: 'This device has no writable characteristic\n\nPossible reasons:\n1. Device does not support write operation\n2. Device firmware restriction\n\nSuggestion: Check device documentation or contact manufacturer',
|
||||
showCancel: false
|
||||
})
|
||||
return
|
||||
@@ -1148,8 +1214,8 @@ const writeBLEData = async (data) => {
|
||||
// 检查数据长度(蓝牙一般限制在20字节以内)
|
||||
if (buffer.byteLength > 20) {
|
||||
uni.showModal({
|
||||
title: '数据过长',
|
||||
content: `数据长度${buffer.byteLength}字节,超过蓝牙限制(20字节)\n\n是否分包发送?`,
|
||||
title: 'Data Too Long',
|
||||
content: `Data length ${buffer.byteLength} bytes exceeds Bluetooth limit (20 bytes)\n\nSend in chunks?`,
|
||||
success: async (res) => {
|
||||
if (res.confirm) {
|
||||
await sendDataInChunks(data, writeServiceId, writeCharacteristic.uuid)
|
||||
@@ -1172,7 +1238,7 @@ const writeBLEData = async (data) => {
|
||||
addToHistory('send', data, hexString)
|
||||
|
||||
uni.showToast({
|
||||
title: `发送成功: ${data}`,
|
||||
title: `Send Success: ${data}`,
|
||||
icon: 'success',
|
||||
duration: 2000
|
||||
})
|
||||
@@ -1180,26 +1246,26 @@ const writeBLEData = async (data) => {
|
||||
fail: (err) => {
|
||||
console.error('写入数据失败', err)
|
||||
|
||||
let errorMsg = '发送失败'
|
||||
let errorMsg = 'Send Failed'
|
||||
let errorDetail = ''
|
||||
|
||||
if (err.errCode === 10008) {
|
||||
errorMsg = '发送失败:数据格式错误'
|
||||
errorDetail = '请检查输入的数据格式'
|
||||
errorMsg = 'Send Failed: Data Format Error'
|
||||
errorDetail = 'Please check input data format'
|
||||
} else if (err.errCode === 10007) {
|
||||
errorMsg = '发送失败:特征值不支持写入'
|
||||
errorDetail = '该特征值不支持写入操作'
|
||||
errorMsg = 'Send Failed: Characteristic Not Writable'
|
||||
errorDetail = 'This characteristic does not support write operation'
|
||||
} else if (err.errCode === 10006) {
|
||||
errorMsg = '发送失败:设备连接已断开'
|
||||
errorDetail = '请重新连接设备'
|
||||
errorMsg = 'Send Failed: Device Connection Lost'
|
||||
errorDetail = 'Please reconnect device'
|
||||
} else if (err.errMsg) {
|
||||
errorMsg = '发送失败'
|
||||
errorMsg = 'Send Failed'
|
||||
errorDetail = err.errMsg
|
||||
}
|
||||
|
||||
uni.showModal({
|
||||
title: errorMsg,
|
||||
content: errorDetail + `\n\n错误码: ${err.errCode || '未知'}`,
|
||||
content: errorDetail + `\n\nError Code: ${err.errCode || 'Unknown'}`,
|
||||
showCancel: false
|
||||
})
|
||||
}
|
||||
@@ -1208,7 +1274,7 @@ const writeBLEData = async (data) => {
|
||||
uni.hideLoading()
|
||||
console.error('发送数据异常', err)
|
||||
uni.showToast({
|
||||
title: '发送失败',
|
||||
title: 'Send Failed',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
@@ -1230,7 +1296,7 @@ const sendDataInChunks = async (data, serviceId, characteristicId) => {
|
||||
console.log(`数据分为 ${chunks.length} 包发送`)
|
||||
|
||||
uni.showLoading({
|
||||
title: `发送中 0/${chunks.length}`,
|
||||
title: `Sending 0/${chunks.length}`,
|
||||
mask: true
|
||||
})
|
||||
|
||||
@@ -1245,7 +1311,7 @@ const sendDataInChunks = async (data, serviceId, characteristicId) => {
|
||||
success: () => {
|
||||
console.log(`第 ${i + 1}/${chunks.length} 包发送成功`)
|
||||
uni.showLoading({
|
||||
title: `发送中 ${i + 1}/${chunks.length}`,
|
||||
title: `Sending ${i + 1}/${chunks.length}`,
|
||||
mask: true
|
||||
})
|
||||
resolve()
|
||||
@@ -1269,7 +1335,7 @@ const sendDataInChunks = async (data, serviceId, characteristicId) => {
|
||||
addToHistory('send', data, hexString)
|
||||
|
||||
uni.showToast({
|
||||
title: `分包发送成功 (${chunks.length}包)`,
|
||||
title: `Chunked Send Success (${chunks.length} chunks)`,
|
||||
icon: 'success',
|
||||
duration: 2000
|
||||
})
|
||||
@@ -1277,7 +1343,7 @@ const sendDataInChunks = async (data, serviceId, characteristicId) => {
|
||||
uni.hideLoading()
|
||||
console.error('分包发送失败', err)
|
||||
uni.showToast({
|
||||
title: '分包发送失败',
|
||||
title: 'Chunked Send Failed',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
@@ -1288,7 +1354,7 @@ const sendDataInChunks = async (data, serviceId, characteristicId) => {
|
||||
const readBLEData = async () => {
|
||||
if (!connectedDeviceId.value || services.value.length === 0) {
|
||||
uni.showToast({
|
||||
title: '请先连接设备',
|
||||
title: 'Please Connect Device First',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
@@ -1305,7 +1371,7 @@ const readBLEData = async () => {
|
||||
// 执行读取操作
|
||||
const performRead = async () => {
|
||||
uni.showLoading({
|
||||
title: '正在读取数据...',
|
||||
title: 'Reading Data...',
|
||||
mask: true
|
||||
})
|
||||
|
||||
@@ -1350,7 +1416,7 @@ const performRead = async () => {
|
||||
|
||||
if (totalReadable === 0) {
|
||||
uni.showToast({
|
||||
title: '该设备没有可读特征值\n请等待设备推送数据',
|
||||
title: 'No Readable Characteristic\nWait for Device Push Data',
|
||||
icon: 'none',
|
||||
duration: 2500
|
||||
})
|
||||
@@ -1374,7 +1440,7 @@ const performRead = async () => {
|
||||
uni.hideLoading()
|
||||
console.error('读取数据失败', err)
|
||||
uni.showToast({
|
||||
title: '读取数据失败',
|
||||
title: 'Read Data Failed',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
@@ -1456,7 +1522,7 @@ const onBLEConnectionStateChange = () => {
|
||||
if (!res.connected && res.deviceId === connectedDeviceId.value) {
|
||||
cleanupConnection()
|
||||
uni.showToast({
|
||||
title: '设备已断开',
|
||||
title: 'Device Disconnected',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
@@ -1470,7 +1536,7 @@ const sendCustomData = () => {
|
||||
|
||||
if (!trimmedData) {
|
||||
uni.showToast({
|
||||
title: '请输入数据',
|
||||
title: 'Please Input Data',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
@@ -1700,22 +1766,6 @@ page {
|
||||
}
|
||||
}
|
||||
|
||||
.preview-button-wrapper {
|
||||
padding: 0 20rpx;
|
||||
margin-bottom: 20rpx;
|
||||
|
||||
:deep(.u-button) {
|
||||
width: 100%;
|
||||
height: 80rpx;
|
||||
border-radius: 16rpx;
|
||||
font-size: 28rpx;
|
||||
font-weight: 600;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
}
|
||||
|
||||
.device-list {
|
||||
background: rgba(255, 255, 255, 0.95);
|
||||
margin: 20rpx;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user