Linux + iPhone Continuity / iMessage / SMS
fix: moar le connection issues
Zack Bartel1ba69de5c63a
diff --git a/docs/BLUETOOTH.md b/docs/BLUETOOTH.mdindex 37c4d00..07cea6e 100644--- a/docs/BLUETOOTH.md+++ b/docs/BLUETOOTH.md@@ -123,6 +123,7 @@ checks the daemon does not make.| The phone shows two entries for this computer | A failed pairing left both a Classic and an LE record | Delete both on the phone before retrying || LE never connects and the log repeats `org.bluez.Error.InProgress` | BlueZ is holding an auto-connect registration that never completed | `sudo systemctl restart bluetooth` -- nothing short of that clears it, see 2026-08-19 below. With `tether-btclass@hci0` enabled the class survives the restart || The status says "Nothing is listening for the iPhone's LE link" | BlueZ refused to register for the phone's LE connection, so nothing is waiting for it | `sudo systemctl restart bluetooth`. This is different from waiting on the phone, which the status says instead when a registration is open |+| LE never connects and the log repeats `le-connection-abort-by-local` | Something on this side is cancelling the connection. Tether's own cause was a `PreferredBearer` write racing the async connect, fixed; anything else writing that property during a connect will do the same | Check no other Bluetooth tool is driving the same device. The phone is not the cause: `abort-by-local` means the local host cancelled || Notifications stopped and never came back, while messages and contacts kept working | Fixed. The bearer supervisor used to stop retrying LE after six attempts, and only a Classic drop or a daemon restart re-armed it | Nothing. LE is now retried indefinitely at the five-minute ceiling, and the ANCS solicitation is kept on air whenever LE is down || `tether --bt-status` reports `Bond: BR/EDR only` | The bond was made without cross-transport key derivation, so it has no LE half and can never carry ANCS | Forget this computer on the iPhone and pair again. Check `secure-connections` in the same output first: re-pairing cannot help while it is off || Everything connects but `ancs_ready` stays false | Compatibility mode, or iOS has not authorized notification content yet | Check `Mode:` in `tether --bt-status`. In full mode the daemon retries, the first request returns `NotPermitted` until the prompt on the phone is approved |@@ -514,6 +515,44 @@ The `Role Change ... Role: Peripheral` right before the exchange is also worthnoting: the iPhone takes the central role on the ACL, which is what connect-firstpairing exists to allow.+### 2026-08-19 - `le-connection-abort-by-local` was our own PreferredBearer write++After a reboot, LE would not come up at all: every attempt logged+`org.bluez.Error.Failed: le-connection-abort-by-local`, while the same+`Bearer.LE1.Connect` issued by hand succeeded immediately.++The difference was not the advertisement. The supervisor used to do this:++```+set PreferredBearer = "le"+Bearer.LE1.Connect() <- asynchronous, returns at once+set PreferredBearer = "bredr" <- milliseconds later, connect still in flight+```++The second write lands while BlueZ is still setting the LE connection up, and+BlueZ abandons it -- which is exactly what `abort-by-local` says: the local host+cancelled it, not the phone.++`PreferredBearer` steers `Device1.Connect`. This path calls the per-bearer+`Bearer.LE1.Connect`, which already names the transport, so the property was+never needed here. It is now written only on the `Device1.Connect` fallback used+by BlueZ builds that publish `Bearer.LE1` without a `Connect` method. With the+writes removed, LE connected on the first attempt from a cold LE-down state and+held for a three-minute soak with zero aborts.++**An earlier reading of this was wrong and is corrected here.** The first+experiment compared "advert on air" against "advert off" and concluded that+advertising and initiating could not coexist without the kernel's Simultaneous+Central and Peripheral feature. That comparison was confounded: the advert was+only ever off while the *daemon was stopped*, so it was also the only condition+in which nothing was rewriting `PreferredBearer`. The advert was not shown to+interfere, and no evidence here says it does.++Tether still sequences the two -- the solicitation stays off air while outbound+attempts are being spent (`BearerStatus::le_dialling`) -- because doing one thing+at a time is cheap now that the connect succeeds on the first attempt. That is a+conservative choice, not a measured requirement.+### PBAP`Select("int", "pb")` followed by `PullAll` with `Format: vcard30` and `MaxCount` returned 456 contacts on the first attempt.diff --git a/src/core/include/tether/bluetooth/bearer_supervisor.hpp b/src/core/include/tether/bluetooth/bearer_supervisor.hppindex 1769714..f208bc2 100644--- a/src/core/include/tether/bluetooth/bearer_supervisor.hpp+++ b/src/core/include/tether/bluetooth/bearer_supervisor.hpp@@ -13,6 +13,9 @@ namespace tether::bluetooth {inline constexpr int CLASSIC_FAILURES_BEFORE_ADVICE = 6;inline constexpr int LE_ATTEMPTS_BEFORE_ADVICE = 6;+ inline constexpr int LE_DIAL_WINDOW_SECONDS = 45;+ inline constexpr int LE_SOLICIT_WINDOW_SECONDS = 180;+// How long an ANCS session is held across an LE outage before the path is// dropped. The bearer flaps faster than a GATT subscription can be rebuilt,// so a blip must not reset discovery, while a genuine loss still clears.@@ -53,6 +56,7 @@ namespace tether::bluetooth {bool classic_connected = false;bool le_connected = false;bool le_available = false;+ bool le_dialling = false;int classic_backoff = 0;int le_backoff = 0;std::string reason;@@ -86,8 +90,10 @@ namespace tether::bluetooth {int classic_failures_ = 0;int le_failures_ = 0;bool le_listening_ = false;+ bool le_phone_silent_ = false;bool le_stuck_locally_ = false;int64_t classic_connected_since_ = -1;+ int64_t le_down_since_ = -1;int64_t next_classic_attempt_ = 0;int64_t next_le_attempt_ = 0;};diff --git a/src/core/src/bluetooth/bearer_supervisor.cpp b/src/core/src/bluetooth/bearer_supervisor.cppindex 8611f8b..50fd26a 100644--- a/src/core/src/bluetooth/bearer_supervisor.cpp+++ b/src/core/src/bluetooth/bearer_supervisor.cpp@@ -66,6 +66,8 @@ namespace tether::bluetooth {classic_failures_ = 0;le_failures_ = 0;le_listening_ = false;+ le_phone_silent_ = false;+ le_down_since_ = -1;le_stuck_locally_ = false;classic_connected_since_ = -1;next_classic_attempt_ = 0;@@ -91,7 +93,14 @@ namespace tether::bluetooth {status_.classic_connected = ops_.classic_connected();status_.le_available = ops_.le_bearer_available();status_.le_connected = status_.le_available && ops_.le_connected();+ if (status_.le_connected)+ le_down_since_ = -1;+ else if (le_down_since_ < 0)+ le_down_since_ = now;+if (status_.le_connected) {+ // Neither route is in use once the link is up.+ status_.le_dialling = false;le_failures_ = 0;le_stuck_locally_ = false;status_.le_backoff = 0;@@ -159,17 +168,17 @@ namespace tether::bluetooth {}if (!status_.le_connected) {+ // alternate the two routes rather than running both+ const int64_t phase = (now - le_down_since_) % (LE_DIAL_WINDOW_SECONDS + LE_SOLICIT_WINDOW_SECONDS);+ status_.le_dialling = phase < LE_DIAL_WINDOW_SECONDS;+const bool settled =classic_connected_since_ >= 0 && now - classic_connected_since_ >= BEARER_SETTLE_SECONDS;- if (settled && !ops_.le_connect_outstanding() && le_failures_ < LE_ATTEMPTS_BEFORE_ADVICE &&- now >= next_le_attempt_) {- // Select LE only for this attempt, then hand the preference back- // so LE is never left preferred while idle.- ops_.set_preferred_bearer("le");+ if (settled && status_.le_dialling && !ops_.le_connect_outstanding() &&+ le_failures_ < LE_ATTEMPTS_BEFORE_ADVICE && now >= next_le_attempt_) {std::string err;const ConnectResult result = classify(ops_.connect_le(err), err);- ops_.set_preferred_bearer("bredr");switch (result) {case ConnectResult::Requested:@@ -183,6 +192,7 @@ namespace tether::bluetooth {++le_failures_;le_listening_ = false;le_stuck_locally_ = false;+ le_phone_silent_ = is_transient(err);status_.le_backoff = next_backoff(status_.le_backoff, err);next_le_attempt_ = now + status_.le_backoff;debug::log(WARN, "bluetooth: LE connect failed ({}), retrying in {}s", err, status_.le_backoff);@@ -194,6 +204,11 @@ namespace tether::bluetooth {status_.reason = "Connected. Waiting for the iPhone to open the LE link that carries ""notifications. If it does not, open Settings > Bluetooth > (i) on the ""iPhone and check Share System Notifications.";+ else if (le_phone_silent_)+ status_.reason = "The iPhone is not answering on LE. Its Bluetooth is wedged on its own "+ "side: turn Bluetooth off and back on on the iPhone, which clears this even "+ "when Share System Notifications is already on. Messages and contacts are "+ "unaffected.";elsestatus_.reason = "Nothing is listening for the iPhone's LE link. BlueZ refused to register ""for it, which only \"sudo systemctl restart bluetooth\" clears. Messages "diff --git a/src/core/src/bluetooth/connection.cpp b/src/core/src/bluetooth/connection.cppindex 311389f..2e98f91 100644--- a/src/core/src/bluetooth/connection.cpp+++ b/src/core/src/bluetooth/connection.cpp@@ -348,6 +348,8 @@ namespace tether::bluetooth {// Some BlueZ builds publish Bearer.LE1 without Connect; the first// reply says which, and every later attempt uses the one that// exists.+ if (le_->bearer_connect_missing)+ set_preferred_bearer("le");const char* iface = le_->bearer_connect_missing ? IFACE_DEVICE : IFACE_BEARER_LE;auto* holder = new std::shared_ptr<LeConnect>(le_);const std::string path = device->path;@@ -893,8 +895,9 @@ namespace tether::bluetooth {profiles->tick(now, link_ready);const auto& bearer = bearers->status();- supervise_ancs_solicitation(- *monitor, ancs_enabled && bearer.classic_connected && bearer.le_available && !bearer.le_connected);+ supervise_ancs_solicitation(*monitor,+ ancs_enabled && bearer.classic_connected && bearer.le_available &&+ !bearer.le_connected && !bearer.le_dialling);// publish() drops a payload identical to the last one, so refreshing// every tick costs nothing and cannot miss a transition.diff --git a/test/test_bluetooth_supervisor.cpp b/test/test_bluetooth_supervisor.cppindex 14bea53..9728dfa 100644--- a/test/test_bluetooth_supervisor.cpp+++ b/test/test_bluetooth_supervisor.cpp@@ -111,26 +111,25 @@ TEST(BearerSupervisor, ConnectsClassicThenLeAfterSettling) {EXPECT_TRUE(sup.status().le_connected);}-// PreferredBearer is an instruction for the next connection, not a durable-// setting. Leaving LE preferred while idle changes later reconnect behavior.-TEST(BearerSupervisor, NeverLeavesLePreferredWhileIdle) {+// PreferredBearer steers Device1.Connect, but the LE path calls the per-bearer+// Connect, which already names the transport. Writing "le" and then "bredr" back+// milliseconds later raced the asynchronous connect that was still in flight and+// produced le-connection-abort-by-local, so the LE path must not touch it at all.+TEST(BearerSupervisor, DoesNotSteerPreferredBearerWhenDiallingLe) {FakeBearer ops;BearerSupervisor sup(ops, true);sup.tick(0);sup.tick(1);sup.tick(1 + BEARER_SETTLE_SECONDS);+ ASSERT_EQ(ops.le_attempts, 1) << "the LE attempt this is about never happened";+ for (const auto& choice : ops.preferred)+ EXPECT_NE(choice, "le") << "raced the async LE connect by rewriting PreferredBearer under it";++ // Classic still selects it, and must leave it there rather than clearing it.ASSERT_FALSE(ops.preferred.empty());EXPECT_EQ(ops.preferred.back(), "bredr");-- // Every "le" selection must be immediately followed by a return to "bredr".- for (size_t i = 0; i < ops.preferred.size(); ++i) {- if (ops.preferred[i] == "le") {- ASSERT_LT(i + 1, ops.preferred.size()) << "left LE preferred at the end";- EXPECT_EQ(ops.preferred[i + 1], "bredr");- }- }}TEST(BearerSupervisor, ClassicBackoffGrowsAndCaps) {@@ -526,8 +525,8 @@ TEST(BearerSupervisor, HoldsOneLeRegistrationRatherThanRepeatingIt) {sup.tick(now);EXPECT_EQ(listening.le_attempts, 1);- // Hours of ticks while BlueZ is listening must not add a second one.- for (int i = 0; i < 500; ++i)+ // Ticks throughout the dial window must not add a second one.+ for (int i = 0; i < LE_DIAL_WINDOW_SECONDS - BEARER_SETTLE_SECONDS - 3; ++i)sup.tick(++now);EXPECT_EQ(listening.le_attempts, 1) << "re-registered on top of a live registration";EXPECT_NE(sup.status().reason.find("Waiting for the iPhone to open the LE link"), std::string::npos);@@ -587,6 +586,103 @@ TEST(BearerSupervisor, ARefusedRegistrationSaysNothingIsListening) {EXPECT_GT(sup.status().le_backoff, BEARER_BACKOFF_MIN_SECONDS) << "a refusal must still back off";}+// The two routes to an LE link cannot run together. Measured 2026-08-19 with the+// PreferredBearer race already removed: advert off -> connects first attempt, zero+// aborts; advert on -> four le-connection-abort-by-local and no link. So they+// alternate, and the phase has to be bounded by time rather than by failures --+// an outstanding registration suppresses new attempts, so failures stop accruing+// and a failure-counted phase would never end, leaving the advert off forever.+TEST(BearerSupervisor, AlternatesBetweenDiallingAndSoliciting) {+ class ListeningBearer : public FakeBearer {+ public:+ ConnectResult connect_le(std::string&) override {+ ++le_attempts;+ outstanding = true; // BlueZ holds the registration, as it really does+ return ConnectResult::Requested;+ }+ } ops;++ BearerSupervisor sup(ops, true);+ int64_t now = 0;+ sup.tick(now);+ sup.tick(++now);+ now += BEARER_SETTLE_SECONDS;+ sup.tick(now);+ EXPECT_TRUE(sup.status().le_dialling) << "the dial window should come first";+ EXPECT_EQ(ops.le_attempts, 1);++ // The registration never completes. The phase must still end.+ now += LE_DIAL_WINDOW_SECONDS;+ sup.tick(now);+ EXPECT_FALSE(sup.status().le_dialling) << "deadlocked: an open registration froze the dial phase";++ // No connect may be issued anywhere in the solicitation window.+ const int attempts = ops.le_attempts;+ const int64_t cycle = LE_DIAL_WINDOW_SECONDS + LE_SOLICIT_WINDOW_SECONDS;+ while (now < cycle - 2) {+ sup.tick(++now);+ ASSERT_FALSE(sup.status().le_dialling) << "left the solicitation window early, at t=" << now;+ }+ EXPECT_EQ(ops.le_attempts, attempts) << "dialled while the solicitation was broadcasting";++ // ...and then it comes back around to dialling.+ now = cycle + 1;+ sup.tick(now);+ EXPECT_TRUE(sup.status().le_dialling) << "never returned to the dial route";+}++// A link that is up needs neither route, so nothing should be suppressed by it.+TEST(BearerSupervisor, NotDiallingOnceLeIsUp) {+ FakeBearer ops;+ BearerSupervisor sup(ops, true);++ sup.tick(0);+ sup.tick(1);+ sup.tick(1 + BEARER_SETTLE_SECONDS);+ sup.tick(2 + BEARER_SETTLE_SECONDS);+ ASSERT_TRUE(sup.status().le_connected);+ EXPECT_FALSE(sup.status().le_dialling);+}++// A dead LE link has two very different causes. A timeout is the phone ignoring+// us, which only cycling Bluetooth on the phone clears; a refusal is BlueZ+// declining to register, which needs bluetoothd restarted. Sharing one message+// sent debugging to the wrong machine.+TEST(BearerSupervisor, TellsPhoneSilenceApartFromABlueZRefusal) {+ FakeBearer silent;+ silent.le_succeeds = false;+ silent.le_error = "GDBus.Error:org.bluez.Error.Failed: Connection timed out";+ BearerSupervisor a(silent, true);+ int64_t now = 0;+ a.tick(now);+ a.tick(++now);+ now += BEARER_SETTLE_SECONDS;+ while (silent.le_attempts < LE_ATTEMPTS_BEFORE_ADVICE) {+ a.tick(now);+ now += BEARER_BACKOFF_MAX_SECONDS;+ }+ a.tick(now);+ EXPECT_NE(a.status().reason.find("not answering on LE"), std::string::npos);+ EXPECT_NE(a.status().reason.find("turn Bluetooth off and back on on the iPhone"), std::string::npos);+ EXPECT_EQ(a.status().reason.find("systemctl"), std::string::npos) << "blamed the local stack for the phone";++ FakeBearer refused;+ refused.le_succeeds = false;+ refused.le_error = "org.bluez.Error.AuthenticationFailed";+ BearerSupervisor b(refused, true);+ now = 0;+ b.tick(now);+ b.tick(++now);+ now += BEARER_SETTLE_SECONDS;+ while (refused.le_attempts < LE_ATTEMPTS_BEFORE_ADVICE) {+ b.tick(now);+ now += BEARER_BACKOFF_MAX_SECONDS;+ }+ b.tick(now);+ EXPECT_NE(b.status().reason.find("Nothing is listening"), std::string::npos);+ EXPECT_EQ(b.status().reason.find("not answering"), std::string::npos);+}+// A refusal from the phone is a real answer, and repeating it every five seconds// helps nobody.TEST(BearerSupervisor, PersistentFailuresStillEscalate) {