Skip to content

Adjust NFC writing AAR records to better fit tag size - #7285

Open
jpelgrom wants to merge 4 commits into
home-assistant:mainfrom
jpelgrom:nfc-fallback-applicationrecord
Open

Adjust NFC writing AAR records to better fit tag size#7285
jpelgrom wants to merge 4 commits into
home-assistant:mainfrom
jpelgrom:nfc-fallback-applicationrecord

Conversation

@jpelgrom

Copy link
Copy Markdown
Member

Summary

When writing NFC tags, the app tries to include Android Application Records (AAR) for io.homeassistant.companion.android and io.homeassistant.companion.android.minimal alongside the URL. If those don't fit on the tag, it falls back to no AARs to reduce the message size. This PR:

  • adjusts behavior so that when all AARs don't fit on the tag, it will fall back to one AAR with the current application ID first*, before falling back to no AARs
  • updates behavior for tags that supported NDEF records but weren't already formatted to also try messages with less/no AARs when formatting, instead of immediately failing

This might help with later comments in #7095 as some users reported manually writing one application record to the tag as a workaround/fix.

For the functions that were touched documentation (KDoc) and tests were added.

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. To figure out JUnit 4 doesn't support parametrized tests per function and what was the easiest alternative.
  • AI fully generated the code for this contribution, but I've reviewed and understood it before submitting and will respond without AI during review.

Screenshots

n/a

Link to pull request in documentation repositories

n/a, technical implementation detail

Any other notes

Note that when testing the application ID contains the .debug suffix. With the previous implementation that suffix would never be written to NFC tags, as it either had the production IDs hardcoded or none. With these changes, in the situation with 1 AAR you get the current application ID which will contain .debug if using a debug variant.

* Related: the iOS companion app implementation always writes one AAR for io.homeassistant.companion.android.

jpelgrom added 3 commits July 31, 2026 12:15
 - Improves fallback handling to add the current package instead of all or none, to make the best use of available space
Copilot AI review requested due to automatic review settings July 31, 2026 18:26

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 pull request refines NFC tag writing in the Android companion app by improving how NDEF messages are sized: it now attempts progressively smaller payloads (all AARs → current app AAR → URL only) to better fit different tag capacities, and applies the same fallback behavior when formatting NDEF-formatable tags.

Changes:

  • Add a multi-step fallback strategy for AAR (Android Application Record) inclusion based on tag capacity.
  • Update NDEF-formatable tag handling to retry formatting with smaller messages instead of failing immediately.
  • Add Robolectric tests covering expected record counts and failure modes across representative tag sizes.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
app/src/main/kotlin/io/homeassistant/companion/android/nfc/NFCUtil.kt Implements message-size fallback logic for NDEF and NDEF-formatable tag writing.
app/src/test/kotlin/io/homeassistant/companion/android/nfc/NFCUtilTest.kt Adds Robolectric tests validating the fallback behavior for various tag sizes and error cases.

Comment thread app/src/main/kotlin/io/homeassistant/companion/android/nfc/NFCUtil.kt Outdated
Comment thread app/src/main/kotlin/io/homeassistant/companion/android/nfc/NFCUtil.kt Outdated
Comment on lines +49 to +50
@RunWith(RobolectricTestRunner::class)
class NFCUtilTest {

@jpelgrom jpelgrom Jul 31, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't believe it's needed as we're not interacting with any application classes or Hilt here, running as a unit test similar to UriExtensionsTest.

Comment thread app/src/test/kotlin/io/homeassistant/companion/android/nfc/NFCUtilTest.kt Outdated
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.

2 participants