Fix Bluetooth discovery so scanned devices are listed
This commit is contained in:
1 parent
5d859d7b11
commit
6a6222c198
9 files changed
+276
-7
No files matched your search
@@ -245,7 +245,13 @@ bool isWifiEnabledFast() {
|
||||
|
||||
bool isBtEnabledFast() {
|
||||
if (::access("/run/bluetoothd.pid", F_OK) != 0) {
|
||||
return false;
|
||||
// supervise-daemon normally writes /run/bluetoothd.pid, but a manually
|
||||
// started bluetoothd (or a different OpenRC supervisor) may not. Ask
|
||||
// BlueZ directly before declaring the controller absent.
|
||||
std::string show = runCaptureCmd("timeout 2 bluetoothctl show 2>/dev/null");
|
||||
if (show.find("Powered:") == std::string::npos ||
|
||||
show.find("Powered: no") != std::string::npos)
|
||||
return false;
|
||||
}
|
||||
std::string out = runCaptureCmd("/usr/sbin/rfkill list bluetooth 2>/dev/null");
|
||||
if (out.find("Soft blocked: yes") != std::string::npos ||
|
||||
@@ -420,7 +426,87 @@ const char* iconForBtDevice(const BtDeviceEntry& d) {
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<BtDeviceEntry> queryBtDevices() {
|
||||
// BlueZ discovery is tied to the D-Bus client that called StartDiscovery.
|
||||
// A short-lived `bluetoothctl scan on` can therefore drop every unpaired
|
||||
// device as soon as it exits, and a following `bluetoothctl devices` query
|
||||
// (from a different process) legitimately returns an empty list. Keep the
|
||||
// scan stream itself: it contains "[NEW] Device AA:BB:... Name" and
|
||||
// "[CHG] Device AA:BB:... Name: ..." lines for every device seen.
|
||||
std::string stripBtAnsi(const std::string& in) {
|
||||
std::string out;
|
||||
out.reserve(in.size());
|
||||
for (size_t i = 0; i < in.size();) {
|
||||
if (in[i] == '\033' && i + 1 < in.size() && in[i + 1] == '[') {
|
||||
size_t j = i + 2;
|
||||
while (j < in.size() &&
|
||||
(std::isdigit(static_cast<unsigned char>(in[j])) || in[j] == ';' ||
|
||||
in[j] == '?' || in[j] == '!' || in[j] == '<' || in[j] == '>' ||
|
||||
in[j] == '='))
|
||||
++j;
|
||||
if (j < in.size())
|
||||
++j;
|
||||
i = j;
|
||||
continue;
|
||||
}
|
||||
if (in[i] != '\r')
|
||||
out.push_back(in[i]);
|
||||
++i;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
bool isBluetoothMac(const std::string& s) {
|
||||
if (s.size() != 17)
|
||||
return false;
|
||||
for (size_t i = 0; i < s.size(); ++i) {
|
||||
if (i % 3 == 2) {
|
||||
if (s[i] != ':')
|
||||
return false;
|
||||
} else if (!std::isxdigit(static_cast<unsigned char>(s[i]))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<BtDeviceEntry> parseBtScanDevices(const std::string& raw) {
|
||||
std::vector<BtDeviceEntry> out;
|
||||
std::unordered_map<std::string, size_t> byMac;
|
||||
std::istringstream ss(stripBtAnsi(raw));
|
||||
std::string line;
|
||||
while (std::getline(ss, line)) {
|
||||
size_t pos = line.find("Device ");
|
||||
if (pos == std::string::npos)
|
||||
continue;
|
||||
std::string mac = trimStr(line.substr(pos + 7, 17));
|
||||
if (!isBluetoothMac(mac))
|
||||
continue;
|
||||
bool removed = (line.find("[DEL]") != std::string::npos);
|
||||
std::string rest = trimStr(line.substr(pos + 7 + 17));
|
||||
std::string name;
|
||||
if (rest.rfind("Name:", 0) == 0 || rest.rfind("Alias:", 0) == 0) {
|
||||
name = trimStr(rest.substr(rest.find(':') + 1));
|
||||
} else if (!rest.empty() && rest.find(':') == std::string::npos &&
|
||||
rest.rfind("RSSI", 0) != 0 && rest.rfind("TxPower", 0) != 0) {
|
||||
name = rest;
|
||||
}
|
||||
auto it = byMac.find(mac);
|
||||
if (it == byMac.end()) {
|
||||
if (removed)
|
||||
continue;
|
||||
BtDeviceEntry d;
|
||||
d.mac = mac;
|
||||
d.name = name.empty() ? mac : name;
|
||||
byMac[mac] = out.size();
|
||||
out.push_back(d);
|
||||
} else if (!name.empty()) {
|
||||
out[it->second].name = name;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::vector<BtDeviceEntry> queryBtDevices(const std::string& scanRaw = "") {
|
||||
std::vector<BtDeviceEntry> out;
|
||||
std::unordered_set<std::string> pairedMacs;
|
||||
std::unordered_set<std::string> connectedMacs;
|
||||
@@ -483,6 +569,21 @@ std::vector<BtDeviceEntry> queryBtDevices() {
|
||||
if (e.name.size() == 17 && e.name[2] == '-' && e.name[5] == '-') return true;
|
||||
return false;
|
||||
};
|
||||
// Merge what the scan actually saw with the persistent device list. BlueZ
|
||||
// may have dropped unpaired devices when the discovery client exited, so
|
||||
// the scan stream is the authoritative source for a fresh Scan action.
|
||||
for (const auto& found : parseBtScanDevices(scanRaw)) {
|
||||
bool known = false;
|
||||
for (auto& d : out) {
|
||||
if (d.mac != found.mac) continue;
|
||||
known = true;
|
||||
if (isRawMacName(d) && !isRawMacName(found))
|
||||
d.name = found.name;
|
||||
break;
|
||||
}
|
||||
if (!known)
|
||||
out.push_back(found);
|
||||
}
|
||||
std::sort(out.begin(), out.end(),
|
||||
[&](const BtDeviceEntry& a, const BtDeviceEntry& b) {
|
||||
if (a.connected != b.connected) return a.connected > b.connected;
|
||||
@@ -802,6 +903,7 @@ int runNetworksApp(int argc, char** argv) {
|
||||
cellInfo = queryCellularInfo();
|
||||
} else if (activeTab == NetTab::Bluetooth) {
|
||||
btOn = isBtEnabledFast();
|
||||
std::string scanRaw;
|
||||
if (btOn) {
|
||||
int obexPid = readIntFile("/tmp/bananarchy_bt_obex.pid");
|
||||
if (obexPid <= 0 || ::kill(obexPid, 0) != 0) {
|
||||
@@ -809,9 +911,21 @@ int runNetworksApp(int argc, char** argv) {
|
||||
}
|
||||
btRx = readBtRxStatus();
|
||||
if (forceScan) {
|
||||
runCaptureCmd("timeout 16 bluetoothctl --timeout 15 scan on >/dev/null 2>&1");
|
||||
// Keep the scan output: BlueZ discovery is client-scoped, so a
|
||||
// separate `bluetoothctl devices` call after the scan can be empty.
|
||||
// Also power the controller on when bluetoothd left it down, which
|
||||
// is common on the WCNSS controller of the 2720 Flip.
|
||||
std::string show = runCaptureCmd(
|
||||
"timeout 2 bluetoothctl show 2>/dev/null");
|
||||
if (show.find("Powered: no") != std::string::npos ||
|
||||
show.find("Powered:") == std::string::npos) {
|
||||
runCaptureCmd("timeout 2 bluetoothctl power on >/dev/null 2>&1");
|
||||
}
|
||||
scanRaw = runCaptureCmd(
|
||||
"timeout 16 bluetoothctl --timeout 15 scan on 2>/dev/null || "
|
||||
"timeout 12 bluetoothctl scan on 2>/dev/null || true");
|
||||
}
|
||||
btList = queryBtDevices();
|
||||
btList = queryBtDevices(scanRaw);
|
||||
} else {
|
||||
btList.clear();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user