Fix bluetoothd CPU busy-loops, cache BT state, and refresh a1120 images

- Add /etc/conf.d/bluetooth (supervise-daemon, retry=TERM/3/KILL/1, --noplugin=hfp,bap,bass,mcp,vcp,micp,csip,ccp,tmap,gmap,asha,midi,neard) and drop gateway UUIDs (111f/1112/110a) from main.conf ReconnectUUIDs so Experimental = true keeps PreferredBearer without BlueZ hfp-hf/ATT EOF GMainLoop spins.

- Set duplicate-data off in bananarchy-bt-scan.sh and update sxmo_bluetoothtoggle.sh to disconnect, btmgmt power off, and clean state files on disable so adapter_stop() never busy-loops on dual-bearer links.

- Add single-shot BlueZ ObjectManager state caching (bt_state.tsv, dump-state, CTRL_READY_FILE, EPHEMERAL_DEV_PROPS filter) in bananarchy-bt-obex and consume it in networks.so instead of per-device bluetoothctl refresh storms.

- Extend Bluetooth host tests and check-rebuild.sh, update docs, and rebuild/pin a1120 (cocojamboo) images and release archives for nokia-argon, nokia-beatles, nokia-coconut, and nokia-sparkler.
This commit is contained in:
Luxferre committed 2026-11-18 06:34:13 +02:00
1 parent 1e1fe7c317
commit 1e7077e314
15 files changed
+924 -282

No files matched your search

