ReedawFoodApp/toolJs/Bluetooth.js

456 lines
11 KiB
JavaScript

import $store from '@/store'
import useBluetooth from '@/toolJs/Bluetooth.js'
import $tools from '@/toolJs/tools.js'
import $data from '@/content.json'
import $model from '@/toolJs/model.js'
// // 蓝牙连接
let deviceId = ""
let deviceName = ""
let devicesList = []
let searchTimer = null
export default {
unitInstruction,
convertToGrams,
unitConversion,
openBluetoothAdapter,
startBluetoothDeviceDiscovery,
onBluetoothDeviceFound,
Bluetoothfilter,
createBLEConnection,
getBLEDeviceServices,
closeBluetoothAdapter,
closeBLEConnection,
onBLEConnectionStateChange,
stopBluetoothDevicesDiscovery
}
// // 初始化蓝牙
function openBluetoothAdapter() {
devicesList = []
deviceId = ""
deviceName = ""
clearTimeout(searchTimer);
uni.openBluetoothAdapter({
success: e => {
$store.commit("changeBluetoothValue", {
bleTipsText: "蓝牙搜索中",
unitList: $data.unitMinus,
isConnectStatus: 0,
deviceId: "",
serviceId: "",
})
startBluetoothDeviceDiscovery()
},
fail: err => {
$store.commit("changeBluetoothValue", {
bleTipsText: "点击重新连接",
unitList: $data.unitMinus,
isConnectStatus: 1,
deviceId: "",
serviceId: "",
})
}
});
}
// 开始搜寻附近的蓝牙外围设备
function startBluetoothDeviceDiscovery() {
uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
interval: 200, //上报设备的间隔
success: res => {
onBluetoothDeviceFound();
searchTimer = setTimeout(() => {
uni.stopBluetoothDevicesDiscovery()
if (!devicesList.length) {
clearTimeout(searchTimer);
$store.commit("changeBluetoothValue", {
bleTipsText: "点击重新连接",
isConnectStatus: 1
});
}
}, 30000); // 30秒超时
},
fail: res => {
$store.commit("changeBluetoothValue", {
bleTipsText: "点击重新连接",
isConnectStatus: 1
})
}
});
}
/**
* 发现外围设备
*/
function onBluetoothDeviceFound() {
uni.onBluetoothDeviceFound(res => {
res.devices.forEach(device => {
if (!device.name && !device.localName) {
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);
device.name = "c00002"
Bluetoothfilter(device)
}
return
}
console.log("device.name", device.name)
if (device.name.toLowerCase().indexOf('pc-c06pro') != -1 ||
device.name.toLowerCase().indexOf('pc-c02pro') != -1 ||
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)) {
clearTimeout(searchTimer);
const bytes = new Uint8Array(device.advertisData);
const macBytes = bytes.slice(6, 12);
device.macAddr = $tools.ab2hex(macBytes, ':').toUpperCase()
stopBluetoothDevicesDiscovery()
Bluetoothfilter(device)
return
}
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 ||
device.name.toLowerCase().indexOf('ailink_') != -1 ||
device.name.toLowerCase().indexOf('pcf01') != -1 ||
device.name.toLowerCase().indexOf('chipsea_ble') != -1 ||
device.name.toLowerCase().indexOf('Yihejia_Lung') != -1) {
clearTimeout(searchTimer);
stopBluetoothDevicesDiscovery()
setTimeout(function() {
Bluetoothfilter(device)
}, 200)
return
}
})
});
}
// 蓝牙过滤
function Bluetoothfilter(device) {
const foundDevices = devicesList
const idx = $tools.inArray(foundDevices, "deviceId", device.deviceId)
if (idx === -1) {
devicesList.push(device);
deviceId = device.deviceId
console.log("deviceName", device.name)
// 广播体脂秤
if (device.name.toLowerCase().indexOf("pcl") != -1) {
uni.navigateTo({
url: "/pageTwo/devices/PCL?deviceId=" + device.deviceId
})
return
}
// 广播思迈德协议,假阻抗,体脂秤
if (device.name.toLowerCase() == "c00002") {
uni.navigateTo({
url: "/pageTwo/devices/PCL22?deviceId=" + device.deviceId
})
return
}
// 广播思迈德协议,假阻抗,体脂秤
if (device.name.toLowerCase().indexOf('da') != -1) {
uni.navigateTo({
url: "/pageTwo/devices/PCL22S?deviceId=" + device.deviceId
})
return
}
// 身高仪
if (device.name.toLowerCase().indexOf("g02") != -1) {
deviceName = 'heightName'
createBLEConnection("FFF0")
return
}
// 跳绳
if (device.name.toLowerCase().indexOf("ypc") != -1) {
deviceName = 'skiping'
createBLEConnection("FFE0")
return
}
// 婴儿秤
if (device.name.toLowerCase().indexOf('chipsea_ble') != -1) {
deviceName = 'bodyName'
createBLEConnection("FFF0")
return
}
// 八电极
if (device.name.toLowerCase().indexOf('ailink_') != -1) {
deviceName = 'L06'
createBLEConnection("FFE0")
return
}
//厨房秤
if (device.name.toLowerCase().indexOf('pc-c06pro') != -1 ||
device.name.toLowerCase().indexOf('pc-c02pro') != -1 ||
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)) {
deviceName = 'CFC'
createBLEConnection("FFF0")
return
}
}
}
// 排查设备
function handleDevType(device) {
$model.getCheckDevice({
mac: device.macAddr,
}).then(res => {
console.log("排查设备:", device, res)
if (res.code == 0) {
createBLEConnection("FFF0")
} else {
devicesList = []
$tools.msg('设备未登记,请联系出售方进行系统认证')
$store.commit("changeBluetoothValue", {
foodType: 1,
isFood: false,
isConnectStatus: 1,
bleTipsText: "点击重新连接",
})
}
})
}
//连接设备
function createBLEConnection(serviceId) {
uni.createBLEConnection({
deviceId: deviceId,
success: res => {
setTimeout(function() {
getBLEDeviceServices(serviceId)
}, 200)
},
fail: res => {
$store.commit("changeBluetoothValue", {
foodType: 1,
isConnectStatus: 1,
bleTipsText: "点击重新连接",
})
}
});
}
/**
* 获取设备的UUID
*/
function getBLEDeviceServices(serviceId) {
let serviceList = [];
uni.getBLEDeviceServices({
deviceId: deviceId,
success: res => {
console.log("获取设备的UUID成功", res, deviceName)
stopBluetoothDevicesDiscovery();
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;
}
}
if (deviceName == 'CFC') { //厨房秤
$store.commit("changeBluetoothValue", {
isFood: true,
})
return
}
if (deviceName == 'skiping') {
uni.navigateTo({
url: "/pageTwo/skiping/skip?deviceId=" + deviceId
})
return
}
if (deviceName == 'L06') {
uni.navigateTo({
url: "/pageTwo/devices/pcL06?deviceId=" + deviceId
})
return
}
if (deviceName == 'bodyName') {
uni.navigateTo({
url: "/pageTwo/devices/B20?deviceId=" + deviceId
})
return
}
if (deviceName == 'heightName') {
uni.navigateTo({
url: "/pageTwo/devices/G02?deviceId=" + deviceId
})
return
}
},
fail: res => {
$store.commit("changeBluetoothValue", {
foodType: 1,
isConnectStatus: 1,
bleTipsText: "点击重新连接",
})
console.log('获取设备的UUID失败:', res)
}
});
}
/**
* 断开蓝牙模块
*/
function closeBluetoothAdapter() {
uni.closeBluetoothAdapter({
success: res => {
clearTimeout(searchTimer);
$store.commit("changeBluetoothValue", {
deviceId: "",
serviceId: "",
foodUnit: "g",
foodNotify: "",
foodWrite: "",
foodType: 1,
foodWeight: 100,
unitList: $data.unitMinus,
isConnectStatus: 1,
bleTipsText: "点击重新连接",
})
console.log('蓝牙模块关闭成功');
}
})
}
/**
* 断开蓝牙连接
*/
function closeBLEConnection() {
uni.closeBLEConnection({
deviceId: deviceId,
success: res => {
console.log('断开蓝牙连接成功');
}
});
}
// 监听蓝牙连接状态
function onBLEConnectionStateChange() {
uni.onBLEConnectionStateChange(function(res) {
$store.commit("changeConnected", res.connected);
})
}
/**
* 停止搜索蓝牙设备
*/
function stopBluetoothDevicesDiscovery() {
uni.stopBluetoothDevicesDiscovery({
success: e => {
console.log("停止搜索蓝牙设备", e)
},
});
}
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
}
function parseUnitMask(hexValue, UNIT_MAP) {
// 转换为数字
const mask = typeof hexValue === 'string' ? parseInt(hexValue, 16) : hexValue;
// 验证输入
if (isNaN(mask) || mask < 0 || mask > 0xFF) {
console.error('无效的位掩码值');
return [];
}
// 存储结果
const usedUnits = [];
// 遍历每一位
for (let i = 0; i < 8; i++) {
// 使用位运算检查第 i 位是否为 1
if (mask & (1 << i)) {
usedUnits.push(UNIT_MAP[i]);
}
}
return usedUnits;
}