examTeamApp/toolJs/Bluetooth.js

206 lines
4.9 KiB
JavaScript

import $store from '@/store'
import $Bluetooth from '@/toolJs/Bluetooth.js'
import $tools from '@/toolJs/tools.js'
import messages from '@/locale/index.js'
let $t = messages[uni.getLocale()]
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($t.linkBluetooth.Nodevicefound)
}
});
}
/**
* 发现外围设备
*/
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 = device.deviceId
$Bluetooth.stopBluetoothDevicesDiscovery()
$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('断开蓝牙连接成功');
$store.commit("changeConnected", false);
}
});
}
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: "/pageTwo/devices/search?id=" + acd_id + '&device=' + device
})
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)
// }
}
// 蓝牙连接失败
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: $t.tips.msgTitle,
content: $t.linkBluetooth.accreditTips,
showCancel: false,
confirmText: $t.tips.btnConfirm,
success(modalSuccess) {
uni.openSetting({
success(settingdata) {
if (settingdata.authSetting["scope.bluetooth"]) {
uni.openBluetoothAdapter({
success: e => {
uni.showToast({
title:$t.linkBluetooth.openBluetoothSuccess,
icon: "none"
})
$store.commit("changeBluetooth", true);
},
fail: err => {
$tools.showModal($t.linkBluetooth.onPhoneBluetoothTips)
console.log('初始化蓝牙失败:' + err.errMsg);
}
});
} else {
uni.showToast({
title: $t.linkBluetooth.openSettingFail,
icon: "none"
})
}
}
})
}
})
} else {
$tools.showModal($t.linkBluetooth.onPhoneBluetoothTips)
}
}