Skip to content

Linux + iPhone Continuity / iMessage / SMS

fix: bt scanning

Zack Bartel4a4af9b65551
diff --git a/README.md b/README.md
index 3d98774..a64ed4e 100644
--- a/README.md
+++ b/README.md
@@ -141,7 +141,8 @@ make install
3. Bluetooth pair via GUI or CLI (for Messages and Notifications):
Check that your machine has the Bluetooth features first.
- This sets the Bluetooth adapter class to "A/V Hands-Free" instead of "Computer"
+
+ This sets the Bluetooth adapter class to "A/V Hands-Free" instead of "Computer":
```bash
./scripts/bt-probe.sh --set-class
diff --git a/src/core/include/tether/bluetooth/pairing.hpp b/src/core/include/tether/bluetooth/pairing.hpp
index 3e8591c..14723c0 100644
--- a/src/core/include/tether/bluetooth/pairing.hpp
+++ b/src/core/include/tether/bluetooth/pairing.hpp
@@ -43,6 +43,10 @@ namespace tether::bluetooth {
// Removes the bond so a clean pairing can be retried.
PairResult unpair_device(BluezMonitor& monitor, const std::string& address);
+ // Runs a BlueZ discovery for `seconds`, calling `on_tick` about once a second
+ // so a caller can publish the growing device list.
+ bool scan_devices(BluezMonitor& monitor, int seconds, const std::function<void()>& on_tick, std::string& err);
+
// Puts the ANCS solicitation advertisement back on air without re-pairing.
// This is what makes iOS reveal its "Show Message Notifications" and
// "Sync Contacts" toggles, and it expires a few minutes after pairing, so
diff --git a/src/core/src/bluetooth/pairing.cpp b/src/core/src/bluetooth/pairing.cpp
index b243502..3277bd1 100644
--- a/src/core/src/bluetooth/pairing.cpp
+++ b/src/core/src/bluetooth/pairing.cpp
@@ -108,7 +108,10 @@ namespace tether::bluetooth {
return false;
}
- bool call_adapter(GDBusConnection* conn, const std::string& path, const char* method) {
+ bool call_adapter(GDBusConnection* conn,
+ const std::string& path,
+ const char* method,
+ std::string* err_out = nullptr) {
GError* error = nullptr;
GVariant* reply = g_dbus_connection_call_sync(conn,
BLUEZ_NAME,
@@ -122,7 +125,10 @@ namespace tether::bluetooth {
nullptr,
&error);
if (!reply) {
- debug::log(WARN, "bluetooth: {} failed: {}", method, error ? error->message : "unknown");
+ const std::string message = error && error->message ? error->message : "unknown";
+ debug::log(WARN, "bluetooth: {} failed: {}", method, message);
+ if (err_out)
+ *err_out = message;
g_clear_error(&error);
return false;
}
@@ -130,6 +136,32 @@ namespace tether::bluetooth {
return true;
}
+ bool get_adapter_bool(GDBusConnection* conn, const std::string& adapter, const char* name) {
+ GError* error = nullptr;
+ GVariant* reply = g_dbus_connection_call_sync(conn,
+ BLUEZ_NAME,
+ adapter.c_str(),
+ IFACE_PROPS,
+ "Get",
+ g_variant_new("(ss)", IFACE_ADAPTER, name),
+ G_VARIANT_TYPE("(v)"),
+ G_DBUS_CALL_FLAGS_NONE,
+ 5000,
+ nullptr,
+ &error);
+ if (!reply) {
+ g_clear_error(&error);
+ return false;
+ }
+ GVariant* boxed = nullptr;
+ g_variant_get(reply, "(v)", &boxed);
+ const bool value = boxed && g_variant_get_boolean(boxed);
+ if (boxed)
+ g_variant_unref(boxed);
+ g_variant_unref(reply);
+ return value;
+ }
+
// Scans until the device shows up. Needed because an unpair makes BlueZ
// forget the device entirely, which would otherwise leave no way to pair
// again from inside the app.
@@ -524,6 +556,49 @@ namespace tether::bluetooth {
return result;
}
+ bool scan_devices(BluezMonitor& monitor, int seconds, const std::function<void()>& on_tick, std::string& err) {
+ GDBusConnection* conn = monitor.connection();
+ if (!conn) {
+ err = "Bluetooth is unavailable.";
+ return false;
+ }
+
+ auto objects = monitor.snapshot();
+ if (objects.adapters.empty()) {
+ err = "No Bluetooth adapter is present.";
+ return false;
+ }
+ const std::string adapter = objects.adapters.front().path;
+
+ // InProgress means BlueZ believes a discovery is already running.
+ std::string start_err;
+ bool owns_discovery = call_adapter(conn, adapter, "StartDiscovery", &start_err);
+ if (!owns_discovery) {
+ if (start_err.find("InProgress") == std::string::npos) {
+ err = start_err.empty() ? "BlueZ refused to start scanning." : start_err;
+ return false;
+ }
+ if (!get_adapter_bool(conn, adapter, "Discovering")) {
+ err = "BlueZ is holding a discovery session that never ended, so scanning cannot start. "
+ "Restart the Bluetooth service (sudo systemctl restart bluetooth) and try again.";
+ return false;
+ }
+ }
+
+ const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(seconds);
+ while (std::chrono::steady_clock::now() < deadline) {
+ std::this_thread::sleep_for(std::chrono::seconds(1));
+ if (on_tick)
+ on_tick();
+ }
+
+ // A discovery left running blocks the pairing transaction that usually
+ // follows it, so it stops even when the caller loses interest.
+ if (owns_discovery)
+ call_adapter(conn, adapter, "StopDiscovery");
+ return true;
+ }
+
PairResult unpair_device(BluezMonitor& monitor, const std::string& address) {
PairResult result;
result.device_address = normalize_address(address);
diff --git a/src/core/src/net.cpp b/src/core/src/net.cpp
index 9c99191..1d4f30b 100644
--- a/src/core/src/net.cpp
+++ b/src/core/src/net.cpp
@@ -277,6 +277,38 @@ namespace tether {
// advertisement object paths.
static std::atomic<bool> g_bt_pair_busy{false};
+ // A second StartDiscovery while one is running just gets stopped early by the
+ // first one's StopDiscovery.
+ static std::atomic<bool> g_bt_scan_busy{false};
+
+ static constexpr int BT_SCAN_SECONDS = 20;
+
+ static void run_bt_scan() {
+ nlohmann::json event;
+ event["command"] = "bt_scan_result";
+
+ if (g_bt_scan_busy.exchange(true)) {
+ event["success"] = false;
+ event["message"] = "A Bluetooth scan is already running.";
+ broadcast_local_event(event.dump());
+ return;
+ }
+
+ std::string err;
+ // Each tick republishes the device list, so the UI fills in as the phone
+ // shows up rather than only at the end of the scan.
+ const bool ok =
+ bluetooth::g_bluez &&
+ bluetooth::scan_devices(
+ *bluetooth::g_bluez, BT_SCAN_SECONDS, []() { broadcast_local_event(build_bt_devices().dump()); }, err);
+
+ g_bt_scan_busy = false;
+ event["success"] = ok;
+ event["message"] = ok ? "Bluetooth scan finished." : (err.empty() ? "Bluetooth is unavailable." : err);
+ broadcast_local_event(build_bt_devices().dump());
+ broadcast_local_event(event.dump());
+ }
+
static void run_bt_pair(const std::string& address) {
if (!bluetooth::g_bluez) {
nlohmann::json event;
@@ -677,6 +709,8 @@ namespace tether {
std::string payload = build_bt_status().dump() + "\n";
write_plain_packet(client_fd, payload);
continue;
+ } else if (j.contains("command") && j["command"] == "bt_scan") {
+ std::thread(run_bt_scan).detach();
} else if (j.contains("command") && j["command"] == "bt_list_devices") {
std::string payload = build_bt_devices().dump() + "\n";
write_plain_packet(client_fd, payload);
diff --git a/src/gtk/devices_view.cpp b/src/gtk/devices_view.cpp
index cca2875..973cb74 100644
--- a/src/gtk/devices_view.cpp
+++ b/src/gtk/devices_view.cpp
@@ -384,10 +384,10 @@ namespace tether::ui {
void devices_view_trigger_discovery() {
set_status_main("Scanning for nearby devices...");
- // Refresh means both routes: mDNS for Wi-Fi peers, BlueZ for Bluetooth.
+ // Refresh means both routes: mDNS for Wi-Fi peers, and a real BlueZ discovery for Bluetooth.
daemon_send({{"command", "discover"}});
daemon_send({{"command", "bt_status"}});
- daemon_send({{"command", "bt_list_devices"}});
+ daemon_send({{"command", "bt_scan"}});
}
// Refresh the device list based on Discovered (unpaired), Paired (offline), Paired (online)
@@ -737,6 +737,20 @@ namespace tether::ui {
// Notifications views need the same event.
return false;
}
+ if (command == "bt_scan_result") {
+ const std::string message = event.value("message", "");
+ set_status_main(message);
+ set_text(g_devices.lbl_bt_progress, message);
+ // A failed scan already carries the reason; only a scan that really
+ // ran and found nothing wants the "check the phone" advice.
+ if (event.value("success", false) && g_devices.bt_devices.empty())
+ set_text(g_devices.lbl_welcome_bt,
+ "No iPhone found. Unlock the phone and open Settings > Bluetooth so it advertises, "
+ "then scan again.");
+ else if (!event.value("success", false))
+ set_text(g_devices.lbl_welcome_bt, message);
+ return true;
+ }
if (command == "bt_solicit_result") {
set_text(g_devices.lbl_bt_progress, event.value("message", ""));
return false;