Skip to content

Finish Bluetooth activity if permission revoked while backgrounded - #267

Open
evanofficial wants to merge 1 commit into
chesterbr:mainfrom
evanofficial:fix/bluetooth-permission-revoked-crash
Open

Finish Bluetooth activity if permission revoked while backgrounded#267
evanofficial wants to merge 1 commit into
chesterbr:mainfrom
evanofficial:fix/bluetooth-permission-revoked-crash

Conversation

@evanofficial

Copy link
Copy Markdown

Fixes #265

What

Adds a permission re-check in BluetoothActivity.onResume(). If BLUETOOTH_CONNECT (or any other required Bluetooth permission) has been revoked while the app was in the background, the activity calls finish() before any Bluetooth calls can run.

Why

onCreate() already checks permissions and only proceeds if they are granted. However, onResume() did not re-verify, so returning from the background with a revoked permission led to a SecurityException crash — most visibly in ServidorBluetoothActivity.atualizaClientes() (called 4s after onResume() via postDelayed), and potentially in ClienteBluetoothActivity.listaDispositivosPareados() as well.

Fixing it in the base class covers both subclasses without duplicating logic.

Fixes SecurityException crash on Android 12+ when BLUETOOTH_CONNECT is
revoked while the app is in the background. Re-checking permissions in
onResume() prevents any subsequent Bluetooth calls from running without
the required permission.
@chesterbr

Copy link
Copy Markdown
Owner

Olá! Obrigado pela contribuição. No momento eu não tenho acesso a hardware Bluetooth (e não encontrei até hoje nenhuma solução de emulação que permita fazer testes automáticos ou manuais - incrivelmente, Java ME tinha várias opções assim), então não tenho como verificar este fix, mas posso considerar incluir numa versão futura se você ou alguma outra pessoa puder testar em hardware real.

Nesse sentido, também seria interessante que o PR acompanhasse passos para verificar a resolução. Obrigado!

@chesterbr

Copy link
Copy Markdown
Owner

Uma dúvida: o que acontece exatamente quando essa detecção no resume encerra a atividade? (i.e., como isso é diferente da exceção? nos dois casos parece que a activity é encerrada). Haveria a possibilidade de re-requisitar, ao invés de derrubara a activity?

Outra dúvida: a perta sempre ocorre quando o aplicativo perde foco? Só me pergunto se estamos realmente cobrindo um fluxo de uso, ou apenas seguindo uma dedução de IA que pode ou não corresponder a um problema de usuário.

Obrigado!

@evanofficial

Copy link
Copy Markdown
Author

Uma dúvida: o que acontece exatamente quando essa detecção no resume encerra a atividade? (i.e., como isso é diferente da exceção? nos dois casos parece que a activity é encerrada). Haveria a possibilidade de re-requisitar, ao invés de derrubara a activity?

Outra dúvida: a perta sempre ocorre quando o aplicativo perde foco? Só me pergunto se estamos realmente cobrindo um fluxo de uso, ou apenas seguindo uma dedução de IA que pode ou não corresponder a um problema de usuário.

Obrigado!

Good points! To answer both:

  1. Re-requesting is definitely an option — the tradeoff is that it adds complexity around what happens if the user denies again, and the postDelayed calls in atualizaClientes that could still fire before the dialog resolves. finish() is the safer minimal fix, but happy to rework it to re-request if you'd prefer that flow.

  2. Not every time the app loses focus — it only happens when the user specifically revokes BLUETOOTH_CONNECT in system settings while the app is backgrounded. The issue has the Play Console data showing it hit 13 real users on v3.0.6 across Android 12-15, so it is a real (if uncommon) scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash: SecurityException ao chamar métodos do BluetoothAdapter no Android 12+

2 participants