Avoid crashing when Bluetooth adapter is not available - #7271
Conversation
There was a problem hiding this comment.
Pull request overview
This PR prevents crashes in the Improv (BLE Wi‑Fi onboarding) integration on devices that do not have a Bluetooth adapter by making Improv manager creation nullable and ensuring scan/provision flows don’t dereference a missing adapter.
Changes:
- Make
ImprovManagerFactoryreturnImprovManager?and returnnullwhen no Bluetooth adapter is present. - Update
ImprovRepositoryImplto tolerate a missing Improv manager during scanning and teardown, and to short-circuit provisioning when the manager is unavailable. - Add unit tests covering the “no Bluetooth adapter” behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| app/src/main/kotlin/io/homeassistant/companion/android/frontend/improv/ImprovRepositoryImpl.kt | Handles ImprovManager being nullable to avoid crashes and make scan/stopScan safe when no adapter exists. |
| app/src/main/kotlin/io/homeassistant/companion/android/frontend/improv/ImprovManagerFactory.kt | Updates the factory contract to allow returning null on devices without a Bluetooth adapter. |
| app/src/main/kotlin/io/homeassistant/companion/android/frontend/improv/FrontendImprovModule.kt | Adjusts the Hilt-provided factory to only create ImprovManager when a Bluetooth adapter is available. |
| app/src/test/kotlin/io/homeassistant/companion/android/frontend/improv/ImprovRepositoryImplTest.kt | Adds tests for missing-adapter cases (scan/provision). |
|
While this is a good additional check, these calls should never happen from the frontend if there is no Bluetooth adapter because then the device should be sending |
The issue was on hilt side, since we inject the manager into the ViewModel and the ImprovManager is in a class attribute it was initiated when created. In the initialization there is an attribute that get the Bluetooth adapter without checking if it's available and causing a NPE. |
|
OK that would have been good context to have and obvious if a stacktrace was included :) Checked the library and indeed we're using the adapter without additional checks. The user (= this app) is supposed to handle permission/availability checks. |
Summary
While working on the #7035 I found out that we are currently crashing the app when the device doesn't have a Bluetooth adapter which could happen. This PR handle this case.
Checklist
Select exactly one option that describes AI usage in this contribution: