From b5605e802540ba696a3bf0dcee0a9edbff9c9f6e Mon Sep 17 00:00:00 2001 From: liyi Date: Fri, 7 Mar 2025 11:12:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A032bit=E8=AE=BE=E5=A4=87u?= =?UTF-8?q?uid=E5=88=A4=E6=96=AD=E3=80=81=E5=A2=9E=E5=8A=A0android?= =?UTF-8?q?=E6=9C=BA=E5=9E=8B=E4=B8=8B=E7=9A=84mtu=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni/basic.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/uni/basic.js b/uni/basic.js index cae8c1e..3d5ea52 100644 --- a/uni/basic.js +++ b/uni/basic.js @@ -49,6 +49,18 @@ export function searchAndConnectDevice(name, reset = true) { } break } + if (uuid && uuid.slice(0, 2) === '75') { + await stopBluetoothDevicesDiscovery() + clearTimeout(timeoutTimer) + clearInterval(timer) + if (uuid.slice(4, 6) === '00' && reset) { + resolve(Result.DeviceHasBeenReset) + } else { + const connectResult = await createBLEConnection(deviceList[i].deviceId) + resolve(connectResult) + } + break + } } } } else { @@ -177,6 +189,25 @@ export function createBLEConnection(deviceId, reconnectNumber = 0) { res.data.serviceId, res.data.notifyCharacteristicId ) + // 使用同步方法获取系统信息 + const systemInfo = uni.getSystemInfoSync(); + + if (systemInfo.platform === 'android') { + // 如果是Android机型,则设置BLE MTU + uni.setBLEMTU({ + deviceId: deviceId, + mtu: 512, + fail: res => { + console.log('mtu fail', res); + }, + success: res => { + console.log('mtu success', res); + } + }); + } else { + // 对于非Android设备,可以在这里添加其他逻辑或者直接忽略 + console.log('当前设备不是Android,跳过设置BLE MTU'); + } resolve(res) }, async fail(err) {