fix(ble): remove wake-up do path Capacitor (crashava plugin) v1.10.17
Some checks are pending
Build Android (APK + AAB) / build-android (push) Waiting to run

Karlão reportou que toda vez que tenta parear no APK, o app fecha
sem alert popup = crash nativo do plugin BLE Java.

Hipótese: as chamadas extras que adicionei (ble.read no notify char +
ble.write/writeWithoutResponse com 0x5A x4 wake bytes) crashavam o
plugin v6 em algum estado inválido.

Fix: remover wake-up sequence do path Capacitor. Mínimo viável:
1. ble.connect
2. ble.getServices
3. ble.startNotifications
4. delay 800ms
5. write JBD-0x03 com wnr forçado

A descoberta no PC (Web Bluetooth) confirmou que JBD-0x03 direto
(sem wake) já é suficiente — BMS responde com 41 bytes em 3 chunks.

startNotifications agora também envolto em try/catch que retorna
false se falhar (em vez de propagar exception nativa).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
PontualTech / Karlão 2026-04-29 14:44:16 -03:00
parent ca66a6995f
commit 70b123735e
6 changed files with 19 additions and 45 deletions

View file

@ -6226,26 +6226,13 @@ async function bmsProbeAndAttach(deviceId,deviceName){
else if(first===0xAA)bmsHandleJK(deviceId,dv,deviceName); // JK BMS else if(first===0xAA)bmsHandleJK(deviceId,dv,deviceName); // JK BMS
else if(first===0xA5)bmsHandleDaly(deviceId,dv,deviceName); // Daly else if(first===0xA5)bmsHandleDaly(deviceId,dv,deviceName); // Daly
}); });
try{
await ble.startNotifications({deviceId,service:foundService,characteristic:notifyChar}); await ble.startNotifications({deviceId,service:foundService,characteristic:notifyChar});
setBleDiag('Notify ativo · iniciando wake sequence...','ok'); setBleDiag('Notify ativo · aguardando 800ms...','ok');
await new Promise(r=>setTimeout(r,500)); }catch(e){setBleDiag('startNotifications erro: '+(e.message||e.errorMessage||'?'),'err');return false}
// Wake-up sequence (técnica Xiaoxiang): read inicial + wake bytes await new Promise(r=>setTimeout(r,800));
try{ // Wake-up REMOVIDO no path Capacitor — chamadas extra causavam crash nativo
await ble.read({deviceId,service:foundService,characteristic:notifyChar}); // Provamos no PC (Web Bluetooth) que JBD-0x03 direto funciona
setBleDiag('Read inicial OK (acorda stack)','info');
}catch(e){setBleDiag('Read inicial skip: '+(e.message||'?'),'info')}
await new Promise(r=>setTimeout(r,300));
// Wake bytes 5A x4 (algumas firmwares Xiaoxiang)
try{
const useWnr=writeOnlyWnr;
const fn=useWnr?'writeWithoutResponse':'write';
const wakePromise=ble[fn]({deviceId,service:foundService,characteristic:writeChar,value:bytesToBase64([0x5A,0x5A,0x5A,0x5A])});
const wakeTimeout=new Promise((_,rej)=>setTimeout(()=>rej(new Error('wake timeout')),2000));
await Promise.race([wakePromise,wakeTimeout]);
setBleDiag('Wake 5A x4 enviado','info');
}catch(e){setBleDiag('Wake skip: '+(e.message||'?'),'info')}
await new Promise(r=>setTimeout(r,1500));
setBleDiag('Iniciando probe de protocolos...','ok');
// Salva config no device pra reuso // Salva config no device pra reuso
const dev=state.btDevices?.find(d=>d.id===deviceId); const dev=state.btDevices?.find(d=>d.id===deviceId);
if(dev){ if(dev){
@ -6474,7 +6461,7 @@ async function removeBluetoothDevice(id){
renderBluetoothCard(); renderBluetoothCard();
} }
const APP_VERSION='1.10.16'; const APP_VERSION='1.10.17';
function renderBluetoothCard(){ function renderBluetoothCard(){
const el=document.getElementById('bt-list'); const el=document.getElementById('bt-list');
const supportEl=document.getElementById('bt-support'); const supportEl=document.getElementById('bt-support');

View file

@ -7,8 +7,8 @@ android {
applicationId "br.com.pontualtech.shivao" applicationId "br.com.pontualtech.shivao"
minSdkVersion rootProject.ext.minSdkVersion minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 29 versionCode 30
versionName "1.10.16" versionName "1.10.17"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
aaptOptions { aaptOptions {
// Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps. // Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps.

View file

@ -1,6 +1,6 @@
{ {
"name": "shivao-mobile", "name": "shivao-mobile",
"version": "1.10.16", "version": "1.10.17",
"description": "Shivao app nativo (Capacitor wrapper Android/iOS)", "description": "Shivao app nativo (Capacitor wrapper Android/iOS)",
"main": "index.js", "main": "index.js",
"type": "module", "type": "module",

View file

@ -6226,26 +6226,13 @@ async function bmsProbeAndAttach(deviceId,deviceName){
else if(first===0xAA)bmsHandleJK(deviceId,dv,deviceName); // JK BMS else if(first===0xAA)bmsHandleJK(deviceId,dv,deviceName); // JK BMS
else if(first===0xA5)bmsHandleDaly(deviceId,dv,deviceName); // Daly else if(first===0xA5)bmsHandleDaly(deviceId,dv,deviceName); // Daly
}); });
try{
await ble.startNotifications({deviceId,service:foundService,characteristic:notifyChar}); await ble.startNotifications({deviceId,service:foundService,characteristic:notifyChar});
setBleDiag('Notify ativo · iniciando wake sequence...','ok'); setBleDiag('Notify ativo · aguardando 800ms...','ok');
await new Promise(r=>setTimeout(r,500)); }catch(e){setBleDiag('startNotifications erro: '+(e.message||e.errorMessage||'?'),'err');return false}
// Wake-up sequence (técnica Xiaoxiang): read inicial + wake bytes await new Promise(r=>setTimeout(r,800));
try{ // Wake-up REMOVIDO no path Capacitor — chamadas extra causavam crash nativo
await ble.read({deviceId,service:foundService,characteristic:notifyChar}); // Provamos no PC (Web Bluetooth) que JBD-0x03 direto funciona
setBleDiag('Read inicial OK (acorda stack)','info');
}catch(e){setBleDiag('Read inicial skip: '+(e.message||'?'),'info')}
await new Promise(r=>setTimeout(r,300));
// Wake bytes 5A x4 (algumas firmwares Xiaoxiang)
try{
const useWnr=writeOnlyWnr;
const fn=useWnr?'writeWithoutResponse':'write';
const wakePromise=ble[fn]({deviceId,service:foundService,characteristic:writeChar,value:bytesToBase64([0x5A,0x5A,0x5A,0x5A])});
const wakeTimeout=new Promise((_,rej)=>setTimeout(()=>rej(new Error('wake timeout')),2000));
await Promise.race([wakePromise,wakeTimeout]);
setBleDiag('Wake 5A x4 enviado','info');
}catch(e){setBleDiag('Wake skip: '+(e.message||'?'),'info')}
await new Promise(r=>setTimeout(r,1500));
setBleDiag('Iniciando probe de protocolos...','ok');
// Salva config no device pra reuso // Salva config no device pra reuso
const dev=state.btDevices?.find(d=>d.id===deviceId); const dev=state.btDevices?.find(d=>d.id===deviceId);
if(dev){ if(dev){
@ -6474,7 +6461,7 @@ async function removeBluetoothDevice(id){
renderBluetoothCard(); renderBluetoothCard();
} }
const APP_VERSION='1.10.16'; const APP_VERSION='1.10.17';
function renderBluetoothCard(){ function renderBluetoothCard(){
const el=document.getElementById('bt-list'); const el=document.getElementById('bt-list');
const supportEl=document.getElementById('bt-support'); const supportEl=document.getElementById('bt-support');

View file

@ -1,7 +1,7 @@
// Shivao Service Worker — offline real // Shivao Service Worker — offline real
// Estratégia: shell precachado, tiles cache-first, windy network-first, /api passa direto. // Estratégia: shell precachado, tiles cache-first, windy network-first, /api passa direto.
// Versão usada nos cache names — bumpa essa string pra invalidar caches antigos em deploys. // Versão usada nos cache names — bumpa essa string pra invalidar caches antigos em deploys.
const VERSION = 'shivao-v1.10.16'; const VERSION = 'shivao-v1.10.17';
const SHELL_CACHE = `shivao-shell-${VERSION}`; const SHELL_CACHE = `shivao-shell-${VERSION}`;
const TILES_CACHE = 'shivao-tiles-v1'; // separado pra não invalidar tiles em update do shell const TILES_CACHE = 'shivao-tiles-v1'; // separado pra não invalidar tiles em update do shell
const WINDY_CACHE = `shivao-windy-${VERSION}`; const WINDY_CACHE = `shivao-windy-${VERSION}`;

View file

@ -413,7 +413,7 @@ app.get('/.well-known/assetlinks.json', (req, res) => {
}); });
// Atalho: /apk redireciona pra última APK release no Forgejo // Atalho: /apk redireciona pra última APK release no Forgejo
const LATEST_APK_URL = 'https://git.pontualtech.work/karlao/shivao-projeto/releases/download/v1.10.16/Shivao-v1.10.16.apk'; const LATEST_APK_URL = 'https://git.pontualtech.work/karlao/shivao-projeto/releases/download/v1.10.17/Shivao-v1.10.17.apk';
app.get('/apk', (req, res) => res.redirect(302, LATEST_APK_URL)); 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) // Página A4 imprimível com QR Code + instruções (cola no barco/marina)