Skip to content

Linux + iPhone Continuity / iMessage / SMS

feat: disable bluetooth connection (but keep bond)

Zack Bartel5d9a64d9766a
diff --git a/src/cli/main.cpp b/src/cli/main.cpp
index b90b01f..afc68fc 100644
--- a/src/cli/main.cpp
+++ b/src/cli/main.cpp
@@ -118,6 +118,7 @@ void print_help() {
<< " --bt-unpair <addr> Remove a Bluetooth bond.\n"
<< " --bt-solicit Re-advertise for ANCS so the iPhone shows its permission\n"
<< " toggles again, without removing the bond.\n"
+ << " --bt-enable <on|off> Connect to the iPhone over Bluetooth, or stop.\n"
<< " --bt-ancs <on|off> Turn notification mirroring on or off.\n"
<< " --bt-ancs-content <on|off> Mirror notification titles and bodies, not just the app.\n"
<< " --bt-notifications List mirrored iPhone notifications.\n"
@@ -206,6 +207,7 @@ static int print_bt_status(tether::Client& client) {
: "unknown");
if (cap.value("bonded_device_present", false))
fprintf(stdout, "Bond: %s\n", cap.value("bond_has_le", false) ? "BR/EDR + LE" : "BR/EDR only");
+ fprintf(stdout, "Tether: %s\n", resp.value("enabled", true) ? "connecting" : "off (--bt-enable on)");
for (const auto& adapter : resp["adapters"]) {
fprintf(stdout, " %s %s\n", adapter.value("address", "").c_str(), adapter.value("name", "").c_str());
@@ -574,6 +576,10 @@ int main(int argc, char* argv[]) {
arg_val2 = argv[++i];
} else if (arg == "--bt-solicit") {
action = "bt_solicit";
+ } else if (arg == "--bt-enable") {
+ action = "bt_enable";
+ if (i + 1 < argc && argv[i + 1][0] != '-')
+ arg_val = argv[++i];
} else if (arg == "--bt-ancs") {
action = "bt_ancs";
if (i + 1 < argc && argv[i + 1][0] != '-')
@@ -744,6 +750,25 @@ int main(int argc, char* argv[]) {
return run_bt_transaction(client, action == "bt_pair" ? "bt_pair" : "bt_unpair", arg_val);
} else if (action == "bt_solicit") {
return run_bt_transaction(client, "bt_solicit");
+ } else if (action == "bt_enable") {
+ if (arg_val != "on" && arg_val != "off") {
+ debug::log(ERR, "Expected on or off, e.g. --bt-enable off\n");
+ return 1;
+ }
+ nlohmann::json request;
+ request["command"] = "bt_set_enabled";
+ request["enabled"] = arg_val == "on";
+ if (!client.send(request.dump() + "\n")) {
+ debug::log(ERR, "Could not reach the daemon.\n");
+ return 1;
+ }
+ if (arg_val == "on") {
+ fprintf(stdout, "Bluetooth enabled.\n");
+ } else {
+ fprintf(stdout,
+ "Bluetooth disabled. Tether will not reconnect the iPhone. A link that is\n"
+ "already up stays up until you disconnect it or the phone goes out of range.\n");
+ }
} else if (action == "bt_ancs" || action == "bt_ancs_content") {
const bool content = action == "bt_ancs_content";
if (arg_val != "on" && arg_val != "off") {
diff --git a/src/core/include/tether/bluetooth/config.hpp b/src/core/include/tether/bluetooth/config.hpp
index d6c2236..82cae34 100644
--- a/src/core/include/tether/bluetooth/config.hpp
+++ b/src/core/include/tether/bluetooth/config.hpp
@@ -29,6 +29,8 @@ namespace tether::bluetooth {
bool ancs_content_enabled = true;
// Group replies are off until deliberately enabled
bool group_messages_enabled = false;
+ // When off, supervision runs against no device, so the daemon stops re-dialling
+ bool enabled = true;
bool operator==(const Config&) const = default;
};
@@ -41,6 +43,9 @@ namespace tether::bluetooth {
Config load_config();
bool save_config(const Config& config);
+ // The address supervision should run against: none while Bluetooth is off.
+ std::string supervised_address(const Config& config);
+
// exposed for tests without touching fs
std::string serialize_config(const Config& config);
Config deserialize_config(const std::string& text);
diff --git a/src/core/src/bluetooth/config.cpp b/src/core/src/bluetooth/config.cpp
index d809b07..849fc5d 100644
--- a/src/core/src/bluetooth/config.cpp
+++ b/src/core/src/bluetooth/config.cpp
@@ -49,6 +49,7 @@ namespace tether::bluetooth {
j["ancs_enabled"] = config.ancs_enabled;
j["ancs_content_enabled"] = config.ancs_content_enabled;
j["group_messages_enabled"] = config.group_messages_enabled;
+ j["enabled"] = config.enabled;
return j.dump(2);
}
@@ -64,12 +65,17 @@ namespace tether::bluetooth {
const int version = j.value("config_version", 0);
config.ancs_content_enabled = version < 1 ? true : j.value("ancs_content_enabled", true);
config.group_messages_enabled = j.value("group_messages_enabled", false);
+ config.enabled = j.value("enabled", true);
} catch (const std::exception&) {
// A corrupt file must not stop the daemon; defaults are safe.
}
return config;
}
+ std::string supervised_address(const Config& config) {
+ return config.enabled ? config.device_address : std::string{};
+ }
+
Config load_config() {
std::string path = config_path();
if (path.empty())
diff --git a/src/core/src/bluetooth/diagnostics.cpp b/src/core/src/bluetooth/diagnostics.cpp
index df5be72..f9947c9 100644
--- a/src/core/src/bluetooth/diagnostics.cpp
+++ b/src/core/src/bluetooth/diagnostics.cpp
@@ -191,6 +191,7 @@ namespace tether::bluetooth {
report["ancs_enabled"] = config.ancs_enabled;
report["ancs_content_enabled"] = config.ancs_content_enabled;
report["group_messages_enabled"] = config.group_messages_enabled;
+ report["enabled"] = config.enabled;
report["ancs_soliciting"] = ancs_solicitation_active();
report["status"] = redactor.value(status);
report["connection"] = redactor.value(connection);
diff --git a/src/core/src/net.cpp b/src/core/src/net.cpp
index 33fb24e..80c236f 100644
--- a/src/core/src/net.cpp
+++ b/src/core/src/net.cpp
@@ -280,6 +280,7 @@ namespace tether {
status["device_address"] = config.device_address;
status["ancs_enabled"] = config.ancs_enabled;
status["ancs_content_enabled"] = config.ancs_content_enabled;
+ status["enabled"] = config.enabled;
if (!bluetooth::g_bluez) {
status["capability"] = nullptr;
status["adapters"] = nlohmann::json::array();
@@ -375,10 +376,11 @@ namespace tether {
config.ancs_enabled = true;
else if (result.status == "paired")
config.ancs_enabled = false;
+ config.enabled = true;
bluetooth::save_config(config);
// Point supervision at the device we just bonded with.
if (bluetooth::g_bt_connections)
- bluetooth::g_bt_connections->set_device(config.device_address, config.ancs_enabled);
+ bluetooth::g_bt_connections->set_device(bluetooth::supervised_address(config), config.ancs_enabled);
}
g_bt_pair_busy = false;
@@ -743,7 +745,8 @@ namespace tether {
config.device_address = j["address"];
bluetooth::save_config(config);
if (bluetooth::g_bt_connections)
- bluetooth::g_bt_connections->set_device(config.device_address, config.ancs_enabled);
+ bluetooth::g_bt_connections->set_device(bluetooth::supervised_address(config),
+ config.ancs_enabled);
broadcast_local_event(build_bt_status().dump());
} else if (j.contains("command") && j["command"] == "bt_list_threads") {
std::string payload = build_bt_threads().dump() + "\n";
@@ -854,7 +857,16 @@ namespace tether {
config.ancs_enabled = j.value("enabled", true);
bluetooth::save_config(config);
if (bluetooth::g_bt_connections)
- bluetooth::g_bt_connections->set_device(config.device_address, config.ancs_enabled);
+ bluetooth::g_bt_connections->set_device(bluetooth::supervised_address(config),
+ config.ancs_enabled);
+ broadcast_local_event(build_bt_status().dump());
+ } else if (j.contains("command") && j["command"] == "bt_set_enabled") {
+ auto config = bluetooth::load_config();
+ config.enabled = j.value("enabled", true);
+ bluetooth::save_config(config);
+ if (bluetooth::g_bt_connections)
+ bluetooth::g_bt_connections->set_device(bluetooth::supervised_address(config),
+ config.ancs_enabled);
broadcast_local_event(build_bt_status().dump());
} else if (j.contains("command") && j["command"] == "bt_set_ancs_content") {
auto config = bluetooth::load_config();
diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp
index 82d0d01..8b0ba75 100644
--- a/src/daemon/main.cpp
+++ b/src/daemon/main.cpp
@@ -216,7 +216,7 @@ int main(int argc, char** argv) {
});
}
- connections.start(bt_config.device_address, bt_config.ancs_enabled);
+ connections.start(tether::bluetooth::supervised_address(bt_config), bt_config.ancs_enabled);
} else {
debug::log(INFO, "Bluetooth unavailable; messages and notifications are disabled");
}
diff --git a/src/gtk/devices_view.cpp b/src/gtk/devices_view.cpp
index c58c94c..52a27e1 100644
--- a/src/gtk/devices_view.cpp
+++ b/src/gtk/devices_view.cpp
@@ -57,6 +57,7 @@ namespace tether::ui {
GtkWidget* lbl_bt_progress = nullptr;
GtkWidget* btn_bt_pair = nullptr;
GtkWidget* btn_bt_solicit = nullptr;
+ GtkWidget* chk_bt_enabled = nullptr;
GtkWidget* chk_bt_ancs = nullptr;
GtkWidget* chk_bt_content = nullptr;
GtkWidget* btn_bt_unpair = nullptr;
@@ -116,6 +117,7 @@ namespace tether::ui {
set_text(g_devices.lbl_welcome_bt, bt_note);
}
+ void on_bt_enabled_toggled(GtkWidget* widget, gpointer);
void on_bt_ancs_toggled(GtkWidget* widget, gpointer);
void on_bt_content_toggled(GtkWidget* widget, gpointer);
@@ -125,6 +127,7 @@ namespace tether::ui {
const bool paired = device && device->value("paired", false);
const bool supervised = is_supervised_bt_device(address);
const bool available = g_devices.bt_status.value("available", false);
+ const bool bt_on = g_devices.bt_status.value("enabled", true);
set_markup(g_devices.lbl_bt_name, "<b>" + escape_markup(g_devices.selected_bt_name) + "</b>");
@@ -204,6 +207,10 @@ namespace tether::ui {
reason = "Not paired over Bluetooth yet.";
} else if (!supervised) {
reason = "Tether is not using this device. Pair it to select it.";
+ } else if (!bt_on) {
+ // Supervision is idle while switched off, so its status would
+ // otherwise report no device selected.
+ reason = "Bluetooth is switched off for this iPhone.";
} else {
reason = connection.value("profile_reason", "");
if (reason.empty())
@@ -221,9 +228,17 @@ namespace tether::ui {
const bool ancs_on = g_devices.bt_status.value("ancs_enabled", true);
+ gtk_widget_set_visible(g_devices.chk_bt_enabled, available && supervised);
gtk_widget_set_visible(g_devices.chk_bt_ancs, available && supervised);
gtk_widget_set_visible(g_devices.chk_bt_content, available && supervised);
- gtk_widget_set_sensitive(g_devices.chk_bt_content, ancs_on);
+ gtk_widget_set_sensitive(g_devices.chk_bt_ancs, bt_on);
+ gtk_widget_set_sensitive(g_devices.chk_bt_content, bt_on && ancs_on);
+
+ g_signal_handlers_block_by_func(
+ g_devices.chk_bt_enabled, reinterpret_cast<gpointer>(on_bt_enabled_toggled), nullptr);
+ gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(g_devices.chk_bt_enabled), bt_on);
+ g_signal_handlers_unblock_by_func(
+ g_devices.chk_bt_enabled, reinterpret_cast<gpointer>(on_bt_enabled_toggled), nullptr);
g_signal_handlers_block_by_func(
g_devices.chk_bt_ancs, reinterpret_cast<gpointer>(on_bt_ancs_toggled), nullptr);
@@ -359,6 +374,11 @@ namespace tether::ui {
set_text(g_devices.lbl_bt_progress, "Removing the pairing\u2026");
}
+ void on_bt_enabled_toggled(GtkWidget* widget, gpointer) {
+ daemon_send({{"command", "bt_set_enabled"},
+ {"enabled", gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)) == TRUE}});
+ }
+
// The advertisement that makes iOS reveal its Messages and Contacts
// permission toggles expires a few minutes after pairing. Without this the
// only way back to those toggles is to remove the bond and pair again.
@@ -1107,6 +1127,14 @@ namespace tether::ui {
}
gtk_box_pack_start(GTK_BOX(bt_box), capabilities, FALSE, FALSE, 0);
+ g_devices.chk_bt_enabled = gtk_check_button_new_with_label("Connect to this iPhone over Bluetooth");
+ gtk_widget_set_tooltip_text(g_devices.chk_bt_enabled,
+ "Keep the Bluetooth link to the iPhone up, reconnecting whenever it drops. "
+ "Turning this off stops Tether reconnecting; a link that is already up stays "
+ "up until you disconnect it.");
+ g_signal_connect(g_devices.chk_bt_enabled, "toggled", G_CALLBACK(on_bt_enabled_toggled), nullptr);
+ gtk_box_pack_start(GTK_BOX(bt_box), g_devices.chk_bt_enabled, FALSE, FALSE, 0);
+
// pairing that produced no LE half latches mirroring off, and this is the only way back to it without the CLI.
g_devices.chk_bt_ancs = gtk_check_button_new_with_label("Mirror iPhone notifications");
gtk_widget_set_tooltip_text(g_devices.chk_bt_ancs,
diff --git a/test/test_bluetooth_pairing.cpp b/test/test_bluetooth_pairing.cpp
index 4fade67..0575fe5 100644
--- a/test/test_bluetooth_pairing.cpp
+++ b/test/test_bluetooth_pairing.cpp
@@ -46,6 +46,7 @@ TEST(BluetoothConfig, RoundTrips) {
config.device_address = "60:57:C8:30:6A:F7";
config.auth_strategy = AuthStrategy::ExplicitPair;
config.ancs_enabled = false;
+ config.enabled = false;
EXPECT_EQ(deserialize_config(serialize_config(config)), config);
}
@@ -55,6 +56,19 @@ TEST(BluetoothConfig, DefaultsToConnectFirstAndAncsEnabled) {
EXPECT_EQ(config.auth_strategy, AuthStrategy::ConnectFirst);
EXPECT_TRUE(config.ancs_enabled);
EXPECT_TRUE(config.device_address.empty());
+ // A config written before the toggle existed keeps connecting.
+ EXPECT_TRUE(config.enabled);
+}
+
+// Switching Bluetooth off supervises no device, which is what stops the daemon
+// re-dialling a link the user disconnected.
+TEST(BluetoothConfig, SupervisedAddressIsEmptyWhenDisabled) {
+ Config config;
+ config.device_address = "60:57:C8:30:6A:F7";
+ EXPECT_EQ(supervised_address(config), config.device_address);
+
+ config.enabled = false;
+ EXPECT_TRUE(supervised_address(config).empty());
}
// Connect-first is the default for a reason: a Linux-initiated Pair() can yield