@@ -184,39 +184,40 @@ void MediaController::handleConversationalAwareness(const QByteArray &data) {
184184}
185185
186186void MediaController::activateA2dpProfile () {
187- if (connectedDeviceMacAddress.isEmpty ()) {
188- LOG_WARN (" Connected device MAC address is empty, cannot activate A2DP profile" );
187+ if (connectedDeviceMacAddress.isEmpty () || m_deviceOutputName. isEmpty () ) {
188+ LOG_WARN (" Connected device MAC address or output name is empty, cannot activate A2DP profile" );
189189 return ;
190190 }
191191
192192 LOG_INFO (" Activating A2DP profile for AirPods" );
193193 int result = QProcess::execute (
194194 " pactl" , QStringList ()
195195 << " set-card-profile"
196- << " bluez_card. " + connectedDeviceMacAddress << " a2dp-sink" );
196+ << m_deviceOutputName << " a2dp-sink" );
197197 if (result != 0 ) {
198198 LOG_ERROR (" Failed to activate A2DP profile" );
199199 }
200200}
201201
202202void MediaController::removeAudioOutputDevice () {
203- if (connectedDeviceMacAddress.isEmpty ()) {
204- LOG_WARN (" Connected device MAC address is empty, cannot remove audio output device" );
203+ if (connectedDeviceMacAddress.isEmpty () || m_deviceOutputName. isEmpty () ) {
204+ LOG_WARN (" Connected device MAC address or output name is empty, cannot remove audio output device" );
205205 return ;
206206 }
207207
208208 LOG_INFO (" Removing AirPods as audio output device" );
209209 int result = QProcess::execute (
210210 " pactl" , QStringList ()
211211 << " set-card-profile"
212- << " bluez_card. " + connectedDeviceMacAddress << " off" );
212+ << m_deviceOutputName << " off" );
213213 if (result != 0 ) {
214214 LOG_ERROR (" Failed to remove AirPods as audio output device" );
215215 }
216216}
217217
218218void MediaController::setConnectedDeviceMacAddress (const QString &macAddress) {
219219 connectedDeviceMacAddress = macAddress;
220+ m_deviceOutputName = getAudioDeviceName ();
220221}
221222
222223MediaController::MediaState MediaController::mediaStateFromPlayerctlOutput (
@@ -252,4 +253,51 @@ MediaController::~MediaController() {
252253 playerctlProcess->waitForFinished (1000 );
253254 }
254255 }
255- }
256+ }
257+
258+ QString MediaController::getAudioDeviceName ()
259+ {
260+ if (connectedDeviceMacAddress.isEmpty ()) { return QString (); }
261+
262+ // Use a cleaner MAC address format for PulseAudio matching
263+ QProcess process;
264+ process.start (" pactl" , QStringList () << " list" << " sinks" );
265+ process.waitForFinished ();
266+
267+ QString output = process.readAllStandardOutput ();
268+ if (output.isEmpty ())
269+ {
270+ QProcess process;
271+ process.start (" pactl" , QStringList () << " list" << " sinks" );
272+ process.waitForFinished ();
273+ output = process.readAllStandardOutput ();
274+
275+ if (output.isEmpty ())
276+ {
277+ LOG_ERROR (" Failed to get PulseAudio and PipeWire sink list" );
278+ return QString ();
279+ }
280+ }
281+
282+ QStringList lines = output.split (" \n " );
283+
284+ QString currentSinkName;
285+ bool foundDevice = false ;
286+
287+ for (const QString &line : lines)
288+ {
289+ if (line.contains (" device.name" ))
290+ {
291+ currentSinkName = line.mid (line.indexOf (u' "' ) + 1 ).trimmed ().removeLast ();
292+ }
293+
294+ // Look for Bluetooth MAC address in various formats
295+ if (currentSinkName.contains (connectedDeviceMacAddress, Qt::CaseInsensitive))
296+ {
297+ foundDevice = true ;
298+ return currentSinkName;
299+ }
300+ }
301+
302+ return QString ();
303+ }
0 commit comments