diff --git a/api/100J/HBY100-J.js b/api/100J/HBY100-J.js index bae7095..266bb90 100644 --- a/api/100J/HBY100-J.js +++ b/api/100J/HBY100-J.js @@ -429,8 +429,27 @@ export function fetchBleLocation() { return protocolInstance.getLocation(); } -// 等待蓝牙连接(扫描中时轮询,设备页可能在后台完成连接,100J 扫描约 15s) -function waitForBleConnection(maxWaitMs = 12000, intervalMs = 500) { +// 100J 设备页扫描最长约 15s,指令侧不宜空等过久;适配器关闭时不应再轮询 +const WAIT_BLE_CONNECTED_MS = 5000; +const BLE_POLL_INTERVAL_MS = 400; +const BLE_RECONNECT_MS = 2000; + +/** 系统蓝牙开关是否开启(与手机设置一致,避免关机蓝牙仍白等 12s) */ +function getBleAdapterAvailable() { + return new Promise((resolve) => { + if (typeof uni.getBluetoothAdapterState !== 'function') { + resolve(true); + return; + } + uni.getBluetoothAdapterState({ + success: (res) => resolve(!!res.available), + fail: () => resolve(false) + }); + }); +} + +// 等待协议层出现 bleDeviceId(页面后台扫描/连接中),超时则走 4G +function waitForBleConnection(maxWaitMs = WAIT_BLE_CONNECTED_MS, intervalMs = BLE_POLL_INTERVAL_MS) { if (protocolInstance.isBleConnected && protocolInstance.bleDeviceId) return Promise.resolve(true); return new Promise((resolve) => { const start = Date.now(); @@ -447,13 +466,13 @@ function waitForBleConnection(maxWaitMs = 12000, intervalMs = 500) { } setTimeout(tick, intervalMs); }; - console.log('[100J] 蓝牙未连接,等待扫描/连接中...', maxWaitMs, 'ms'); + console.log('[100J] 蓝牙未连接,短时等待扫描/连接…', maxWaitMs, 'ms'); tick(); }); } // 暴露给页面:尝试重连蓝牙(优先策略:断线后发指令前先尝试重连) -export function tryReconnectBle(timeoutMs = 2500) { +export function tryReconnectBle(timeoutMs = BLE_RECONNECT_MS) { if (protocolInstance.isBleConnected) return Promise.resolve(true); if (!protocolInstance.bleDeviceId) return Promise.resolve(false); return new Promise((resolve) => { @@ -565,28 +584,41 @@ function execWithBleFirst(bleExec, httpExec, logName, onWaiting) { return do4G(); }); } - // 无 bleDeviceId:页面可能在扫描,最多等 12s(否则用户以为点了没反应) + // 无 bleDeviceId:系统蓝牙关闭则立即 4G;开启则短时等页面扫描连上(不再白等 12s) if (!protocolInstance.bleDeviceId) { - if (typeof onWaiting === 'function') onWaiting(); - else showWaitUi('蓝牙连接中…'); - return waitForBleConnection(12000) - .then(connected => { - return connected ? doBle().catch(() => { + return getBleAdapterAvailable().then((adapterOk) => { + if (!adapterOk) { + console.log('[100J]', logName || '指令', '系统蓝牙未开启,走4G'); + return do4G(); + } + if (typeof onWaiting === 'function') onWaiting(); + else showWaitUi('蓝牙连接中…'); + return waitForBleConnection() + .then((connected) => { + return connected ? doBle().catch(() => { + console.log('[100J]', logName || '指令', '蓝牙失败,回退4G'); + return do4G(); + }) : do4G(); + }) + .finally(hideWaitUi); + }); + } + // 有 bleDeviceId 但未连:系统蓝牙关则直接 4G,否则短时重连 + return getBleAdapterAvailable().then((adapterOk) => { + if (!adapterOk) { + console.log('[100J]', logName || '指令', '系统蓝牙未开启,走4G'); + return do4G(); + } + if (typeof onWaiting !== 'function') showWaitUi('正在重连蓝牙…'); + return tryReconnectBle() + .then((reconnected) => { + return reconnected ? doBle().catch(() => { console.log('[100J]', logName || '指令', '蓝牙失败,回退4G'); return do4G(); }) : do4G(); }) .finally(hideWaitUi); - } - if (typeof onWaiting !== 'function') showWaitUi('正在重连蓝牙…'); - return tryReconnectBle(2500) - .then(reconnected => { - return reconnected ? doBle().catch(() => { - console.log('[100J]', logName || '指令', '蓝牙失败,回退4G'); - return do4G(); - }) : do4G(); - }) - .finally(hideWaitUi); + }); } // 爆闪模式