Skip to content

Fix connecting to printers on Bluetooth LE - #386

Merged
DL6ER merged 10 commits into
masterfrom
fix/le-adapter-search
Dec 3, 2026
Merged

DL6ER merged 10 commits into
masterfrom
fix/le-adapter-search

Conversation

@DL6ER

@DL6ER DL6ER commented Dec 3, 2026

Copy link
Copy Markdown
Owner

Three fixes for printers on Bluetooth LE, found with a Niimbot B1:

  1. With a second Bluetooth adapter, a printer set up by its address could be reached through the wrong one ("Host is down"). It is now searched for first, once until the next look for printers, and reached through the adapter that hears it.
  2. A connection the adapter could not set up ("Function not implemented") is tried once more.
  3. A print during a search of classic Bluetooth failed as the search ended. It now waits for the search.

The wait was tried on a B1. With a second adapter attached, its connection still drops at times.

No pictures: the label designer looks as before.

DL6ER added 6 commits December 3, 2026 00:00
A printer set up by its address was connected through any adapter, as no search had heard it. With a second adapter, e.g. a dongle on USB/IP out of the printer's reach, the kernel may take that one, and every connection timed out ("Host is down"). Where no search heard the printer and the kernel lists more than one adapter, it is now searched for `FIND_WAIT` seconds first and connected through the adapter that heard it, which is kept as after any search.

Signed-off-by: DL6ER <dl6er@dl6er.de>
The first status query of a B1 after the server started could fail with "[Errno 38] Function not implemented" while the search for printers ran, and the label designer then showed for a few seconds that the printer does not answer. Linux tells ENOSYS where the adapter could not set up an LE connection for a reason it has no error number for, e.g. as a search on the adapter ended. Such a connection is now set up once more.

Signed-off-by: DL6ER <dl6er@dl6er.de>
Signed-off-by: DL6ER <dl6er@dl6er.de>
…nded

A print on a Niimbot B1 right after the server started ran into the search for printers, which also searched classic Bluetooth for 11 s. The LE link of its status query came up, but the connection failed only as the search ended ("Host is down"), and the job connected right after got no answer to its end of page, so nothing came out. While classic Bluetooth is searched, see `devices.classic_search`, a connection on Bluetooth LE now waits for the search to end, for at most `CLASSIC_SEARCH_WAIT` seconds.

Signed-off-by: DL6ER <dl6er@dl6er.de>
A printer no search heard, e.g. a B1 that is off, is asked again every 10 s while the label designer follows it, and with more than one adapter each try searched every adapter for `FIND_WAIT` seconds. `adapter_reaching()` now searches for an address once, and again only after the label designer looked for printers since, see `devices.looked`.

Signed-off-by: DL6ER <dl6er@dl6er.de>
Signed-off-by: DL6ER <dl6er@dl6er.de>
Copilot AI balanced review requested due to automatic review settings December 3, 2026 13:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

A status query can hold the printer lock while waiting for discovery, causing concurrent prints to time out and fail as busy.

3 open issues
What changed in this PR

Improves Bluetooth LE printer connections by selecting a reachable adapter, retrying setup failures, and waiting for classic Bluetooth discovery.

Changes:

  • Searches for unheard printers when multiple adapters are available.
  • Retries ENOSYS connection failures once and waits for classic discovery.
  • Adds regression tests and isolates tests from host Bluetooth adapters.
File Description
tests/​printing/​test_niimbot_ble.py Tests adapter selection, search caching, retries, and discovery waits.
tests/​conftest.py Isolates adapter discovery and resets search state.
app/​labeldesigner/​drivers/​transports.py Adds LE connection retries and discovery waits.
app/​labeldesigner/​bluetooth.py Finds reachable adapters and caches search attempts.

Review effort: Balanced

Comment thread app/labeldesigner/drivers/transports.py Resolved
Comment thread app/labeldesigner/bluetooth.py Resolved
Comment thread app/labeldesigner/drivers/transports.py Resolved
DL6ER added 2 commits December 3, 2026 15:37
…e printer

A status query of a B1 or PUQU on Bluetooth LE waited for a search of classic Bluetooth while it held the printer's lock, so a print that came meanwhile could give up after `DEVICE_LOCK_TIMEOUT` and fail as busy. The status queries now wait in `transports.after_classic_search()` before they lock the printer; a job still waits there with the lock, as it holds it anyway.

The wait also lasted only `CLASSIC_SEARCH` plus 2 s, while a search that resolves names goes on for `NAME_WAIT` more. It now lasts `CLASSIC_SEARCH + NAME_WAIT + SEARCH_ENDS` at most.

Signed-off-by: DL6ER <dl6er@dl6er.de>
Two status queries of printers on Bluetooth LE that no search heard could each start a search, and the kernel refused the second as busy: that one returned before the first knew the adapter, so its printer was reached through any adapter and not searched for again until the next look. `discover()` now runs one search at a time, also the label designer's, and `adapter_reaching()` searches only where the search it waited for did not hear the printer.

Signed-off-by: DL6ER <dl6er@dl6er.de>
@DL6ER
DL6ER requested a balanced review from Copilot December 3, 2026 15:56

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

DL6ER added 2 commits December 3, 2026 17:26
Review points on the waits for a search:
1. A status query of a printer on Bluetooth LE searches for its adapter and waits for a search of classic Bluetooth in `transports.ready_le()`, which `bluetooth_status()` runs before the query locks the printer where the driver tells `le`, so that a print meanwhile does not wait for the lock. The drivers no longer call the wait themselves.
2. Within the lock, the wait for a search of classic Bluetooth comes right before the connection is set up, after the search for the adapter, as a search of classic Bluetooth may begin meanwhile.
3. The wait lasts the longest search from the time that search began, `bluetooth.classic_began`, as it may first wait for another search.
4. A search waits until the adapters stopped searching, as Linux fails a connection on Bluetooth LE set up before.
5. A search for an adapter counts only where an adapter searched: one refused as busy is tried again at the next connection.
6. The adapters counted are those on with Bluetooth LE, read on the management channel as for a search, not the entries in /sys/class/bluetooth; where they cannot be read, the log says so.

Signed-off-by: DL6ER <dl6er@dl6er.de>
…s lock

Our own review of the adapter search:
1. A search with no adapter on with Bluetooth LE ends at once, not after its timeout.
2. A connection in the printer's lock never waits for a search that runs; a status query waits for one at most `SEARCH_WAIT` seconds before it locks the printer, and searches for no adapter while classic Bluetooth is searched, as that search searches Bluetooth LE too.
3. The adapters on are counted again after `COUNT_KEPT` seconds or a look, and `discover()` counts them with its own reading, so that a followed status query does not ask the kernel, nor log that it cannot, every 2 s.
4. A status query of a printer whose connection is kept gets ready for none, as none is set up.
5. A connection that failed through the adapter that heard the printer forgets it, so that the printer is searched for again, e.g. after a dongle was removed.
6. A management reply too short for what it tells is left out, and a connection that failed for another reason closes its socket.
7. `discover()` tells when its search of classic Bluetooth ends at the latest, from its own timeout and waits, and the wait for it follows that.
8. A search the adapters refused is tried again `REFUSED_AGAIN` seconds later, not at each status query.
9. The searches are kept apart by a plain lock, as none takes it twice.

Signed-off-by: DL6ER <dl6er@dl6er.de>
@DL6ER
DL6ER merged commit 2670a87 into master Dec 3, 2026
7 checks passed
@DL6ER
DL6ER deleted the fix/le-adapter-search branch December 3, 2026 18:00
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.

2 participants