Skip to content

Avoid crashing when Bluetooth adapter is not available - #7271

Merged
jpelgrom merged 2 commits into
mainfrom
fix/allow_start_without_bluetooth_adapter
Jul 31, 2026
Merged

Avoid crashing when Bluetooth adapter is not available#7271
jpelgrom merged 2 commits into
mainfrom
fix/allow_start_without_bluetooth_adapter

Conversation

@TimoPtr

@TimoPtr TimoPtr commented Jul 29, 2026

Copy link
Copy Markdown
Member

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

  • New or updated tests have been added to cover the changes following the testing guidelines.
  • The code follows the project's code style and best_practices.
  • The changes have been thoroughly tested, and edge cases have been considered.
  • Changes are backward compatible whenever feasible. Any breaking changes are documented in the changelog for users and/or in the code for developers depending on the relevance.
  • I have read the Open Home Foundation AI Policy.

Select exactly one option that describes AI usage in this contribution:

  • I have not used AI for this contribution.
  • AI assistance was used for this contribution.
  • AI fully generated the code for this contribution, but I've reviewed and understood it before submitting and will respond without AI during review.

Copilot AI review requested due to automatic review settings July 29, 2026 13:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ImprovManagerFactory return ImprovManager? and return null when no Bluetooth adapter is present.
  • Update ImprovRepositoryImpl to 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).

@jpelgrom

Copy link
Copy Markdown
Member

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 canSetupImprov = false, preventing the frontend from interacting with the app's Improv implementation. Did you check if the config sends this correctly / there is a frontend issue here as well?

@TimoPtr

TimoPtr commented Jul 31, 2026

Copy link
Copy Markdown
Member Author

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 canSetupImprov = false, preventing the frontend from interacting with the app's Improv implementation. Did you check if the config sends this correctly / there is a frontend issue here as well?

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.

@TimoPtr
TimoPtr requested a review from jpelgrom July 31, 2026 06:27
@jpelgrom jpelgrom changed the title Avoid crashing when Bluetooth adatper is not available Avoid crashing when Bluetooth adapter is not available Jul 31, 2026
@jpelgrom

Copy link
Copy Markdown
Member

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.

@jpelgrom
jpelgrom merged commit e01f4dd into main Jul 31, 2026
38 of 39 checks passed
@jpelgrom
jpelgrom deleted the fix/allow_start_without_bluetooth_adapter branch July 31, 2026 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants