2024-07-22 14:13:19 +08:00
|
|
|
import $store from '@/store'
|
2024-08-22 09:48:00 +08:00
|
|
|
import $Bluetooth from '@/toolJs/Bluetooth.js'
|
|
|
|
|
import $tools from '@/toolJs/tools.js'
|
2025-04-26 13:35:30 +08:00
|
|
|
import messages from '@/locale/index.js'
|
2025-04-30 16:47:18 +08:00
|
|
|
// let Language = $store.state.setLocale
|
|
|
|
|
// let $t = messages[Language]
|
2024-07-22 14:13:19 +08:00
|
|
|
let myTime;
|
|
|
|
|
let devicesList = []
|
|
|
|
|
export default {
|
|
|
|
|
str2Num,
|
|
|
|
|
handleDevicesMac,
|
|
|
|
|
openBluetoothAdapter,
|
|
|
|
|
startBluetoothDeviceDiscovery,
|
|
|
|
|
onBluetoothDeviceFound,
|
|
|
|
|
getBluetoothAdapter,
|
|
|
|
|
handleDevice,
|
|
|
|
|
onBLEConnectionStateChange,
|
|
|
|
|
closeBLEConnection,
|
|
|
|
|
closeBluetoothAdapter,
|
|
|
|
|
stopBluetoothDevicesDiscovery
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 蓝牙连接
|
|
|
|
|
function openBluetoothAdapter() {
|
|
|
|
|
uni.openBluetoothAdapter({
|
|
|
|
|
success: e => {
|
|
|
|
|
$store.commit("changeBluetooth", true)
|
|
|
|
|
$Bluetooth.startBluetoothDeviceDiscovery()
|
|
|
|
|
console.log('初始化蓝牙成功:' + e.errMsg);
|
|
|
|
|
},
|
|
|
|
|
fail: e => {
|
|
|
|
|
$Bluetooth.getBluetoothAdapter(e)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 开始搜寻附近的蓝牙外围设备
|
|
|
|
|
function startBluetoothDeviceDiscovery() {
|
2025-04-30 16:47:18 +08:00
|
|
|
let Language = $store.state.setLocale
|
|
|
|
|
let $t = messages[Language]
|
2024-07-22 14:13:19 +08:00
|
|
|
devicesList = []
|
|
|
|
|
uni.startBluetoothDevicesDiscovery({
|
|
|
|
|
allowDuplicatesKey: true,
|
|
|
|
|
interval: 200, //上报设备的间隔
|
|
|
|
|
services: [],
|
|
|
|
|
success: res => {
|
|
|
|
|
$Bluetooth.onBluetoothDeviceFound();
|
|
|
|
|
},
|
|
|
|
|
fail: res => {
|
2025-04-29 17:29:44 +08:00
|
|
|
$tools.msg($t.linkBluetooth.Nodevicefound)
|
2024-07-22 14:13:19 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发现外围设备
|
|
|
|
|
*/
|
|
|
|
|
function onBluetoothDeviceFound() {
|
|
|
|
|
uni.onBluetoothDeviceFound(res => {
|
|
|
|
|
console.log('开始监听寻找到新设备的事件', res);
|
|
|
|
|
res.devices.forEach(device => {
|
|
|
|
|
if (!device.name && !device.localName) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (device.name.indexOf('YPC') != -1) {
|
|
|
|
|
let buff = device.name.slice(7, 19)
|
|
|
|
|
device.macAddr = $Bluetooth.str2Num(buff)
|
2024-08-30 18:03:10 +08:00
|
|
|
device.deviceId = device.deviceId
|
2024-11-12 15:38:28 +08:00
|
|
|
$Bluetooth.stopBluetoothDevicesDiscovery()
|
2024-07-22 14:13:19 +08:00
|
|
|
$Bluetooth.handleDevice(device)
|
|
|
|
|
console.log("ypc", device)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// 蓝牙过滤
|
|
|
|
|
function handleDevice(device) {
|
|
|
|
|
const foundDevices = devicesList
|
|
|
|
|
const idx = $tools.inArray(foundDevices, "deviceId", device.deviceId)
|
|
|
|
|
console.log("ind", idx, devicesList, device)
|
|
|
|
|
if (idx === -1) {
|
|
|
|
|
devicesList.push(device);
|
|
|
|
|
$store.commit("changedevicesList", devicesList);
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 停止搜索蓝牙设备
|
|
|
|
|
*/
|
|
|
|
|
function stopBluetoothDevicesDiscovery() {
|
|
|
|
|
uni.stopBluetoothDevicesDiscovery({
|
|
|
|
|
success: e => {
|
|
|
|
|
console.log("停止搜索蓝牙设备", e)
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// 监听蓝牙连接状态
|
|
|
|
|
function onBLEConnectionStateChange() {
|
|
|
|
|
uni.onBLEConnectionStateChange(function(res) {
|
|
|
|
|
console.log("蓝牙连接状态", JSON.stringify(res));
|
|
|
|
|
if (!res.connected) {
|
|
|
|
|
$Bluetooth.closeBLEConnection()
|
|
|
|
|
$Bluetooth.closeBluetoothAdapter()
|
|
|
|
|
}
|
|
|
|
|
$store.commit("changeConnected", res.connected);
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 断开蓝牙模块
|
|
|
|
|
*/
|
|
|
|
|
function closeBluetoothAdapter() {
|
|
|
|
|
uni.closeBluetoothAdapter({
|
|
|
|
|
success: res => {
|
|
|
|
|
console.log('蓝牙模块关闭成功');
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 断开蓝牙连接
|
|
|
|
|
*/
|
|
|
|
|
function closeBLEConnection(deviceId) {
|
|
|
|
|
console.log("deviceId", deviceId)
|
|
|
|
|
uni.closeBLEConnection({
|
|
|
|
|
deviceId: deviceId,
|
|
|
|
|
success: res => {
|
|
|
|
|
console.log('断开蓝牙连接成功');
|
2024-11-14 14:07:14 +08:00
|
|
|
$store.commit("changeConnected", false);
|
2024-07-22 14:13:19 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function str2Num(str) {
|
|
|
|
|
var result = "";
|
|
|
|
|
for (let i = 0; i < str.length - 2; i++) {
|
|
|
|
|
result += str[i];
|
|
|
|
|
if (i % 2 === 1) result += ':';
|
|
|
|
|
}
|
|
|
|
|
return result + str.slice(-2)
|
|
|
|
|
}
|
|
|
|
|
// 蓝牙连接
|
|
|
|
|
function handleDevicesMac(device, acd_id) {
|
|
|
|
|
console.log("卡片设备", device, acd_id)
|
2024-11-12 15:38:28 +08:00
|
|
|
// if (device == 'true' || device || device == true) {
|
|
|
|
|
uni.openBluetoothAdapter({
|
|
|
|
|
success: e => {
|
|
|
|
|
$store.commit("changeBluetooth", true);
|
|
|
|
|
uni.navigateTo({
|
|
|
|
|
url: "/pageTwo/devices/search?id=" + acd_id + '&device=' + device
|
2024-07-22 14:13:19 +08:00
|
|
|
})
|
2024-11-12 15:38:28 +08:00
|
|
|
console.log('初始化蓝牙成功:' + e.errMsg);
|
|
|
|
|
},
|
|
|
|
|
fail: err => {
|
|
|
|
|
console.log('初始化蓝牙失败:' + err.errMsg);
|
|
|
|
|
return $Bluetooth.getBluetoothAdapter(err)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// } else {
|
|
|
|
|
// $tools.msg("请先添加设备!")
|
|
|
|
|
// setTimeout(function() {
|
|
|
|
|
// uni.navigateTo({
|
|
|
|
|
// url: "/pageTwo/business/business"
|
|
|
|
|
// })
|
|
|
|
|
// }, 500)
|
|
|
|
|
// }
|
2024-07-22 14:13:19 +08:00
|
|
|
}
|
|
|
|
|
// 蓝牙连接失败
|
|
|
|
|
function getBluetoothAdapter(err) {
|
2025-04-30 16:47:18 +08:00
|
|
|
let Language = $store.state.setLocale
|
|
|
|
|
let $t = messages[Language]
|
2024-07-22 14:13:19 +08:00
|
|
|
if (err.errMsg == "openBluetoothAdapter:fail auth denied" || err.errMsg ===
|
|
|
|
|
"openBluetoothAdapter:fail auth deny" ||
|
|
|
|
|
err.errMsg === "openBluetoothAdapter:fail authorize no response"
|
|
|
|
|
) {
|
|
|
|
|
uni.showModal({
|
2025-04-26 13:35:30 +08:00
|
|
|
title: $t.tips.msgTitle,
|
|
|
|
|
content: $t.linkBluetooth.accreditTips,
|
2024-07-22 14:13:19 +08:00
|
|
|
showCancel: false,
|
2025-04-26 13:35:30 +08:00
|
|
|
confirmText: $t.tips.btnConfirm,
|
2024-07-22 14:13:19 +08:00
|
|
|
success(modalSuccess) {
|
|
|
|
|
uni.openSetting({
|
|
|
|
|
success(settingdata) {
|
|
|
|
|
if (settingdata.authSetting["scope.bluetooth"]) {
|
|
|
|
|
uni.openBluetoothAdapter({
|
|
|
|
|
success: e => {
|
|
|
|
|
uni.showToast({
|
2025-04-30 16:47:18 +08:00
|
|
|
title: $t.linkBluetooth
|
|
|
|
|
.openBluetoothSuccess,
|
2024-07-22 14:13:19 +08:00
|
|
|
icon: "none"
|
|
|
|
|
})
|
|
|
|
|
$store.commit("changeBluetooth", true);
|
|
|
|
|
},
|
|
|
|
|
fail: err => {
|
2025-04-26 13:35:30 +08:00
|
|
|
$tools.showModal($t.linkBluetooth.onPhoneBluetoothTips)
|
2024-07-22 14:13:19 +08:00
|
|
|
console.log('初始化蓝牙失败:' + err.errMsg);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
uni.showToast({
|
2025-04-26 13:35:30 +08:00
|
|
|
title: $t.linkBluetooth.openSettingFail,
|
2024-07-22 14:13:19 +08:00
|
|
|
icon: "none"
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
} else {
|
2025-04-26 13:35:30 +08:00
|
|
|
$tools.showModal($t.linkBluetooth.onPhoneBluetoothTips)
|
2024-07-22 14:13:19 +08:00
|
|
|
}
|
|
|
|
|
}
|