+370 -152
View File
@@ -206,6 +206,7 @@ constexpr const char* kObexDecisionPath = "/run/bananarchy/bt-obex/decision.tsv"
constexpr const char* kPairPromptPath = "/run/bananarchy/bt-obex/pair_prompt.tsv";
constexpr const char* kPairDecisionPath = "/run/bananarchy/bt-obex/pair_decision.tsv";
constexpr const char* kBtOpDonePath = "/run/bananarchy/bt-obex/op_done.tsv";
constexpr const char* kBtStateTsvPath = "/run/bananarchy/bt-obex/bt_state.tsv";
std::string shortenHomePath(const std::string& p) {
if (p.empty()) return "~/Downloads";
@@ -705,62 +706,258 @@ std::vector<BtDeviceEntry> parseBtScanDevices(const std::string& raw) {
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;
struct BtRawDevSnapshot {
std::string mac;
std::string name;
bool paired = false;
bool connected = false;
std::string icon;
unsigned long devClass = 0;
bool hasClass = false;
int batteryPct = -1;
std::string uuidsLow;
};
auto parseMacSet = [](const std::string& raw,
std::unordered_set<std::string>& dst) {
std::istringstream ss(raw);
std::string line;
while (std::getline(ss, line)) {
if (line.rfind("Device ", 0) == 0 && line.size() >= 24) {
dst.insert(line.substr(7, 17));
}
struct BtStateSnapshot {
bool valid = false;
bool powered = false;
bool discoverable = false;
std::string alias;
std::vector<BtRawDevSnapshot> devs;
};
std::vector<std::string> splitTabCols(const std::string& line) {
std::vector<std::string> cols;
size_t start = 0;
while (start <= line.size()) {
size_t pos = line.find('\t', start);
if (pos == std::string::npos) {
cols.push_back(line.substr(start));
break;
}
};
cols.push_back(line.substr(start, pos - start));
start = pos + 1;
}
return cols;
}
parseMacSet(runCaptureCmd("timeout 2 bluetoothctl devices Paired 2>/dev/null || "
"timeout 2 bluetoothctl paired-devices 2>/dev/null"),
pairedMacs);
parseMacSet(runCaptureCmd("timeout 2 bluetoothctl devices Connected 2>/dev/null"),
connectedMacs);
std::string paRaw = runCaptureCmd(
"pactl list sinks short 2>/dev/null; pactl list sources short 2>/dev/null; "
"pactl list cards 2>/dev/null");
std::unordered_map<std::string, std::string> cardActiveProfile;
{
std::istringstream pss(paRaw);
std::string pline;
std::string curCard;
while (std::getline(pss, pline)) {
std::string t = trimStr(pline);
if (t.rfind("Name: bluez_card.", 0) == 0) {
curCard = trimStr(t.substr(6));
} else if (!curCard.empty() && t.rfind("Active Profile:", 0) == 0) {
cardActiveProfile[curCard] = trimStr(t.substr(15));
curCard.clear();
BtStateSnapshot parseBtStateTsv(const std::string& raw) {
BtStateSnapshot snap;
std::istringstream ss(raw);
std::string line;
while (std::getline(ss, line)) {
if (!line.empty() && line.back() == '\r') line.pop_back();
if (line.empty()) continue;
std::vector<std::string> cols = splitTabCols(line);
if (cols[0] == "CTRL" && cols.size() >= 3) {
snap.valid = true;
snap.powered = (cols[1] == "1");
snap.discoverable = (cols[2] == "1");
if (cols.size() >= 4) snap.alias = trimStr(cols[3]);
} else if (cols[0] == "DEV" && cols.size() >= 8) {
std::string mac = trimStr(cols[1]);
if (!isBluetoothMac(mac)) continue;
BtRawDevSnapshot dev;
dev.mac = mac;
dev.name = trimStr(cols[2]);
if (dev.name.empty()) dev.name = mac;
dev.paired = (cols[3] == "1");
dev.connected = (cols[4] == "1");
dev.icon = toLowerStr(trimStr(cols[5]));
std::string clsStr = trimStr(cols[6]);
if (!clsStr.empty()) {
char* endp = nullptr;
unsigned long cls = std::strtoul(clsStr.c_str(), &endp, 0);
if (endp != clsStr.c_str() && cls != 0) {
dev.devClass = cls;
dev.hasClass = true;
}
}
std::string batStr = trimStr(cols[7]);
if (!batStr.empty()) {
char* endp = nullptr;
long bp = std::strtol(batStr.c_str(), &endp, 10);
if (endp != batStr.c_str() && bp >= 0 && bp <= 100) {
dev.batteryPct = static_cast<int>(bp);
}
}
if (cols.size() >= 9) dev.uuidsLow = toLowerStr(trimStr(cols[8]));
snap.devs.push_back(std::move(dev));
}
}
return snap;
}
std::string allRaw = runCaptureCmd("timeout 2 bluetoothctl devices 2>/dev/null");
std::istringstream ss(allRaw);
std::string line;
std::string infoCmd;
int infoCount = 0;
BtStateSnapshot loadBtStateSnapshot(bool forceFresh) {
int obexPid = readIntFile("/tmp/bananarchy_bt_obex.pid");
bool obexAlive = (obexPid > 0 && (::kill(obexPid, 0) == 0 || errno == EPERM));
struct stat st = {};
if (!forceFresh && obexAlive &&
::stat(kBtStateTsvPath, &st) == 0 && st.st_size > 0) {
std::ifstream in(kBtStateTsvPath);
if (in.is_open()) {
std::ostringstream oss;
oss << in.rdbuf();
BtStateSnapshot parsed = parseBtStateTsv(oss.str());
if (parsed.valid) return parsed;
}
}
std::string raw = runCaptureCmd(
"timeout 2 /usr/bin/bananarchy-bt-obex dump-state 2>/dev/null");
return parseBtStateTsv(raw);
}
while (std::getline(ss, line)) {
if (line.rfind("Device ", 0) != 0 || line.size() < 24) continue;
BtDeviceEntry d;
d.mac = line.substr(7, 17);
d.name = (line.size() > 25) ? trimStr(line.substr(25)) : d.mac;
d.paired = (pairedMacs.count(d.mac) > 0);
d.connected = (connectedMacs.count(d.mac) > 0);
out.push_back(d);
const BtStateSnapshot* gCachedBtSnapshot = nullptr;
void classifyBtDevice(
BtDeviceEntry& d,
const std::string& paRaw,
const std::unordered_map<std::string, std::string>& cardActiveProfile,
const std::string& iconLow,
unsigned long devClass,
bool hasClass,
const std::string& hintsLow) {
std::string macU = d.mac;
for (char& c : macU) {
if (c == ':') c = '_';
}
bool hasPaSink = (paRaw.find("bluez_output." + macU) != std::string::npos);
bool hasPaSrc = (paRaw.find("bluez_input." + macU) != std::string::npos);
std::string nameLow = toLowerStr(d.name);
bool hasA2dp = (hintsLow.find("0000110b-") != std::string::npos ||
hintsLow.find("audio sink") != std::string::npos);
bool hasHfp = (hintsLow.find("00001108-") != std::string::npos ||
hintsLow.find("0000111e-") != std::string::npos);
bool hasHid = (hintsLow.find("00001124-") != std::string::npos ||
hintsLow.find("00001812-") != std::string::npos ||
hintsLow.find("human interface device") != std::string::npos);
unsigned long majorClass = hasClass ? (devClass & 0x1F00UL) : 0;
unsigned long minorClass = hasClass ? (devClass & 0x00FFUL) : 0;
bool isCoDPhoneOrPc =
(hasClass && (majorClass == 0x0100UL || majorClass == 0x0200UL));
bool isCoDAudio = (hasClass && majorClass == 0x0400UL);
bool isCoDGamepad =
(hasClass && majorClass == 0x0500UL &&
((minorClass & 0x3CUL) == 0x04UL || (minorClass & 0x3CUL) == 0x08UL));
bool isCoDKeyboard =
(hasClass && majorClass == 0x0500UL && (minorClass & 0x40UL) != 0);
bool isCoDMouse =
(hasClass && majorClass == 0x0500UL && (minorClass & 0xC0UL) == 0x80UL);
bool isPhoneOrPc =
(iconLow == "phone" || iconLow == "computer" || isCoDPhoneOrPc);
if (iconLow == "input-gaming" || iconLow == "input-joystick" ||
isCoDGamepad ||
nameLow.find("gamepad") != std::string::npos ||
nameLow.find("controller") != std::string::npos ||
nameLow.find("joy-con") != std::string::npos ||
nameLow.find("dualshock") != std::string::npos ||
nameLow.find("dualsense") != std::string::npos ||
nameLow.find("8bitdo") != std::string::npos ||
nameLow.find("xbox") != std::string::npos) {
d.kind = BtDevKind::Gamepad;
d.profileLabel = "HID Gamepad";
} else if (iconLow == "input-keyboard" || isCoDKeyboard ||
nameLow.find("keyboard") != std::string::npos ||
nameLow.find("keychron") != std::string::npos) {
d.kind = BtDevKind::Keyboard;
d.profileLabel = "HID Keyboard";
} else if (iconLow == "input-mouse" || iconLow == "input-tablet" ||
isCoDMouse ||
nameLow.find("mouse") != std::string::npos ||
nameLow.find("trackpad") != std::string::npos) {
d.kind = BtDevKind::Mouse;
d.profileLabel = "HID Mouse";
} else if (isPhoneOrPc) {
d.kind = BtDevKind::Phone;
d.profileLabel = "OBEX/PAN";
} else if (hasPaSink || hasPaSrc || hasA2dp || hasHfp || isCoDAudio ||
iconLow.rfind("audio-", 0) == 0) {
d.kind = BtDevKind::Audio;
if (d.connected) {
auto it = cardActiveProfile.find("bluez_card." + macU);
std::string activeProf =
(it != cardActiveProfile.end()) ? toLowerStr(it->second) : "";
if (activeProf.rfind("headset-head-unit", 0) == 0 ||
activeProf.find("hfp") != std::string::npos ||
activeProf.find("hsp") != std::string::npos) {
d.profileLabel = "HSP/HFP";
} else if (activeProf.rfind("a2dp", 0) == 0) {
d.profileLabel = "A2DP";
} else if (hasPaSrc) {
d.profileLabel = "HSP/HFP";
} else if (hasPaSink) {
d.profileLabel = "A2DP";
} else if (hasA2dp) {
d.profileLabel = "A2DP";
} else if (hasHfp) {
d.profileLabel = "HSP/HFP";
} else {
d.profileLabel = "A2DP";
}
} else {
if (hasA2dp && hasHfp) d.profileLabel = "Audio A2DP/HFP";
else if (hasHfp) d.profileLabel = "Audio HSP/HFP";
else d.profileLabel = "Audio A2DP";
}
} else if (hasHid) {
d.kind = BtDevKind::Keyboard;
d.profileLabel = "HID Input";
}
}
std::vector<BtDeviceEntry> queryBtDevices(const std::string& scanRaw = "") {
std::vector<BtDeviceEntry> out;
std::unordered_map<std::string, const BtRawDevSnapshot*> snapByMac;
bool usingSnapshot = (gCachedBtSnapshot && gCachedBtSnapshot->valid);
if (usingSnapshot) {
for (const auto& rd : gCachedBtSnapshot->devs) {
BtDeviceEntry d;
d.mac = rd.mac;
d.name = rd.name;
d.paired = rd.paired;
d.connected = rd.connected;
d.batteryPct = rd.batteryPct;
out.push_back(d);
snapByMac[rd.mac] = &rd;
}
} else {
std::unordered_set<std::string> pairedMacs;
std::unordered_set<std::string> connectedMacs;
auto parseMacSet = [](const std::string& raw,
std::unordered_set<std::string>& dst) {
std::istringstream ss(raw);
std::string line;
while (std::getline(ss, line)) {
if (line.rfind("Device ", 0) == 0 && line.size() >= 24) {
dst.insert(line.substr(7, 17));
}
}
};
parseMacSet(runCaptureCmd("timeout 2 bluetoothctl devices Paired 2>/dev/null || "
"timeout 2 bluetoothctl paired-devices 2>/dev/null"),
pairedMacs);
parseMacSet(runCaptureCmd("timeout 2 bluetoothctl devices Connected 2>/dev/null"),
connectedMacs);
std::string allRaw = runCaptureCmd("timeout 2 bluetoothctl devices 2>/dev/null");
std::istringstream ss(allRaw);
std::string line;
while (std::getline(ss, line)) {
if (line.rfind("Device ", 0) != 0 || line.size() < 24) continue;
BtDeviceEntry d;
d.mac = line.substr(7, 17);
d.name = (line.size() > 25) ? trimStr(line.substr(25)) : d.mac;
d.paired = (pairedMacs.count(d.mac) > 0);
d.connected = (connectedMacs.count(d.mac) > 0);
out.push_back(d);
}
}
auto isRawMacName = [](const BtDeviceEntry& e) {
@@ -793,109 +990,99 @@ std::vector<BtDeviceEntry> queryBtDevices(const std::string& scanRaw = "") {
return a.name < b.name;
});
bool anyConnected = false;
for (const auto& d : out) {
if (infoCount >= 24) break;
if (d.connected || d.paired || !isRawMacName(d)) {
infoCmd += "printf '===BTDEV %s===\\n' " + shellQuote(d.mac) +
"; timeout 1 bluetoothctl info " + shellQuote(d.mac) +
" 2>/dev/null; ";
++infoCount;
if (d.connected) {
anyConnected = true;
break;
}
}
std::string paRaw;
std::unordered_map<std::string, std::string> cardActiveProfile;
if (anyConnected) {
paRaw = runCaptureCmd(
"pactl list sinks short 2>/dev/null; pactl list sources short 2>/dev/null; "
"pactl list cards 2>/dev/null");
std::istringstream pss(paRaw);
std::string pline;
std::string curCard;
while (std::getline(pss, pline)) {
std::string t = trimStr(pline);
if (t.rfind("Name: bluez_card.", 0) == 0) {
curCard = trimStr(t.substr(6));
} else if (!curCard.empty() && t.rfind("Active Profile:", 0) == 0) {
cardActiveProfile[curCard] = trimStr(t.substr(15));
curCard.clear();
}
}
}
std::unordered_map<std::string, std::string> infoByMac;
if (!infoCmd.empty()) {
std::string batchInfo = runCaptureCmd(infoCmd);
std::istringstream iss(batchInfo);
std::string iline;
std::string curMac;
while (std::getline(iss, iline)) {
if (iline.rfind("===BTDEV ", 0) == 0 && iline.size() >= 26) {
curMac = iline.substr(9, 17);
} else if (!curMac.empty()) {
infoByMac[curMac] += iline + "\n";
if (!usingSnapshot) {
std::string infoCmd;
int infoCount = 0;
for (const auto& d : out) {
if (infoCount >= 24) break;
if (d.connected || d.paired || !isRawMacName(d)) {
infoCmd += "printf '===BTDEV %s===\\n' " + shellQuote(d.mac) +
"; timeout 1 bluetoothctl info " + shellQuote(d.mac) +
" 2>/dev/null; ";
++infoCount;
}
}
if (!infoCmd.empty()) {
std::string batchInfo = runCaptureCmd(infoCmd);
std::istringstream iss(batchInfo);
std::string iline;
std::string curMac;
while (std::getline(iss, iline)) {
if (iline.rfind("===BTDEV ", 0) == 0 && iline.size() >= 26) {
curMac = iline.substr(9, 17);
} else if (!curMac.empty()) {
infoByMac[curMac] += iline + "\n";
}
}
}
}
for (auto& d : out) {
std::string macU = d.mac;
for (char& c : macU) {
if (c == ':') c = '_';
if (usingSnapshot) {
auto sit = snapByMac.find(d.mac);
if (sit != snapByMac.end() && sit->second) {
const BtRawDevSnapshot* rd = sit->second;
classifyBtDevice(d, paRaw, cardActiveProfile, rd->icon, rd->devClass,
rd->hasClass, rd->uuidsLow);
} else {
classifyBtDevice(d, paRaw, cardActiveProfile, "", 0, false, "");
}
continue;
}
bool hasPaSink = (paRaw.find("bluez_output." + macU) != std::string::npos);
bool hasPaSrc = (paRaw.find("bluez_input." + macU) != std::string::npos);
std::string info = infoByMac[d.mac];
std::string infoLow = toLowerStr(info);
std::string nameLow = toLowerStr(d.name);
bool hasA2dp = (infoLow.find("0000110b-") != std::string::npos ||
infoLow.find("audio sink") != std::string::npos);
bool hasHfp = (infoLow.find("00001108-") != std::string::npos ||
infoLow.find("0000111e-") != std::string::npos);
bool hasHid = (infoLow.find("00001124-") != std::string::npos ||
infoLow.find("00001812-") != std::string::npos ||
infoLow.find("human interface device") != std::string::npos);
bool isPhoneOrPc = (infoLow.find("icon: phone") != std::string::npos ||
infoLow.find("icon: computer") != std::string::npos);
if (infoLow.find("icon: input-gaming") != std::string::npos ||
infoLow.find("icon: input-joystick") != std::string::npos ||
nameLow.find("gamepad") != std::string::npos ||
nameLow.find("controller") != std::string::npos ||
nameLow.find("joy-con") != std::string::npos ||
nameLow.find("dualshock") != std::string::npos ||
nameLow.find("dualsense") != std::string::npos ||
nameLow.find("8bitdo") != std::string::npos ||
nameLow.find("xbox") != std::string::npos) {
d.kind = BtDevKind::Gamepad;
d.profileLabel = "HID Gamepad";
} else if (infoLow.find("icon: input-keyboard") != std::string::npos ||
nameLow.find("keyboard") != std::string::npos ||
nameLow.find("keychron") != std::string::npos) {
d.kind = BtDevKind::Keyboard;
d.profileLabel = "HID Keyboard";
} else if (infoLow.find("icon: input-mouse") != std::string::npos ||
infoLow.find("icon: input-tablet") != std::string::npos ||
nameLow.find("mouse") != std::string::npos ||
nameLow.find("trackpad") != std::string::npos) {
d.kind = BtDevKind::Mouse;
d.profileLabel = "HID Mouse";
} else if (isPhoneOrPc) {
d.kind = BtDevKind::Phone;
d.profileLabel = "OBEX/PAN";
} else if (hasPaSink || hasPaSrc || hasA2dp || hasHfp ||
infoLow.find("icon: audio-") != std::string::npos) {
d.kind = BtDevKind::Audio;
if (d.connected) {
std::string activeProf = toLowerStr(cardActiveProfile["bluez_card." + macU]);
if (activeProf.rfind("headset-head-unit", 0) == 0 ||
activeProf.find("hfp") != std::string::npos ||
activeProf.find("hsp") != std::string::npos) {
d.profileLabel = "HSP/HFP";
} else if (activeProf.rfind("a2dp", 0) == 0) {
d.profileLabel = "A2DP";
} else if (hasPaSrc) {
d.profileLabel = "HSP/HFP";
} else if (hasPaSink) {
d.profileLabel = "A2DP";
} else if (hasA2dp) {
d.profileLabel = "A2DP";
} else if (hasHfp) {
d.profileLabel = "HSP/HFP";
} else {
d.profileLabel = "A2DP";
std::string iconLow;
unsigned long devClass = 0;
bool hasClass = false;
{
std::istringstream issInfo(infoLow);
std::string il;
while (std::getline(issInfo, il)) {
std::string t = trimStr(il);
if (t.rfind("icon:", 0) == 0) {
iconLow = trimStr(t.substr(5));
} else if (t.rfind("class:", 0) == 0) {
std::string cs = trimStr(t.substr(6));
char* endp = nullptr;
unsigned long cls = std::strtoul(cs.c_str(), &endp, 0);
if (endp != cs.c_str() && cls != 0) {
devClass = cls;
hasClass = true;
}
}
} else {
if (hasA2dp && hasHfp) d.profileLabel = "Audio A2DP/HFP";
else if (hasHfp) d.profileLabel = "Audio HSP/HFP";
else d.profileLabel = "Audio A2DP";
}
} else if (hasHid) {
d.kind = BtDevKind::Keyboard;
d.profileLabel = "HID Input";
}
classifyBtDevice(d, paRaw, cardActiveProfile, iconLow, devClass, hasClass,
infoLow);
size_t batPos = info.find("Battery Percentage:");
if (batPos != std::string::npos) {
@@ -927,20 +1114,29 @@ BtControllerState queryBtController(bool btOn,
st.discoverable = readSavedBtDiscoverable();
if (btOn) {
std::string showOut = runCaptureCmd("timeout 2 bluetoothctl show 2>/dev/null");
std::istringstream ss(showOut);
std::string line;
while (std::getline(ss, line)) {
std::string t = trimStr(line);
if (t.rfind("Discoverable:", 0) == 0) {
std::string v = toLowerStr(trimStr(t.substr(13)));
if (v == "yes") st.discoverable = true;
else if (v == "no") st.discoverable = false;
} else if (t.rfind("Alias:", 0) == 0) {
std::string al = trimStr(t.substr(6));
if (!al.empty() && al.rfind("BlueZ", 0) != 0 &&
::access((getHomeDirPath() + "/.config/sxmo/bt_name").c_str(), F_OK) != 0) {
st.name = al;
if (gCachedBtSnapshot && gCachedBtSnapshot->valid) {
st.discoverable = gCachedBtSnapshot->discoverable;
const std::string& al = gCachedBtSnapshot->alias;
if (!al.empty() && al.rfind("BlueZ", 0) != 0 &&
::access((getHomeDirPath() + "/.config/sxmo/bt_name").c_str(), F_OK) != 0) {
st.name = al;
}
} else {
std::string showOut = runCaptureCmd("timeout 2 bluetoothctl show 2>/dev/null");
std::istringstream ss(showOut);
std::string line;
while (std::getline(ss, line)) {
std::string t = trimStr(line);
if (t.rfind("Discoverable:", 0) == 0) {
std::string v = toLowerStr(trimStr(t.substr(13)));
if (v == "yes") st.discoverable = true;
else if (v == "no") st.discoverable = false;
} else if (t.rfind("Alias:", 0) == 0) {
std::string al = trimStr(t.substr(6));
if (!al.empty() && al.rfind("BlueZ", 0) != 0 &&
::access((getHomeDirPath() + "/.config/sxmo/bt_name").c_str(), F_OK) != 0) {
st.name = al;
}
}
}
}
@@ -1061,6 +1257,7 @@ int runNetworksApp(int argc, char** argv) {
// Bluetooth target device
BtDeviceEntry targetBt;
bool forceFreshBtState = false;
// Incoming OBEX confirmation & directory picker state
ObexPromptInfo obexPrompt;
@@ -1115,32 +1312,46 @@ int runNetworksApp(int argc, char** argv) {
} else if (activeTab == NetTab::Bluetooth) {
btOn = isBtEnabledFast();
std::string scanRaw;
BtStateSnapshot snap;
if (btOn) {
int obexPid = readIntFile("/tmp/bananarchy_bt_obex.pid");
if (obexPid <= 0 || (::kill(obexPid, 0) != 0 && errno != EPERM)) {
runAsyncCmd("doas -n /usr/bin/bananarchy-bt-obex start >/dev/null 2>&1");
}
btRx = readBtRxStatus();
snap = loadBtStateSnapshot(forceFreshBtState);
forceFreshBtState = false;
if (forceScan) {
// 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) {
bool needPowerOn = false;
if (snap.valid) {
needPowerOn = !snap.powered;
} else {
std::string show = runCaptureCmd(
"timeout 2 bluetoothctl show 2>/dev/null");
needPowerOn = (show.find("Powered: no") != std::string::npos ||
show.find("Powered:") == std::string::npos);
}
if (needPowerOn) {
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");
snap = loadBtStateSnapshot(true);
}
gCachedBtSnapshot = snap.valid ? &snap : nullptr;
btList = queryBtDevices(scanRaw);
} else {
forceFreshBtState = false;
btList.clear();
gCachedBtSnapshot = nullptr;
}
btCtrl = queryBtController(btOn, btList);
gCachedBtSnapshot = nullptr;
int maxRow = btOn ? (4 + static_cast<int>(btList.size())) : 2;
if (btRowSel > maxRow) btRowSel = maxRow;
}
@@ -2242,6 +2453,7 @@ int runNetworksApp(int argc, char** argv) {
runCaptureCmd("timeout 2 bluetoothctl system-alias " + shellQuote(clean) +
" >/dev/null 2>&1");
}
forceFreshBtState = true;
refreshCurrentTab(false);
setBanner("Bluetooth name: " + clean);
};
@@ -2256,6 +2468,7 @@ int runNetworksApp(int argc, char** argv) {
"timeout 2 bluetoothctl discoverable " +
std::string(nextDisc ? "on" : "off") + " >/dev/null 2>&1");
}
forceFreshBtState = true;
refreshCurrentTab(false);
setBanner(nextDisc ? "Bluetooth: Visible for 60s"
: "Bluetooth: Hidden from discovery");
@@ -2295,6 +2508,7 @@ int runNetworksApp(int argc, char** argv) {
runAsyncCmd("sxmo_audio.sh device setinput Mic1 >/dev/null 2>&1; "
"sxmo_audio.sh device set Speaker >/dev/null 2>&1");
}
forceFreshBtState = true;
refreshCurrentTab(false);
setBanner("Disconnected " + targetBt.name);
} else if (act == 2) {
@@ -2351,6 +2565,7 @@ int runNetworksApp(int argc, char** argv) {
} else if (act == 4) {
runCaptureCmd("timeout 3 bluetoothctl remove " + shellQuote(targetBt.mac) +
" >/dev/null 2>&1", &rc);
forceFreshBtState = true;
refreshCurrentTab(false);
setBanner("Removed " + targetBt.name);
}
@@ -2513,6 +2728,7 @@ int runNetworksApp(int argc, char** argv) {
render();
runCaptureCmd("SXMO_OS=alpine doas -n sxmo_bluetoothtoggle.sh " +
std::string(btOn ? "off" : "on") + " >/dev/null 2>&1");
forceFreshBtState = true;
refreshCurrentTab(false);
setBanner(btOn ? "Bluetooth enabled" : "Bluetooth disabled");
} else if (btRowSel == 1) {
@@ -2580,6 +2796,7 @@ int runNetworksApp(int argc, char** argv) {
setBanner("Turning Bluetooth ON...");
render();
runCaptureCmd("SXMO_OS=alpine doas -n sxmo_bluetoothtoggle.sh on >/dev/null 2>&1");
forceFreshBtState = true;
refreshCurrentTab(false);
} else {
setBanner("Scanning Bluetooth (15s)...", 16000);
@@ -3071,6 +3288,7 @@ int runNetworksApp(int argc, char** argv) {
::unlink(kBtOpDonePath);
if (!opMac.empty()) targetBt.mac = opMac;
if (!opName.empty()) targetBt.name = opName;
forceFreshBtState = true;
refreshCurrentTab(false);
refreshTargetBtFromList();
if (opMode == "pair") {