examTeamApp/toolJs/Bluetooth.js

201 lines
4.8 KiB
JavaScript
Raw Normal View History

2024-07-22 14:13:19 +08:00
import $store from '@/store'
import $Bluetooth from '@/toolJs/Bluetooth.js'
import $tools from '@/toolJs/tools.js'
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() {
devicesList = []
uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
interval: 200, //上报设备的间隔
services: [],
success: res => {
$Bluetooth.onBluetoothDeviceFound();
},
fail: res => {
$tools.msg("请确定设备是开机状态、手机蓝牙权限已打开!")
}
});
}
/**
* 发现外围设备
*/
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)
device.deviceId = $Bluetooth.str2Num(buff)
$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('断开蓝牙连接成功');
}
});
}
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)
if (device == 'true' || device || device == true) {
uni.openBluetoothAdapter({
success: e => {
$store.commit("changeBluetooth", true);
uni.navigateTo({
url: "/pages/devices/search?id=" + acd_id+ '&device=' + device
2024-07-22 14:13:19 +08:00
})
console.log('初始化蓝牙成功:' + e.errMsg);
},
fail: err => {
console.log('初始化蓝牙失败:' + err.errMsg);
return $Bluetooth.getBluetoothAdapter(err)
}
});
} else {
$tools.msg("请先添加设备!")
setTimeout(function() {
uni.switchTab({
url: "/pages/business/business"
})
}, 500)
}
}
// 蓝牙连接失败
function getBluetoothAdapter(err) {
if (err.errMsg == "openBluetoothAdapter:fail auth denied" || err.errMsg ===
"openBluetoothAdapter:fail auth deny" ||
err.errMsg === "openBluetoothAdapter:fail authorize no response"
) {
uni.showModal({
title: "提示",
content: "需要您授权使用手机蓝牙",
showCancel: false,
success(modalSuccess) {
uni.openSetting({
success(settingdata) {
if (settingdata.authSetting["scope.bluetooth"]) {
uni.openBluetoothAdapter({
success: e => {
uni.showToast({
title: "获取权限成功,请继续去测量",
icon: "none"
})
$store.commit("changeBluetooth", true);
},
fail: err => {
$tools.showModal("手机蓝牙未打开")
console.log('初始化蓝牙失败:' + err.errMsg);
}
});
} else {
uni.showToast({
title: "获取权限失败,将无法使用手机蓝牙进行测量",
icon: "none"
})
}
}
})
}
})
} else {
$tools.showModal("手机蓝牙未打开")
}
}