From 2fca191676ff664e40dd21c2da9a8037c8eed27e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?PontualTech=20/=20Karl=C3=A3o?= Date: Wed, 29 Apr 2026 07:18:44 -0300 Subject: [PATCH] fix(ble): bmsManualRead reconecta GATT antes do probe v1.10.5 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug v1.10.4: clicar 🔄 Re-ler gerava 'getServices erro: Bluetooth LE not initialized' porque Android desconecta GATT em background pra economizar bateria, mas state.btDevices ainda mostra 'conectado'. Fix: bmsManualRead agora faz 3 passos sequenciais com diagnóstico: 1. ensureBleNativeReady() — garante plugin inicializado 2. ble.connect({deviceId, timeout:15000}) — reconecta GATT (silent se 'already connected') 3. bmsProbeAndAttach() — probe completo Cada passo emite log próprio: "Plugin init OK", "GATT reconectado" ou "GATT já conectado", "🔍 Enumerando characteristics..." Co-Authored-By: Claude Opus 4.7 (1M context) --- app/diario-bordo.html | 28 +++++++++++++++++++++++----- mobile/android/app/build.gradle | 4 ++-- mobile/package.json | 2 +- server/public/index.html | 28 +++++++++++++++++++++++----- server/src/index.js | 2 +- 5 files changed, 50 insertions(+), 14 deletions(-) diff --git a/app/diario-bordo.html b/app/diario-bordo.html index b3b0e5c..94ec173 100644 --- a/app/diario-bordo.html +++ b/app/diario-bordo.html @@ -6125,12 +6125,30 @@ async function bmsQueryBasic(deviceId,withoutResponse){ await bmsWriteCmd(deviceId,BMS_CMD_BASIC,withoutResponse); } -// Re-leitura manual a partir do botão UI +// Re-leitura manual: garante init + reconecta GATT + roda probe async function bmsManualRead(deviceId){ - setBleDiag('🔄 Re-leitura manual · re-rodando probe completo...','info'); + setBleDiag('🔄 Re-leitura manual','info'); + const dev=state.btDevices?.find(d=>d.id===deviceId); + const deviceName=dev?.name||'BMS'; try{ - // Re-roda probe completo (lista chars de novo + tenta protocolos) - await bmsProbeAndAttach(deviceId,state.btDevices?.find(d=>d.id===deviceId)?.name||'BMS'); + // 1. Garante plugin inicializado + await ensureBleNativeReady(); + setBleDiag('Plugin init OK','info'); + // 2. Reconecta GATT (Android pode ter desconectado em background) + const ble=window.Capacitor?.Plugins?.BluetoothLe; + if(ble){ + try{ + await ble.connect({deviceId,timeout:15000}); + setBleDiag('GATT reconectado','ok'); + await new Promise(r=>setTimeout(r,500)); + }catch(e){ + const msg=e.message||e.errorMessage||'?'; + if(/already/i.test(msg))setBleDiag('GATT já conectado','info'); + else setBleDiag('Reconnect erro: '+msg,'warn'); + } + } + // 3. Roda probe completo + await bmsProbeAndAttach(deviceId,deviceName); }catch(e){setBleDiag('Manual read falhou: '+(e.message||e.errorMessage),'err')} } @@ -6247,7 +6265,7 @@ async function removeBluetoothDevice(id){ renderBluetoothCard(); } -const APP_VERSION='1.10.4'; +const APP_VERSION='1.10.5'; function renderBluetoothCard(){ const el=document.getElementById('bt-list'); const supportEl=document.getElementById('bt-support'); diff --git a/mobile/android/app/build.gradle b/mobile/android/app/build.gradle index 3f8b370..9783657 100644 --- a/mobile/android/app/build.gradle +++ b/mobile/android/app/build.gradle @@ -7,8 +7,8 @@ android { applicationId "br.com.pontualtech.shivao" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion - versionCode 20 - versionName "1.10.4" + versionCode 21 + versionName "1.10.5" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" aaptOptions { // Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps. diff --git a/mobile/package.json b/mobile/package.json index 031baff..911102d 100644 --- a/mobile/package.json +++ b/mobile/package.json @@ -1,6 +1,6 @@ { "name": "shivao-mobile", - "version": "1.10.4", + "version": "1.10.5", "description": "Shivao app nativo (Capacitor wrapper Android/iOS)", "main": "index.js", "type": "module", diff --git a/server/public/index.html b/server/public/index.html index b3b0e5c..94ec173 100644 --- a/server/public/index.html +++ b/server/public/index.html @@ -6125,12 +6125,30 @@ async function bmsQueryBasic(deviceId,withoutResponse){ await bmsWriteCmd(deviceId,BMS_CMD_BASIC,withoutResponse); } -// Re-leitura manual a partir do botão UI +// Re-leitura manual: garante init + reconecta GATT + roda probe async function bmsManualRead(deviceId){ - setBleDiag('🔄 Re-leitura manual · re-rodando probe completo...','info'); + setBleDiag('🔄 Re-leitura manual','info'); + const dev=state.btDevices?.find(d=>d.id===deviceId); + const deviceName=dev?.name||'BMS'; try{ - // Re-roda probe completo (lista chars de novo + tenta protocolos) - await bmsProbeAndAttach(deviceId,state.btDevices?.find(d=>d.id===deviceId)?.name||'BMS'); + // 1. Garante plugin inicializado + await ensureBleNativeReady(); + setBleDiag('Plugin init OK','info'); + // 2. Reconecta GATT (Android pode ter desconectado em background) + const ble=window.Capacitor?.Plugins?.BluetoothLe; + if(ble){ + try{ + await ble.connect({deviceId,timeout:15000}); + setBleDiag('GATT reconectado','ok'); + await new Promise(r=>setTimeout(r,500)); + }catch(e){ + const msg=e.message||e.errorMessage||'?'; + if(/already/i.test(msg))setBleDiag('GATT já conectado','info'); + else setBleDiag('Reconnect erro: '+msg,'warn'); + } + } + // 3. Roda probe completo + await bmsProbeAndAttach(deviceId,deviceName); }catch(e){setBleDiag('Manual read falhou: '+(e.message||e.errorMessage),'err')} } @@ -6247,7 +6265,7 @@ async function removeBluetoothDevice(id){ renderBluetoothCard(); } -const APP_VERSION='1.10.4'; +const APP_VERSION='1.10.5'; function renderBluetoothCard(){ const el=document.getElementById('bt-list'); const supportEl=document.getElementById('bt-support'); diff --git a/server/src/index.js b/server/src/index.js index 1148743..4ef6f1a 100644 --- a/server/src/index.js +++ b/server/src/index.js @@ -347,7 +347,7 @@ app.get('/.well-known/assetlinks.json', (req, res) => { }); // Atalho: /apk redireciona pra última APK release no Forgejo -const LATEST_APK_URL = 'https://git.pontualtech.work/karlao/shivao-projeto/releases/download/v1.10.4/Shivao-v1.10.4.apk'; +const LATEST_APK_URL = 'https://git.pontualtech.work/karlao/shivao-projeto/releases/download/v1.10.5/Shivao-v1.10.5.apk'; app.get('/apk', (req, res) => res.redirect(302, LATEST_APK_URL)); // Página A4 imprimível com QR Code + instruções (cola no barco/marina)