Trim whitespace from end of bluetooth device names

I have a speaker at home with a name of "Pebble V3\r\n" that renders
poorly without this change.
custom
Wesley Ellis 2 months ago
parent d147c92053
commit ff9c4885bf
  1. 17
      src/drivers/bluetooth.cpp

@ -364,8 +364,21 @@ auto Scanner::HandleDeviceDiscovery(const esp_bt_gap_cb_param_t& param)
return;
}
device.name = std::pmr::string{reinterpret_cast<char*>(name),
static_cast<size_t>(length)};
// Create string from the device name
std::pmr::string deviceName{reinterpret_cast<char*>(name),
static_cast<size_t>(length)};
// Trim trailing whitespace (spaces, tabs, \r, \n)
const std::string::size_type lastChar = deviceName.find_last_not_of(" \n\r\t");
if (lastChar != std::string::npos) {
deviceName.erase(lastChar + 1);
}
if (deviceName.empty()) {
return;
}
device.name = deviceName;
events::DeviceDiscovered ev{.device = device};
tinyfsm::FsmList<bluetooth::BluetoothState>::dispatch(ev);
}

Loading…
Cancel
Save