From 499babed26389b84fb61c53bdd6e87ae15f921ab Mon Sep 17 00:00:00 2001 From: luojiayi <1712054227@qq.com> Date: Fri, 25 Apr 2025 17:19:37 +0800 Subject: [PATCH] =?UTF-8?q?2025=E5=B9=B404=E6=9C=8825=E6=97=A517:19:26?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/device/index.jsx | 3 +- src/pages/deviceList/index.jsx | 3 +- src/pages/game/index.jsx | 8 +++-- src/pages/index/index.jsx | 55 +++++++++++++++++----------- src/utils/ble.js | 66 +++++++++++++++++----------------- 5 files changed, 77 insertions(+), 58 deletions(-) diff --git a/src/pages/device/index.jsx b/src/pages/device/index.jsx index 797847b..82c1556 100644 --- a/src/pages/device/index.jsx +++ b/src/pages/device/index.jsx @@ -14,6 +14,7 @@ export default function Index() { router.navigate('/pages/deviceList/index') } const deviceCallBack = () => { + console.log(BLESDK.deviceInfo, '解绑2'); setIsLink(BLESDK.deviceInfo.state) } useDidShow(() => { @@ -28,7 +29,7 @@ export default function Index() { success: (res) => { if (res.confirm) { setIsLink(false) - BLESDK.unBindDevice() + BLESDK.closeBluetoothAdapter() Taro.showToast({ title: '设备解绑成功', icon: 'success' }) } } diff --git a/src/pages/deviceList/index.jsx b/src/pages/deviceList/index.jsx index 086623c..1b328c6 100644 --- a/src/pages/deviceList/index.jsx +++ b/src/pages/deviceList/index.jsx @@ -23,7 +23,7 @@ export default function Index() { setTimeout(() => { Taro.hideLoading() - }, 3000) + }, 1500) return () => { BLESDK.stopBluetoothDevicesDiscovery() } @@ -35,7 +35,6 @@ export default function Index() { } BLESDK.onBluetoothDeviceFound((item) => { - if (JSON.stringify(deviceList).indexOf(item.mac) != -1) return deviceList.push(item) setDeviceList([...deviceList]) }) diff --git a/src/pages/game/index.jsx b/src/pages/game/index.jsx index 295f95f..c0d5952 100644 --- a/src/pages/game/index.jsx +++ b/src/pages/game/index.jsx @@ -21,8 +21,6 @@ export default function Index() { }) } const handleColorPicker = (e) => { - console.log(e); - if (e) { setCurColor(e) } @@ -62,6 +60,12 @@ export default function Index() { }) } + BLESDK.onBLECharacteristicValueChange((bytes) => { + if (bytes[0] == 0x7A && bytes[4] == 0xB3) { + Taro.showToast({ title: '操作成功', icon: 'none' }) + } + }) + return ( diff --git a/src/pages/index/index.jsx b/src/pages/index/index.jsx index 2767964..6cb42be 100644 --- a/src/pages/index/index.jsx +++ b/src/pages/index/index.jsx @@ -5,7 +5,7 @@ import ble from "@/assets/ble.png"; import soundCard from "@/assets/soundCard.png"; import game from "@/assets/game.png"; import ai from "@/assets/ai.png"; -import Taro from '@tarojs/taro'; +import Taro, { useDidShow } from '@tarojs/taro'; import { useCallback, useState } from 'react'; import { hex, strInsert } from '@/utils/sendOrder'; import { debounce } from '@/utils/index' @@ -37,10 +37,12 @@ export default function Index() { BLESDK.writeBleValue(new Uint8Array(strInsert(e)).buffer) }), []) const handleChange = (e, key) => { - setRuleForm({ - ...ruleForm, - [key]: e - }) + if (key !== 'nextAndPre') { + setRuleForm({ + ...ruleForm, + [key]: e + }) + } let str = '' switch (key) { case 'volume': @@ -61,8 +63,8 @@ export default function Index() { case 'playMode': str = `7B05EA0AB2${hex(e)}` break; - case 'musicControl': - str = `7B05EA0BB2${hex(e)}` + case 'playPause': + str = `7B05EA0BB2${hex(e ? 1 : 2)}` break; case 'girth': str = `7B05EA0CB2${hex(e)}` @@ -76,23 +78,25 @@ export default function Index() { case 'AI': str = `7B05EA11B2${hex(e)}` break; + case 'musicMode': + str = `7B05EA0AB2${hex(e)}` + break; + case 'nextAndPre': + str = `7B05EA0BB2${hex(e)}` + break; } sendCode(str) } - // 音乐控制 0:停止 1:播放 2:暂停 3:上一曲 4:下一曲 - const musicControl = (num) => { - let str = `7B05EA0BB2${hex(num)}` - sendCode(str) - } const handelMusicMode = (type) => { - let num = ruleForm.girth - if (type == "+" && ruleForm.girth < 3) { + let num = ruleForm.musicMode + if (type == "+" && ruleForm.musicMode < 3) { num++ - } else if (type == "-" && ruleForm.girth > 0) { + } else if (type == "-" && ruleForm.musicMode > 0) { num-- } - handleChange(num, 'girth') + if (num == ruleForm.musicMode) return + handleChange(num, 'musicMode') } const control = (type) => { @@ -102,8 +106,17 @@ export default function Index() { } else if (type == "-" && ruleForm.girth > 0) { num-- } + if (num == ruleForm.girth) return handleChange(num, 'girth') } + const BleCallBack = (bytes) => { + if (bytes[0] == 0x7D && bytes[4] == 0xB0) { + Taro.showToast({ title: bytes[5] == 0x00 ? '操作失败' : '操作成功', icon: 'none' }) + } + } + useDidShow(() => { + BLESDK.onBLECharacteristicValueChange(BleCallBack) + }) return ( @@ -138,12 +151,11 @@ export default function Index() { - musicControl(3)}> + handleChange(3, 'nextAndPre')}> { - musicControl(ruleForm.playPause ? 2 : 1) handleChange(!ruleForm.playPause, 'playPause') }}> - musicControl(4)}> + handleChange(4, 'nextAndPre')}> @@ -205,7 +217,10 @@ export default function Index() { 话筒混响模式 {[1, 2, 3, 4, 5, 6].map(item => { - return handleChange(item, 'reverb')}>{item} + return { + if (item == ruleForm.reverb) return + handleChange(item, 'reverb') + }}>{item} })} diff --git a/src/utils/ble.js b/src/utils/ble.js index b97d88f..8b0fcfc 100644 --- a/src/utils/ble.js +++ b/src/utils/ble.js @@ -21,13 +21,13 @@ const errMsg = { class Bluetooth { config = { - // serviceId: "4E31BF4A-8507-3A2B-7344-C7EDACD38104", //设备服务id - // NotifyUUID: "4E31BF4B-8507-3A2B-7344-C7EDACD38104", - // WriteUUID: "4E31BF4C-8507-3A2B-7344-C7EDACD38104", + serviceId: "4E31BF4A-8507-3A2B-7344-C7EDACD38104", //设备服务id + NotifyUUID: "4E31BF4B-8507-3A2B-7344-C7EDACD38104", + WriteUUID: "4E31BF4C-8507-3A2B-7344-C7EDACD38104", - serviceId: 'C7E6FAE0-E966-1000-8000-BEF9C723DF6A', //设备服务id - NotifyUUID: 'C7E6FAE1-E966-1000-8000-BEF9C723DF6A', - WriteUUID: 'C7E6FAE2-E966-1000-8000-BEF9C723DF6A', + // serviceId: 'C7E6FAE0-E966-1000-8000-BEF9C723DF6A', //设备服务id + // NotifyUUID: 'C7E6FAE1-E966-1000-8000-BEF9C723DF6A', + // WriteUUID: 'C7E6FAE2-E966-1000-8000-BEF9C723DF6A', }; // 连接状态 @@ -45,7 +45,7 @@ class Bluetooth { platform = Taro.getSystemInfoSync().platform; appAuthorize = Taro.getAppAuthorizeSetting(); - deviceInfo = Taro.getStorageSync('deviceInfo') || {} + deviceInfo = {} callBack = () => { }; searchBack = () => { }; @@ -91,6 +91,7 @@ class Bluetooth { success: () => { console.log("初始化蓝牙模块"); this.isInit = true; + this.deviceInfo = Taro.getStorageSync('deviceInfo') || {} if (this.deviceInfo.name) { this.createBleConnection() } @@ -108,7 +109,7 @@ class Bluetooth { if (this.appAuthorize.locationAuthorized != "authorized" && this.platform == "android") return Taro.showToast({ title: "请打开微信定位权限", icon: "none", duration: 3000 }); Taro.startBluetoothDevicesDiscovery({ - allowDuplicatesKey: true, + // allowDuplicatesKey: true, powerLevel: "high", success: (res) => { this.isFindBt = setTimeout(() => { @@ -146,9 +147,9 @@ class Bluetooth { const mac = this.strInsert(String(advertisData).substring(4, 16)).toLocaleUpperCase(); // 判断是否有搜索到设备 this.isFindBt && this.clearTimeoutFn("isFindBt"); - // if (devices.name && devices.name == "W53A") { if (devices.name && devices.name == "LE-AB2020") { + console.log(JSON.stringify(res), "搜索到设备"); const item = { deviceId: devices.deviceId, mac: mac, @@ -166,7 +167,7 @@ class Bluetooth { Taro.setStorageSync("deviceInfo", this.deviceInfo); console.log("正在连接设备"); Taro.createBLEConnection({ - timeout: 8000, + timeout: 3000, deviceId: this.deviceInfo.deviceId, success: () => { this.deviceInfo.state = true; @@ -224,7 +225,7 @@ class Bluetooth { success: (res) => { console.log("连接成功"); Taro.setStorageSync("deviceInfo", { ...this.deviceInfo, state: false }); - handshake1() + // handshake1() this.callBack(this.deviceInfo); }, fail: (err) => this.fail(err), @@ -234,12 +235,12 @@ class Bluetooth { onBLECharacteristicValueChange(fn) { Taro.onBLECharacteristicValueChange((data) => { let bytes = new Uint8Array(data.value); - let value = String(this.ab2hex(bytes)); + let value = String(this.ab2hex(bytes)).toLocaleUpperCase(); // 第二次握手 - if (bytes[2] == 0xea && bytes[3] == 0x01) { - handshake2() - } + // if (bytes[2] == 0xea && bytes[3] == 0x01) { + // handshake2() + // } console.log(`设备回复====>${value}`); TASK.executeTask(); fn && fn(bytes, value); @@ -247,11 +248,11 @@ class Bluetooth { } // 向蓝牙写入数据 writeBleValue(value) { + console.log("发送指令====>" + String(this.ab2hex(value)).toLocaleUpperCase()); if (!this.deviceInfo.state) { - console.log("蓝牙未连接") + // console.log("蓝牙未连接") return } - console.log("发送指令====>" + String(this.ab2hex(value)).toLocaleUpperCase()); return Taro.writeBLECharacteristicValue({ deviceId: this.deviceInfo.deviceId, serviceId: this.config.serviceId, @@ -285,17 +286,16 @@ class Bluetooth { } // 断开与蓝牙低功耗设备的连接 closeBluetoothAdapter() { - return new Promise((resolve, reject) => { - this.linkFlag = false; - this.deviceInfo = { state: false }; - wx.removeStorageSync("deviceInfo"); - this.callBack(this.deviceInfo); - Taro.closeBluetoothAdapter({ - success: (res) => { - this.openBluetoothAdapter().then(resolve).catch(reject); - }, - }); - }) + this.linkFlag = false; + this.deviceInfo = { state: false }; + wx.removeStorageSync("deviceInfo"); + Taro.closeBluetoothAdapter({ + success: (res) => { + console.log(this.deviceInfo, '解绑1'); + this.callBack(this.deviceInfo); + this.openBluetoothAdapter() + }, + }); } // 设备响应回调 deviceCallBack(fn) { @@ -309,11 +309,11 @@ class Bluetooth { }); return hexArr.join("").toLocaleLowerCase(); } - unBindDevice() { - this.deviceInfo = {} - wx.removeStorageSync('deviceInfo') - this.closeBluetoothAdapter() - } + // unBindDevice() { + // this.deviceInfo = {} + // wx.removeStorageSync('deviceInfo') + // this.closeBluetoothAdapter() + // } /** * 字符串插入字符 * @param {*} str 字符串