Linux + iPhone Continuity / iMessage / SMS
fix: handsfree/telephony reconnect on phone initiated conn
Zack Bartel1529b4b6133f
diff --git a/docs/BLUETOOTH.md b/docs/BLUETOOTH.mdindex 30d0ad3..7a0c179 100644--- a/docs/BLUETOOTH.md+++ b/docs/BLUETOOTH.md@@ -183,6 +183,38 @@ Documented in `man org.bluez.Telephony` and `man org.bluez.Call`, with asnapshot with no new bus, thread or subscription. `TelephonyClient` holds no state atall: it reads the snapshot and issues method calls on the monitor's connection.+### Hands-free after a phone-initiated reconnect++BlueZ connects its hands-free profile only on a fresh BR/EDR connect. When the phone+reconnects on its own -- after a Bluetooth toggle, or walking back into range -- it+brings the other profiles up without hands-free, and nothing retries. Measured+2026-09-06, with the phone reconnecting after a Bluetooth toggle:++```+bluetoothd: Device is already marked as connected+bluetoothd: .../sep3/fd0: fd(41) ready+```++No `telephony0`, and all three re-connect paths are dead ends on a live ACL:+`Device1.Connect()` returns success and does nothing, `ConnectProfile("0000111e")`+fails `No more profiles to connect to` (BlueZ matches the remote's UUID list, and the+iPhone advertises `0000111f`), `ConnectProfile("0000111f")` returns success and does+nothing.++Dropping the BR/EDR bearer alone is what recovers it, and it leaves LE and the ANCS+subscription untouched:++```bash+busctl --system call org.bluez /org/bluez/hciN/dev_BDADDR org.bluez.Bearer.BREDR1 Disconnect+```++`BearerSupervisor` does this once per hands-free outage, `HFP_ABSENT_SECONDS` after+BR/EDR comes up without a telephony object, and only while call control is enabled --+the cycle costs the MAP and PBAP sessions riding on that bearer. The flag survives+`reset()`, because the drop the cycle causes runs straight back through it, and clears+only when a telephony object is seen, so a phone that never offers hands-free is asked+exactly once per daemon.+### The audio stays on the phoneControl only, by construction. Ringing, caller ID, answering, hanging up and dialling@@ -203,9 +235,24 @@ say so:up: BlueZ exports no `/fd#` object and PipeWire creates no `bluez_card`.`./scripts/bt-probe.sh --calls` checks all of this in one command.-So the consequence is that **no WirePlumber configuration is needed or wanted**. The-`hfp_hf` role stays off, the machine never becomes an audio destination for the phone,-and "Keeping the phone's audio on the phone" below continues to apply unchanged.+So no WirePlumber configuration is needed for the audio to stay on the phone, and+"Keeping the phone's audio on the phone" below continues to apply unchanged. One is+needed for call control, though: stock `bluez5.roles` **includes** `hfp_hf`, a+phone-initiated link then goes to PipeWire, and BlueZ exports no `org.bluez.Telephony1`+at all -- `Calls (HFP): no`, Calls page empty, with nothing wrong on the phone.+Measured 2026-09-06 on wireplumber 0.5.17. Dropping that one role is enough, and it+does not require giving up the phone's music on the desktop:++```+monitor.bluez.properties = {+ bluez5.roles = [ a2dp_sink a2dp_source bap_sink bap_source asha_sink hfp_ag ]+}+```++That gave `telephony0`, a `Calls (HFP): yes` line carrying carrier, signal and battery,+and the iPhone's music still arriving as A2DP AAC at 44100 Hz stereo. Dropping+`a2dp_sink` too is what "Keeping the phone's audio on the phone" describes, and also+leaves call control working.`org.bluez.Telephony1` is also where desktop call audio arrives on its own eventually.It is not an HFP-specific interface: `profiles/audio/telephony.c` is a shared layer@@ -222,15 +269,25 @@ PipeWire's `bluez5` plugin implements the same profile in the HF role and publis`org.pipewire.Telephony`, and it *can* carry the audio (`AudioGatewayTransport1.Activate()`,with `bluez5.telephony.default-reject-sco` to keep it on the phone until asked).-It cannot be used at the same time. Both register for UUID `0000111e`, and BlueZ's-built-in profile wins: `Device1.Connect()` routes into it and fails, and PipeWire's-profile never gets the link.+It cannot be used at the same time. Both register for UUID `0000111e`, and which one+gets the link depends on who opens it.++A connect from this side routes into BlueZ's built-in profile and fails there:```profiles/audio/hfp-hf.c:hfp_connect() unable to start connectionbtd_service_connect() hfp profile connect failed for <phone>: Input/output error```+**A connect the phone opens goes to PipeWire**, measured 2026-09-06 on bluez 5.87 with+`bluetoothd --experimental` and the hfp plugin loaded -- no `--noplugin=hfp` anywhere.+PipeWire's `/Profile/HFPHF` ran the service level connection, `spa.bluez5.native` logged+`rfcomm_hfp_hf: AG indicator state: service = 1`, a live call arrived on+`bluez_input.<addr>` as `api.bluez5.profile = "headset-audio-gateway"` at 24000 Hz mono,+and BlueZ exported no telephony object at all. So the earlier claim that the built-in+profile always wins holds only for the direction this side initiates, and an iPhone+reconnecting on its own is the other direction.+Dropping `hfp_hf` from `bluez5.roles` makes BlueZ's profile connect immediately andexport `telephony0`. Using PipeWire's instead would mean `bluetoothd --noplugin=hfp`,a system-wide change to every Bluetooth device on the machine, in exchange for desktop@@ -270,7 +327,12 @@ You give up two things:Tether has no code on this path and does not test it in CI.-All three settings are required. Any one missing and nothing works at all.+Step 3 is the one that matters. Steps 1 and 2 were both required in the 2026-09-04 walk+and neither was on 2026-09-06 with pipewire 1.6.8 / wireplumber 0.5.17 / bluez 5.87:+stock `bluez5.roles` already carries `hfp_hf` and `a2dp_sink`, so with no wireplumber+configuration at all and the hfp plugin still loaded, a phone-initiated link put call+audio on the desktop on its own. Treat steps 1 and 2 as what to reach for when the+defaults do not do it, and check the versions before assuming they are needed.1. Stop BlueZ's built-in profile claiming UUID `0000111e`. Only needed on BlueZ >= 5.87;older builds have no built-in to disable.@@ -435,7 +497,7 @@ checks the daemon does not make.| Pairing bonds but the LE half never derives, on a machine with a USB dongle plugged in | Tether used the first powered controller, which is the dongle, not the built-in one | `tether --bt-status` marks the controller in use; `tether --bt-adapter <hciN>` picks another -- see 2026-09-01 || The link reads down forever with `br-connection-unknown`, while messages, contacts and notifications all work | This computer offers the iPhone no BR/EDR profile to connect to, and BlueZ only reports a link up while some local profile is connected | Nothing. Tether no longer waits on that link -- see 2026-08-23 below. Call support does not change this: BlueZ's hands-free profile is not one of the local profiles BlueZ counts || The iPhone's audio moves to the computer when Tether connects | The machine advertises itself as a Bluetooth speaker/headset, and iOS routes to it. Not caused by Tether beyond bringing the link up | See "Keeping the phone's audio on the phone" below |-| `tether --bt-calls` reports call control off | The iPhone has not connected Hands-Free, or `bluetoothd` is running without `--experimental` | The daemon's reason line names which. Confirm with `busctl --system tree org.bluez \| grep telephony` |+| `tether --bt-calls` reports call control off | PipeWire's default `bluez5.roles` includes `hfp_hf` and took the profile, the iPhone reconnected on its own and never opened hands-free, or `bluetoothd` is running without `--experimental` | Drop `hfp_hf` from `bluez5.roles` -- see "Calls". The daemon cycles the BR/EDR bearer once per outage for the second cause; confirm with `busctl --system tree org.bluez \| grep telephony` || Calls work but the audio is on the iPhone | Working as designed. BlueZ signals the call and never opens the voice link, so there is nothing to route here | Nothing. `./scripts/bt-probe.sh --calls` during a call shows the evidence; "Getting the call audio onto the desktop instead" is the trade if you want it || The Calls page is empty after configuring PipeWire for call audio | Expected. PipeWire owns the hands-free profile now, so BlueZ exports no `telephony0` for Tether to drive | Pick one: the revert steps under "Getting the call audio onto the desktop instead", or keep PipeWire and use an oFono-compatible dialer || Configured PipeWire for call audio and the machine is not in the iPhone's audio picker | `a2dp_sink` is missing from `bluez5.roles`. iOS only speaks hands-free to a machine it considers an audio destination | Add `a2dp_sink`, and accept that the phone's music comes here too -- see "Getting the call audio onto the desktop instead" |@@ -444,7 +506,9 @@ checks the daemon does not make.### Keeping the phone's audio on the phoneUnaffected by call support: calls run over BlueZ's own profile and never make this-machine an audio destination. This still applies exactly as written.+machine an audio destination. This still applies exactly as written -- and the role+list below is also what hands BlueZ the hands-free profile in the first place, since+dropping `hfp_hf` is what keeps PipeWire from taking it.Once the Classic link is up, the iPhone's calls, music, and system sounds play on thecomputer instead of the phone. PipeWire registers A2DP sink and HFP audio-gateway@@ -2196,3 +2260,53 @@ so the AppImage now prints three short lines instead of a 24-line paste. Flatpakhere-document, because `flatpak run` under `sudo` is the wrong user. The test on`set_class_command()` now asserts the unit text ends in a newline and that the here-documentcarries a bare `EOF` line to close it.++### 2026-09-06 - Hands-free was never reconnected after the phone brought the link back++Reported as "the iPhone has not connected Hands-Free", on a machine where it had been+connected minutes earlier. Three separate things, measured on bluez 5.87,+pipewire 1.6.8, wireplumber 0.5.17, `bluetoothd --experimental` with the hfp plugin+loaded.++**Stock WirePlumber takes the profile.** Default `bluez5.roles` already contains+`hfp_hf` and `a2dp_sink`, so no configuration is needed for PipeWire to claim+`0000111e`. With no file in `wireplumber.conf.d` at all, a phone-initiated link went to+`/Profile/HFPHF` -- `spa.bluez5.native` logging `rfcomm_hfp_hf: AG indicator state:+service = 1` -- and a live call played on the desktop as+`api.bluez5.profile = "headset-audio-gateway"`, 24000 Hz mono, with the laptop+microphone linked back to the phone. BlueZ exported no telephony object, so+`Calls (HFP)` read `no` with nothing wrong on the phone. The 2026-09-04 entry's summary+that the built-in profile "wins" describes the outbound direction only; the direction an+iPhone actually uses is the other one. `--noplugin=hfp` was never applied here and was+not needed, which also retires "all three settings are required" from the PipeWire+walkthrough.++**Dropping one role is enough, and it need not cost the music.**+`bluez5.roles = [ a2dp_sink a2dp_source bap_sink bap_source asha_sink hfp_ag ]`+gave `telephony0` and `Calls (HFP): yes` with carrier, signal and battery, while the+phone's music still arrived over A2DP AAC at 44100 Hz stereo. Keeping `a2dp_sink` is+what lets the phone stay an audio destination; only `hfp_hf` has to go.++**BlueZ connects hands-free once, on a fresh BR/EDR connect.** After a Bluetooth toggle+on the phone, the phone reconnected on its own and brought everything except+hands-free:++```+bluetoothd: Device is already marked as connected+bluetoothd: .../sep3/fd0: fd(41) ready+```++Nothing retries it, and on a live ACL there is nothing to retry with:+`Device1.Connect()` returns success and does nothing, `ConnectProfile("0000111e")` fails+`No more profiles to connect to` because BlueZ matches the remote's UUID list and the+iPhone advertises `0000111f`, and `ConnectProfile("0000111f")` returns success and does+nothing. `org.bluez.Bearer.BREDR1 Disconnect` recovers it: the reconnect that follows is+a fresh profile connect and carries hands-free, while LE and its ANCS subscription stay+up throughout.++`BearerSupervisor` now does that itself, `HFP_ABSENT_SECONDS` after BR/EDR comes up with+no telephony object and only while call control is on, because the cycle costs the MAP+and PBAP sessions on that bearer. It is one cycle per outage: the drop it causes runs+back through `reset()`, so the spent flag survives `reset()` and clears only when a+telephony object is actually seen. A phone that never offers hands-free -- the PipeWire+case above -- is therefore asked exactly once and then left alone.diff --git a/src/core/include/tether/bluetooth/bearer_supervisor.hpp b/src/core/include/tether/bluetooth/bearer_supervisor.hppindex ec548b5..98028f2 100644--- a/src/core/include/tether/bluetooth/bearer_supervisor.hpp+++ b/src/core/include/tether/bluetooth/bearer_supervisor.hpp@@ -27,6 +27,10 @@ namespace tether::bluetooth {// so a blip must not reset discovery, while a genuine loss still clears.inline constexpr int ANCS_BEARER_GRACE_SECONDS = 30;+ // How long BR/EDR may be up with no hands-free profile before the bearer is+ // cycled once to make BlueZ connect it.+ inline constexpr int HFP_ABSENT_SECONDS = 20;+// How long an LE link may read connected while the phone offers no ANCS// service before the solicitation goes back on air over it. Long enough for// GATT discovery on a freshly opened link, which runs to about a minute.@@ -59,6 +63,11 @@ namespace tether::bluetooth {virtual bool le_connect_outstanding() const = 0;// Whether the ANCS solicitation is registered AND BlueZ is advertising.virtual bool solicitation_on_air() const = 0;++ // Whether BlueZ exports a telephony object for the device.+ virtual bool telephony_present() const = 0;+ // Drops the BR/EDR bearer on its own, leaving LE up. Never Device1.Disconnect().+ virtual bool disconnect_classic(std::string& err) = 0;};struct BearerStatus {@@ -105,15 +114,19 @@ namespace tether::bluetooth {void reset();void set_ancs_enabled(bool enabled);+ void set_calls_enabled(bool enabled);const BearerStatus& status() const { return status_; }private:BearerOps& ops_;bool ancs_enabled_ = true;+ bool calls_enabled_ = false;BearerStatus status_;int classic_failures_ = 0;bool le_dial_spent_ = false;+ bool hfp_cycle_spent_ = false;+ int64_t telephony_absent_since_ = -1;int64_t classic_connected_since_ = -1;int64_t le_down_since_ = -1;// Whether the solicitation has been observed on air since LE went down.diff --git a/src/core/src/bluetooth/bearer_supervisor.cpp b/src/core/src/bluetooth/bearer_supervisor.cppindex 0969d21..fb446f8 100644--- a/src/core/src/bluetooth/bearer_supervisor.cpp+++ b/src/core/src/bluetooth/bearer_supervisor.cpp@@ -78,8 +78,12 @@ namespace tether::bluetooth {void BearerSupervisor::set_ancs_enabled(bool enabled) { ancs_enabled_ = enabled; }+ void BearerSupervisor::set_calls_enabled(bool enabled) { calls_enabled_ = enabled; }+void BearerSupervisor::reset() {+ // hfp_cycle_spent_ deliberately survivesclassic_failures_ = 0;+ telephony_absent_since_ = -1;le_down_since_ = -1;solicited_since_le_down_ = false;classic_connected_since_ = -1;@@ -186,7 +190,22 @@ namespace tether::bluetooth {return !(status_ == previous);}- // Classic is up. LE is only worth attempting once the ACL has settled.+ // Classic is up.+ if (calls_enabled_ && !ops_.telephony_present()) {+ if (telephony_absent_since_ < 0)+ telephony_absent_since_ = now;+ if (!hfp_cycle_spent_ && now - telephony_absent_since_ >= HFP_ABSENT_SECONDS) {+ hfp_cycle_spent_ = true;+ std::string err;+ if (!ops_.disconnect_classic(err))+ debug::log(WARN, "bluetooth: BR/EDR cycle for hands-free failed ({})", err);+ }+ } else {+ telephony_absent_since_ = -1;+ hfp_cycle_spent_ = false;+ }++ // LE is only worth attempting once the ACL has settled.if (!ancs_enabled_) {status_.reason = _("Connected. Notification mirroring is disabled.");return !(status_ == previous);diff --git a/src/core/src/bluetooth/connection.cpp b/src/core/src/bluetooth/connection.cppindex 2d905e8..debea13 100644--- a/src/core/src/bluetooth/connection.cpp+++ b/src/core/src/bluetooth/connection.cpp@@ -372,6 +372,26 @@ namespace tether::bluetooth {return le_->pending;}+ bool telephony_present() const override {+ auto device = lookup();+ return device && monitor_.snapshot().find_telephony(device->path) != nullptr;+ }++ bool disconnect_classic(std::string& err) override {+ auto device = lookup();+ if (!device) {+ err = "device not present";+ return false;+ }+ // Only the per-bearer call: Device1.Disconnect() would take the+ // LE half and the ANCS subscription riding on it.+ if (!device->has_classic_bearer) {+ err = "no BR/EDR bearer";+ return false;+ }+ return call(device->path, IFACE_BEARER_BREDR, "Disconnect", err);+ }+bool solicitation_on_air() const override {if (!ancs_solicitation_active())return false;@@ -1122,6 +1142,7 @@ namespace tether::bluetooth {if (wanted == calls_enabled)return;calls_enabled = wanted;+ bearers->set_calls_enabled(wanted);std::shared_ptr<TelephonyClient> client;if (wanted)diff --git a/test/test_bluetooth_supervisor.cpp b/test/test_bluetooth_supervisor.cppindex 3020f12..3ca83ac 100644--- a/test/test_bluetooth_supervisor.cpp+++ b/test/test_bluetooth_supervisor.cpp@@ -61,6 +61,19 @@ namespace {// about an adapter that will not advertise clears this.bool on_air = true;bool solicitation_on_air() const override { return on_air; }++ // Hands-free connected, as the bus shows it. Tests about the BR/EDR+ // cycle clear this.+ bool telephony = true;+ int classic_disconnects = 0;++ bool telephony_present() const override { return telephony; }++ bool disconnect_classic(std::string&) override {+ ++classic_disconnects;+ classic = false;+ return true;+ }};class FakeProfiles : public ProfileOps {@@ -903,3 +916,66 @@ TEST(BearerSupervisor, NamesTheHostWhenObexIsUpAndClassicKeepsRefusing) {EXPECT_EQ(sup.status().reason.find("keeps refusing"), std::string::npos)<< "blamed the phone while it was serving messages and contacts";}++// A phone-initiated reconnect brings BR/EDR up without hands-free, and nothing+// in BlueZ retries it. One bearer cycle is the remedy, and it must stay one: the+// drop it causes runs back through reset(), which is where a naive flag clears+// and the cycle repeats forever.+TEST(BearerSupervisor, CyclesClassicOnceWhenHandsFreeIsAbsent) {+ FakeBearer ops;+ ops.telephony = false;+ BearerSupervisor sup(ops, true);+ sup.set_calls_enabled(true);++ sup.tick(0); // connects Classic+ sup.tick(1);+ EXPECT_EQ(ops.classic_disconnects, 0) << "cycled before the grace window elapsed";++ sup.tick(1 + HFP_ABSENT_SECONDS - 1);+ EXPECT_EQ(ops.classic_disconnects, 0);++ sup.tick(1 + HFP_ABSENT_SECONDS);+ EXPECT_EQ(ops.classic_disconnects, 1);++ // The link the cycle dropped comes back, still with no hands-free.+ sup.reset();+ for (int64_t now = 1 + HFP_ABSENT_SECONDS; now <= 4 * HFP_ABSENT_SECONDS; ++now)+ sup.tick(now);+ EXPECT_EQ(ops.classic_disconnects, 1) << "hands-free never arrived, so the phone must be asked only once";+}++// Once hands-free is connected the cycle is armed again, so the next reconnect+// that comes up without it is recovered too.+TEST(BearerSupervisor, ArmsAnotherCycleAfterHandsFreeConnects) {+ FakeBearer ops;+ ops.telephony = false;+ BearerSupervisor sup(ops, true);+ sup.set_calls_enabled(true);++ sup.tick(0);+ sup.tick(1);+ sup.tick(1 + HFP_ABSENT_SECONDS);+ EXPECT_EQ(ops.classic_disconnects, 1);++ ops.telephony = true;+ sup.reset();+ sup.tick(100); // reconnects Classic+ sup.tick(101); // Classic up with hands-free on it: arms the next cycle++ ops.telephony = false;+ sup.tick(102);+ sup.tick(102 + HFP_ABSENT_SECONDS);+ EXPECT_EQ(ops.classic_disconnects, 2);+}++// Cycling BR/EDR costs the OBEX sessions on top of it, so it must never happen+// for a feature the user has switched off.+TEST(BearerSupervisor, NeverCyclesClassicWhenCallsAreDisabled) {+ FakeBearer ops;+ ops.telephony = false;+ BearerSupervisor sup(ops, true);++ for (int64_t now = 0; now <= 4 * HFP_ABSENT_SECONDS; ++now)+ sup.tick(now);+ EXPECT_EQ(ops.classic_disconnects, 0);+}