import bluetoothTool from "@/plugins/BluetoothTool.js"; import permission from "@/plugins/permission.js"; const statusMap = [ { code: 0, msg: "蓝牙设备已打开", }, { code: 1, msg: "蓝牙设备未打开", }, { code: 2, msg: "当前设备不支持蓝牙", }, ]; export default { namespaced: true, // 储存数据 state: { discoveredDevices: [], // 已发现的蓝牙设备 connectedBleDevice: null, // 当前连接的蓝牙设备 connectedDeviceId: "", // 已连接的蓝牙设备ID }, // 修改数据 mutations: { // 设置已发现的蓝牙设备 setDiscoveredDevices(state, data) { console.log("setDiscoveredDevices------", data); state.discoveredDevices = data; }, // 设置已发现的蓝牙设备 addDiscoveredDevice(state, device) { console.log("addDiscoveredDevice------", device); state.discoveredDevices.push(device); }, // 已连接蓝牙设备 setConnectedBleDevice(state, data) { console.log(111111, data); state.connectedBleDevice = { ...data }; }, // 设置已连接蓝牙设备ID setConnectedDeviceId(state, data) { state.connectedDeviceId = data; }, }, actions: { // 检查蓝牙设备打开状态 checkBleStatus({ commit }) { return new Promise((resolve) => { // 检查蓝牙是否打开 if (bluetoothTool.isSupportBluetooth()) { console.log("蓝牙设备支持"); if (bluetoothTool.getBluetoothStatus()) { console.log("蓝牙设备已打开"); resolve({ code: statusMap[0].code, msg: statusMap[0].msg, }); } else { console.log("蓝牙设备未打开"); resolve({ code: statusMap[1].code, msg: statusMap[1].msg, }); } } else { resolve({ code: statusMap[2].code, msg: statusMap[2].msg, }); } }); }, // 初始蓝牙 initBle({ commit, state }) { console.log("state======", state); // 检查蓝牙权限 permission .androidPermissionCheck("bluetooth") .then(() => { bluetoothTool.init({ // 监听蓝牙状态变化 listenBTStatusCallback: (state) => { if (state == "STATE_ON") { console.log("蓝牙已打开:", state); // 可以在这里添加蓝牙打开后的初始化逻辑 } else if (state == "STATE_OFF") { console.log("蓝牙已关闭:", state); // 可以在这里添加蓝牙关闭后的清理逻辑 } }, // 发现新设备回调 discoveryDeviceCallback: function (device) { try { // 基本验证 if (!device || !device.name) { console.log("无效设备:", device); return; } // 设备名称过滤 const deviceName = device.name.toUpperCase(); const isBleDevice = deviceName.endsWith("_BLE") || deviceName.endsWith("-LE") || deviceName.endsWith("-BLE"); // 如果是BLE设备,直接返回 if (isBleDevice) { console.log("发现BLE设备:", device); return; } // 设备去重 const isDuplicate = state.discoveredDevices.find( (item) => item.address === device.address ); if (isDuplicate) { console.log("重复设备:", device); return; } // 添加设备到列表 console.log("发现新设备:", device); commit("addDiscoveredDevice", device); } catch (error) { console.error("处理设备发现回调出错:", error); } }, // 搜索完成回调 discoveryFinishedCallback: function () { console.log("蓝牙设备搜索完成"); // 可以在这里添加搜索完成后的处理逻辑 }, // 读取数据回调 readDataCallback: function (dataByteArr) { try { if (!dataByteArr || dataByteArr.length === 0) { console.log("无数据读取"); return; } console.log("读取到数据:", dataByteArr); // 这里可以添加数据处理逻辑 } catch (error) { console.error("处理数据读取回调出错:", error); } }, // 连接异常回调 connExceptionCallback: function (e) { console.error("蓝牙连接异常:", e); // 可以在这里添加连接异常的处理逻辑 uni.showToast({ icon: "none", title: "蓝牙连接异常,请重试", duration: 2000, }); }, }); }) .catch((error) => { console.error("蓝牙权限检查失败:", error); uni.showToast({ icon: "none", title: "请授予蓝牙权限", duration: 2000, }); }); }, // 搜索蓝牙设备 async discovery({ commit }) { // 使用openBluetoothAdapter 接口,免去主动申请权限的麻烦 uni.openBluetoothAdapter({ success: async (res) => { try { await permission.androidPermissionCheck("bluetooth"); commit("setDiscoveredDevices", []); bluetoothTool.discoveryNewDevice(); console.log("openBluetoothAdapter=====", res); } catch (err) { bluetoothTool.shortToast(`授权失败:!${err}`); console.log("授权失败:", err); } }, }); }, // 连接蓝牙 connectBT({ commit }, device) { uni.showLoading({ title: "连接中", }); return new Promise((resolve) => { bluetoothTool.connDevice(device.address, (result) => { uni.hideLoading(); if (result) { console.log(result); bluetoothTool.cancelDiscovery(); commit("setConnectedBleDevice", device); commit("setConnectedDeviceId", device.address); uni.showToast({ icon: "none", title: "连接成功", }); resolve(device); } else { uni.showToast({ icon: "none", title: "连接失败", }); resolve(null); } }); }); }, // 断开连接 closeBluetooth({ commit, state }) { if (state.connectedDeviceId != "") { bluetoothTool.closeBtSocket(); commit("setConnectedBleDevice", null); commit("setConnectedDeviceId", ""); } }, cancelDiscovery() { bluetoothTool.cancelDiscovery(); }, }, };