2025-05-30 11:14:38 +08:00
|
|
|
import $store from '@/store'
|
2026-03-23 09:50:49 +08:00
|
|
|
import useBluetooth from '@/toolJs/Bluetooth.js'
|
2025-05-30 11:14:38 +08:00
|
|
|
import $tools from '@/toolJs/tools.js'
|
2026-03-23 09:50:49 +08:00
|
|
|
import $data from '@/content.json'
|
2026-03-25 17:27:29 +08:00
|
|
|
import $model from '@/toolJs/model.js'
|
2026-03-23 09:50:49 +08:00
|
|
|
// // 蓝牙连接
|
2026-03-23 16:23:40 +08:00
|
|
|
let deviceId = ""
|
2026-03-25 17:27:29 +08:00
|
|
|
let deviceName = ""
|
2025-05-30 11:14:38 +08:00
|
|
|
let devicesList = []
|
2026-03-23 09:50:49 +08:00
|
|
|
let searchTimer = null
|
2025-05-30 11:14:38 +08:00
|
|
|
export default {
|
2026-03-25 17:27:29 +08:00
|
|
|
unitInstruction,
|
|
|
|
|
convertToGrams,
|
|
|
|
|
unitConversion,
|
2025-05-30 11:14:38 +08:00
|
|
|
openBluetoothAdapter,
|
|
|
|
|
startBluetoothDeviceDiscovery,
|
|
|
|
|
onBluetoothDeviceFound,
|
2026-03-23 09:50:49 +08:00
|
|
|
Bluetoothfilter,
|
|
|
|
|
createBLEConnection,
|
|
|
|
|
getBLEDeviceServices,
|
2025-05-30 11:14:38 +08:00
|
|
|
closeBluetoothAdapter,
|
2026-03-23 09:50:49 +08:00
|
|
|
closeBLEConnection,
|
|
|
|
|
onBLEConnectionStateChange,
|
2025-05-30 11:14:38 +08:00
|
|
|
stopBluetoothDevicesDiscovery
|
|
|
|
|
}
|
2026-03-23 09:50:49 +08:00
|
|
|
// // 初始化蓝牙
|
2025-05-30 11:14:38 +08:00
|
|
|
function openBluetoothAdapter() {
|
2026-03-23 09:50:49 +08:00
|
|
|
devicesList = []
|
2026-03-25 17:27:29 +08:00
|
|
|
deviceId = ""
|
|
|
|
|
deviceName = ""
|
2026-03-23 09:50:49 +08:00
|
|
|
clearTimeout(searchTimer);
|
2025-05-30 11:14:38 +08:00
|
|
|
uni.openBluetoothAdapter({
|
|
|
|
|
success: e => {
|
2026-03-23 09:50:49 +08:00
|
|
|
$store.commit("changeBluetoothValue", {
|
|
|
|
|
bleTipsText: "蓝牙搜索中",
|
2026-03-25 17:27:29 +08:00
|
|
|
unitList: $data.unitMinus,
|
2026-03-23 09:50:49 +08:00
|
|
|
isConnectStatus: 0,
|
2026-03-23 16:23:40 +08:00
|
|
|
deviceId: "",
|
|
|
|
|
serviceId: "",
|
2026-03-23 09:50:49 +08:00
|
|
|
})
|
|
|
|
|
startBluetoothDeviceDiscovery()
|
2025-05-30 11:14:38 +08:00
|
|
|
},
|
2026-03-23 09:50:49 +08:00
|
|
|
fail: err => {
|
|
|
|
|
$store.commit("changeBluetoothValue", {
|
2026-03-27 10:04:26 +08:00
|
|
|
bleTipsText: "点击重新连接",
|
2026-03-25 17:27:29 +08:00
|
|
|
unitList: $data.unitMinus,
|
2026-03-23 09:50:49 +08:00
|
|
|
isConnectStatus: 1,
|
2026-03-23 16:23:40 +08:00
|
|
|
deviceId: "",
|
|
|
|
|
serviceId: "",
|
2026-03-23 09:50:49 +08:00
|
|
|
})
|
2025-05-30 11:14:38 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 开始搜寻附近的蓝牙外围设备
|
|
|
|
|
function startBluetoothDeviceDiscovery() {
|
|
|
|
|
uni.startBluetoothDevicesDiscovery({
|
|
|
|
|
allowDuplicatesKey: true,
|
|
|
|
|
interval: 200, //上报设备的间隔
|
|
|
|
|
success: res => {
|
2026-03-23 09:50:49 +08:00
|
|
|
onBluetoothDeviceFound();
|
|
|
|
|
searchTimer = setTimeout(() => {
|
|
|
|
|
uni.stopBluetoothDevicesDiscovery()
|
|
|
|
|
if (!devicesList.length) {
|
|
|
|
|
clearTimeout(searchTimer);
|
|
|
|
|
$store.commit("changeBluetoothValue", {
|
2026-03-27 10:04:26 +08:00
|
|
|
bleTipsText: "点击重新连接",
|
2026-03-23 09:50:49 +08:00
|
|
|
isConnectStatus: 1
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, 30000); // 30秒超时
|
2025-05-30 11:14:38 +08:00
|
|
|
},
|
|
|
|
|
fail: res => {
|
2026-03-23 09:50:49 +08:00
|
|
|
$store.commit("changeBluetoothValue", {
|
2026-03-27 10:04:26 +08:00
|
|
|
bleTipsText: "点击重新连接",
|
2026-03-23 09:50:49 +08:00
|
|
|
isConnectStatus: 1
|
|
|
|
|
})
|
2025-05-30 11:14:38 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 发现外围设备
|
|
|
|
|
*/
|
2026-03-23 09:50:49 +08:00
|
|
|
|
2025-05-30 11:14:38 +08:00
|
|
|
function onBluetoothDeviceFound() {
|
|
|
|
|
uni.onBluetoothDeviceFound(res => {
|
|
|
|
|
res.devices.forEach(device => {
|
|
|
|
|
if (!device.name && !device.localName) {
|
2026-03-23 09:50:49 +08:00
|
|
|
device.advertisData = device.advertisData ? device.advertisData : ''
|
|
|
|
|
let value = $tools.ab2hex(device.advertisData, "")
|
|
|
|
|
let type = value.substring(0, 2)
|
|
|
|
|
let id = value.substring(12, 16)
|
|
|
|
|
if (type.toLowerCase() == 'c0') {
|
|
|
|
|
clearTimeout(searchTimer);
|
2026-03-25 17:27:29 +08:00
|
|
|
device.name = "c00002"
|
2026-03-23 09:50:49 +08:00
|
|
|
Bluetoothfilter(device)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-03-25 17:27:29 +08:00
|
|
|
console.log("device.name", device.name)
|
2026-03-23 09:50:49 +08:00
|
|
|
if (device.name.toLowerCase().indexOf('pc-c06pro') != -1 ||
|
|
|
|
|
device.name.toLowerCase().indexOf('pc-c02pro') != -1 ||
|
|
|
|
|
device.name.toLowerCase().indexOf('pc-c09pro') != -1 ||
|
2026-03-23 16:23:40 +08:00
|
|
|
device.name.toLowerCase().indexOf('pc-c10pro') != -1 ||
|
|
|
|
|
device.name.toLowerCase().indexOf('pc-c07pro') != -1 ||
|
|
|
|
|
(device.localName && device.localName.toLowerCase().indexOf('pc-c07pro') != -1) ||
|
|
|
|
|
(device.localName && device.localName.toLowerCase().indexOf('pc-c10pro') != -1) ||
|
2026-03-23 09:50:49 +08:00
|
|
|
(device.localName && device.localName.toLowerCase().indexOf('pc-c06pro') != -1) ||
|
|
|
|
|
(device.localName && device.localName.toLowerCase().indexOf('pc-c02pro') != -1) ||
|
|
|
|
|
(device.localName && device.localName.toLowerCase().indexOf('pc-c09pro') != -1)) {
|
|
|
|
|
clearTimeout(searchTimer);
|
2026-03-25 17:27:29 +08:00
|
|
|
const bytes = new Uint8Array(device.advertisData);
|
|
|
|
|
const macBytes = bytes.slice(6, 12);
|
|
|
|
|
device.macAddr = $tools.ab2hex(macBytes, ':').toUpperCase()
|
2026-03-23 09:50:49 +08:00
|
|
|
stopBluetoothDevicesDiscovery()
|
|
|
|
|
Bluetoothfilter(device)
|
2025-05-30 11:14:38 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-03-23 09:50:49 +08:00
|
|
|
if (device.name.toLowerCase().indexOf("g02") != -1 ||
|
|
|
|
|
device.name.toLowerCase().indexOf('ypc') != -1 ||
|
|
|
|
|
device.name.toLowerCase().indexOf('da') != -1 ||
|
|
|
|
|
device.name.toLowerCase().indexOf('pcl') != -1 ||
|
2026-03-27 10:04:26 +08:00
|
|
|
device.name.toLowerCase().indexOf('ailink_') != -1 ||
|
2026-03-23 09:50:49 +08:00
|
|
|
device.name.toLowerCase().indexOf('pcf01') != -1 ||
|
2026-03-25 17:27:29 +08:00
|
|
|
device.name.toLowerCase().indexOf('chipsea_ble') != -1 ||
|
2026-03-23 09:50:49 +08:00
|
|
|
device.name.toLowerCase().indexOf('Yihejia_Lung') != -1) {
|
|
|
|
|
clearTimeout(searchTimer);
|
|
|
|
|
stopBluetoothDevicesDiscovery()
|
2026-03-27 10:04:26 +08:00
|
|
|
setTimeout(function() {
|
|
|
|
|
Bluetoothfilter(device)
|
|
|
|
|
}, 200)
|
2025-05-30 11:14:38 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// 蓝牙过滤
|
2026-03-23 09:50:49 +08:00
|
|
|
function Bluetoothfilter(device) {
|
2025-05-30 11:14:38 +08:00
|
|
|
const foundDevices = devicesList
|
|
|
|
|
const idx = $tools.inArray(foundDevices, "deviceId", device.deviceId)
|
|
|
|
|
if (idx === -1) {
|
|
|
|
|
devicesList.push(device);
|
2026-03-23 16:23:40 +08:00
|
|
|
deviceId = device.deviceId
|
2026-03-27 10:04:26 +08:00
|
|
|
console.log("deviceName", device.name)
|
|
|
|
|
// 广播体脂秤
|
2026-03-23 09:50:49 +08:00
|
|
|
if (device.name.toLowerCase().indexOf("pcl") != -1) {
|
|
|
|
|
uni.navigateTo({
|
|
|
|
|
url: "/pageTwo/devices/PCL?deviceId=" + device.deviceId
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-03-27 10:04:26 +08:00
|
|
|
// 广播思迈德协议,假阻抗,体脂秤
|
2026-03-23 09:50:49 +08:00
|
|
|
if (device.name.toLowerCase() == "c00002") {
|
|
|
|
|
uni.navigateTo({
|
|
|
|
|
url: "/pageTwo/devices/PCL22?deviceId=" + device.deviceId
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-03-27 10:04:26 +08:00
|
|
|
// 广播思迈德协议,假阻抗,体脂秤
|
2026-03-30 15:02:56 +08:00
|
|
|
if (device.name.toLowerCase().indexOf('da') != -1) {
|
2026-03-23 09:50:49 +08:00
|
|
|
uni.navigateTo({
|
|
|
|
|
url: "/pageTwo/devices/PCL22S?deviceId=" + device.deviceId
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 身高仪
|
2026-03-27 10:04:26 +08:00
|
|
|
if (device.name.toLowerCase().indexOf("g02") != -1) {
|
|
|
|
|
deviceName = 'heightName'
|
2026-03-25 17:27:29 +08:00
|
|
|
createBLEConnection("FFF0")
|
2026-03-23 09:50:49 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 跳绳
|
2026-03-27 10:04:26 +08:00
|
|
|
if (device.name.toLowerCase().indexOf("ypc") != -1) {
|
|
|
|
|
deviceName = 'skiping'
|
2026-03-25 17:27:29 +08:00
|
|
|
createBLEConnection("FFE0")
|
2026-03-23 09:50:49 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 婴儿秤
|
2026-03-27 10:04:26 +08:00
|
|
|
if (device.name.toLowerCase().indexOf('chipsea_ble') != -1) {
|
|
|
|
|
deviceName = 'bodyName'
|
2026-03-25 17:27:29 +08:00
|
|
|
createBLEConnection("FFF0")
|
2026-03-23 09:50:49 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-03-25 17:27:29 +08:00
|
|
|
// 八电极
|
2026-03-27 10:04:26 +08:00
|
|
|
if (device.name.toLowerCase().indexOf('ailink_') != -1) {
|
|
|
|
|
deviceName = 'L06'
|
2026-03-25 17:27:29 +08:00
|
|
|
createBLEConnection("FFE0")
|
2026-03-23 09:50:49 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
//厨房秤
|
|
|
|
|
if (device.name.toLowerCase().indexOf('pc-c06pro') != -1 ||
|
|
|
|
|
device.name.toLowerCase().indexOf('pc-c02pro') != -1 ||
|
2026-03-23 16:23:40 +08:00
|
|
|
device.name.toLowerCase().indexOf('pc-c09pro') != -1 ||
|
|
|
|
|
device.name.toLowerCase().indexOf('pc-c10pro') != -1 ||
|
|
|
|
|
device.name.toLowerCase().indexOf('pc-c07pro') != -1 ||
|
|
|
|
|
(device.localName && device.localName.toLowerCase().indexOf('pc-c07pro') != -1) ||
|
|
|
|
|
(device.localName && device.localName.toLowerCase().indexOf('pc-c10pro') != -1) ||
|
|
|
|
|
(device.localName && device.localName.toLowerCase().indexOf('pc-c06pro') != -1) ||
|
|
|
|
|
(device.localName && device.localName.toLowerCase().indexOf('pc-c02pro') != -1) ||
|
|
|
|
|
(device.localName && device.localName.toLowerCase().indexOf('pc-c09pro') != -1)) {
|
2026-03-25 17:27:29 +08:00
|
|
|
deviceName = 'CFC'
|
|
|
|
|
createBLEConnection("FFF0")
|
2026-03-23 09:50:49 +08:00
|
|
|
return
|
|
|
|
|
}
|
2025-05-30 11:14:38 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-23 09:50:49 +08:00
|
|
|
// 排查设备
|
|
|
|
|
function handleDevType(device) {
|
|
|
|
|
$model.getCheckDevice({
|
|
|
|
|
mac: device.macAddr,
|
|
|
|
|
}).then(res => {
|
|
|
|
|
console.log("排查设备:", device, res)
|
|
|
|
|
if (res.code == 0) {
|
2026-03-25 17:27:29 +08:00
|
|
|
createBLEConnection("FFF0")
|
2026-03-23 09:50:49 +08:00
|
|
|
} else {
|
|
|
|
|
devicesList = []
|
|
|
|
|
$tools.msg('设备未登记,请联系出售方进行系统认证')
|
|
|
|
|
$store.commit("changeBluetoothValue", {
|
2026-03-25 17:27:29 +08:00
|
|
|
foodType: 1,
|
|
|
|
|
isFood: false,
|
2026-03-23 09:50:49 +08:00
|
|
|
isConnectStatus: 1,
|
2026-03-27 10:04:26 +08:00
|
|
|
bleTipsText: "点击重新连接",
|
2026-03-23 09:50:49 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
//连接设备
|
2026-03-23 16:23:40 +08:00
|
|
|
function createBLEConnection(serviceId) {
|
2026-03-23 09:50:49 +08:00
|
|
|
uni.createBLEConnection({
|
2026-03-23 16:23:40 +08:00
|
|
|
deviceId: deviceId,
|
2026-03-23 09:50:49 +08:00
|
|
|
success: res => {
|
|
|
|
|
setTimeout(function() {
|
2026-03-23 16:23:40 +08:00
|
|
|
getBLEDeviceServices(serviceId)
|
2026-03-23 09:50:49 +08:00
|
|
|
}, 200)
|
|
|
|
|
},
|
|
|
|
|
fail: res => {
|
|
|
|
|
$store.commit("changeBluetoothValue", {
|
2026-03-25 17:27:29 +08:00
|
|
|
foodType: 1,
|
2026-03-23 09:50:49 +08:00
|
|
|
isConnectStatus: 1,
|
2026-03-27 10:04:26 +08:00
|
|
|
bleTipsText: "点击重新连接",
|
2026-03-23 09:50:49 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-05-30 11:14:38 +08:00
|
|
|
/**
|
2026-03-23 09:50:49 +08:00
|
|
|
* 获取设备的UUID
|
2025-05-30 11:14:38 +08:00
|
|
|
*/
|
2026-03-23 16:23:40 +08:00
|
|
|
function getBLEDeviceServices(serviceId) {
|
2026-03-23 09:50:49 +08:00
|
|
|
let serviceList = [];
|
|
|
|
|
uni.getBLEDeviceServices({
|
2026-03-23 16:23:40 +08:00
|
|
|
deviceId: deviceId,
|
2026-03-23 09:50:49 +08:00
|
|
|
success: res => {
|
2026-03-27 10:04:26 +08:00
|
|
|
console.log("获取设备的UUID成功", res, deviceName)
|
2026-03-23 09:50:49 +08:00
|
|
|
stopBluetoothDevicesDiscovery();
|
2026-03-27 10:04:26 +08:00
|
|
|
serviceList = res.services;
|
|
|
|
|
for (let i = 0; i < serviceList.length; i++) {
|
|
|
|
|
let service = serviceList[i];
|
|
|
|
|
if (service.uuid.indexOf(serviceId) != -1) {
|
|
|
|
|
$store.commit("changeBluetoothValue", {
|
|
|
|
|
foodType: 1,
|
|
|
|
|
isConnectStatus: 0,
|
|
|
|
|
deviceId: deviceId,
|
|
|
|
|
serviceId: service.uuid,
|
|
|
|
|
bleTipsText: "蓝牙连接中",
|
|
|
|
|
})
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-25 17:27:29 +08:00
|
|
|
if (deviceName == 'CFC') { //厨房秤
|
|
|
|
|
$store.commit("changeBluetoothValue", {
|
|
|
|
|
isFood: true,
|
|
|
|
|
})
|
2026-03-27 10:04:26 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (deviceName == 'skiping') {
|
2026-03-25 17:27:29 +08:00
|
|
|
uni.navigateTo({
|
|
|
|
|
url: "/pageTwo/skiping/skip?deviceId=" + deviceId
|
|
|
|
|
})
|
2026-03-27 10:04:26 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (deviceName == 'L06') {
|
2026-03-25 17:27:29 +08:00
|
|
|
uni.navigateTo({
|
|
|
|
|
url: "/pageTwo/devices/pcL06?deviceId=" + deviceId
|
|
|
|
|
})
|
2026-03-27 10:04:26 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (deviceName == 'bodyName') {
|
2026-03-25 17:27:29 +08:00
|
|
|
uni.navigateTo({
|
|
|
|
|
url: "/pageTwo/devices/B20?deviceId=" + deviceId
|
|
|
|
|
})
|
2026-03-27 10:04:26 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (deviceName == 'heightName') {
|
2026-03-25 17:27:29 +08:00
|
|
|
uni.navigateTo({
|
|
|
|
|
url: "/pageTwo/devices/G02?deviceId=" + deviceId
|
|
|
|
|
})
|
2026-03-27 10:04:26 +08:00
|
|
|
return
|
2026-03-23 09:50:49 +08:00
|
|
|
}
|
2025-05-30 11:14:38 +08:00
|
|
|
},
|
2026-03-23 09:50:49 +08:00
|
|
|
fail: res => {
|
|
|
|
|
$store.commit("changeBluetoothValue", {
|
2026-03-25 17:27:29 +08:00
|
|
|
foodType: 1,
|
2026-03-23 09:50:49 +08:00
|
|
|
isConnectStatus: 1,
|
2026-03-27 10:04:26 +08:00
|
|
|
bleTipsText: "点击重新连接",
|
2026-03-23 09:50:49 +08:00
|
|
|
})
|
|
|
|
|
console.log('获取设备的UUID失败:', res)
|
|
|
|
|
}
|
2025-05-30 11:14:38 +08:00
|
|
|
});
|
|
|
|
|
}
|
2026-03-23 09:50:49 +08:00
|
|
|
|
2025-05-30 11:14:38 +08:00
|
|
|
/**
|
|
|
|
|
* 断开蓝牙模块
|
|
|
|
|
*/
|
|
|
|
|
function closeBluetoothAdapter() {
|
|
|
|
|
uni.closeBluetoothAdapter({
|
|
|
|
|
success: res => {
|
2026-03-23 09:50:49 +08:00
|
|
|
clearTimeout(searchTimer);
|
2026-03-30 15:02:56 +08:00
|
|
|
$store.commit("changeBluetoothValue", {
|
|
|
|
|
deviceId: "",
|
|
|
|
|
serviceId: "",
|
|
|
|
|
foodUnit: "g",
|
|
|
|
|
foodNotify: "",
|
|
|
|
|
foodWrite: "",
|
|
|
|
|
foodType: 1,
|
|
|
|
|
foodWeight: 100,
|
|
|
|
|
unitList: $data.unitMinus,
|
|
|
|
|
isConnectStatus: 1,
|
|
|
|
|
bleTipsText: "点击重新连接",
|
|
|
|
|
})
|
2025-05-30 11:14:38 +08:00
|
|
|
console.log('蓝牙模块关闭成功');
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 断开蓝牙连接
|
|
|
|
|
*/
|
2026-03-25 17:27:29 +08:00
|
|
|
function closeBLEConnection() {
|
2025-05-30 11:14:38 +08:00
|
|
|
uni.closeBLEConnection({
|
|
|
|
|
deviceId: deviceId,
|
|
|
|
|
success: res => {
|
|
|
|
|
console.log('断开蓝牙连接成功');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-03-23 09:50:49 +08:00
|
|
|
// 监听蓝牙连接状态
|
|
|
|
|
function onBLEConnectionStateChange() {
|
|
|
|
|
uni.onBLEConnectionStateChange(function(res) {
|
2026-03-30 15:02:56 +08:00
|
|
|
$store.commit("changeConnected", res.connected);
|
2026-03-23 09:50:49 +08:00
|
|
|
})
|
2025-05-30 11:14:38 +08:00
|
|
|
}
|
2026-03-23 09:50:49 +08:00
|
|
|
/**
|
|
|
|
|
* 停止搜索蓝牙设备
|
|
|
|
|
*/
|
|
|
|
|
function stopBluetoothDevicesDiscovery() {
|
|
|
|
|
uni.stopBluetoothDevicesDiscovery({
|
2025-05-30 11:14:38 +08:00
|
|
|
success: e => {
|
2026-03-23 09:50:49 +08:00
|
|
|
console.log("停止搜索蓝牙设备", e)
|
2025-05-30 11:14:38 +08:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-03-23 09:50:49 +08:00
|
|
|
|
2026-03-25 17:27:29 +08:00
|
|
|
function unitConversion(unit) {
|
|
|
|
|
if (unit == 'kg') {
|
|
|
|
|
return '千克'
|
|
|
|
|
} else if (unit == '斤') {
|
|
|
|
|
return '斤'
|
|
|
|
|
} else if (unit == 'stlb') {
|
|
|
|
|
return 'stlb'
|
|
|
|
|
} else if (unit == 'lb') {
|
|
|
|
|
return '磅'
|
|
|
|
|
} else if (unit == 'g') {
|
|
|
|
|
return '克'
|
|
|
|
|
} else if (unit == 'ml') {
|
|
|
|
|
return '毫升'
|
|
|
|
|
} else if (unit == 'Waterml') {
|
|
|
|
|
return 'Waterml'
|
|
|
|
|
} else if (unit == 'milkml') {
|
|
|
|
|
return 'milkml'
|
|
|
|
|
} else if (unit == 'oz') {
|
|
|
|
|
return '盎司'
|
|
|
|
|
} else if (unit == 'floz') {
|
|
|
|
|
return 'floz'
|
|
|
|
|
} else if (unit == 'lboz') {
|
|
|
|
|
return 'lboz'
|
|
|
|
|
}
|
|
|
|
|
return unit
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function convertToGrams(value, fromUnit) {
|
|
|
|
|
const conversionFactors = {
|
|
|
|
|
'lb': 453.59237, // 1磅 = 453.59237克
|
|
|
|
|
'oz': 28.349523125, // 1盎司 = 28.349523125克
|
|
|
|
|
'kg': 1000, // 1公斤 = 1000克
|
|
|
|
|
'g': 1,
|
|
|
|
|
'ml': 1,
|
|
|
|
|
'斤': 500,
|
|
|
|
|
'Waterml': 1,
|
|
|
|
|
'milkml': 1.03
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!conversionFactors.hasOwnProperty(fromUnit)) {
|
|
|
|
|
return ''
|
|
|
|
|
}
|
|
|
|
|
return value * conversionFactors[fromUnit];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function unitInstruction(unit) {
|
|
|
|
|
if (unit == 'kg') {
|
|
|
|
|
return 0x00
|
|
|
|
|
} else if (unit == '斤') {
|
|
|
|
|
return 0x01
|
|
|
|
|
} else if (unit == 'st:lb') {
|
|
|
|
|
return 0x02
|
|
|
|
|
} else if (unit == 'lb') {
|
|
|
|
|
return 0x03
|
|
|
|
|
} else if (unit == 'g') {
|
|
|
|
|
return 0x04
|
|
|
|
|
} else if (unit == 'ml') {
|
|
|
|
|
return 0x05
|
|
|
|
|
} else if (unit == 'Waterml') {
|
|
|
|
|
return 0x06
|
|
|
|
|
} else if (unit == 'milkml') {
|
|
|
|
|
return 0x07
|
|
|
|
|
} else if (unit == 'oz') {
|
|
|
|
|
return 0x08
|
|
|
|
|
} else if (unit == 'floz') {
|
|
|
|
|
return 0x09
|
|
|
|
|
} else if (unit == 'lboz') {
|
|
|
|
|
return 0x0A
|
|
|
|
|
}
|
|
|
|
|
return unit
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-23 09:50:49 +08:00
|
|
|
function parseUnitMask(hexValue, UNIT_MAP) {
|
|
|
|
|
// 转换为数字
|
|
|
|
|
const mask = typeof hexValue === 'string' ? parseInt(hexValue, 16) : hexValue;
|
|
|
|
|
|
|
|
|
|
// 验证输入
|
|
|
|
|
if (isNaN(mask) || mask < 0 || mask > 0xFF) {
|
|
|
|
|
console.error('无效的位掩码值');
|
|
|
|
|
return [];
|
2025-05-30 11:14:38 +08:00
|
|
|
}
|
2026-03-23 09:50:49 +08:00
|
|
|
|
|
|
|
|
// 存储结果
|
|
|
|
|
const usedUnits = [];
|
|
|
|
|
|
|
|
|
|
// 遍历每一位
|
|
|
|
|
for (let i = 0; i < 8; i++) {
|
|
|
|
|
// 使用位运算检查第 i 位是否为 1
|
|
|
|
|
if (mask & (1 << i)) {
|
|
|
|
|
usedUnits.push(UNIT_MAP[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return usedUnits;
|
2025-05-30 11:14:38 +08:00
|
|
|
}
|