Warm tip: This article is reproduced from serverfault.com, please click

Qt disconnectFromService() not actaully disconnecting bluetooth device?

发布于 2020-12-06 22:22:26

I'm connecting a pair of bluetooth headphones to my windows pc using Qt's QBluetoothSocket class as follows:

device->connectToService(QBluetoothAddress(device1Info[1]), QBluetoothUuid(QBluetoothUuid::SerialPort));

and to disconnect the headphones I use:

if (device->state() == QBluetoothSocket::ConnectedState) {
    device->disconnectFromService();
}

They connect fine but when I opt to disconnect them they don't actually disconnect. The disconnect signal is emitted which means the socket did disconnect the service but when I check my devices in Windows settings the headphones still register as connected and behave as such. Could this be because I am using QBluetoothUuid::SerialPort as the QBluetoothUuid when connecting to the serive? Or something else?

Questioner
aman
Viewed
0
Kuba hasn't forgotten Monica 2020-12-07 09:07:42

The disconnection doesn’t disconnect the device, it just disconnects the socket. It’s just as if you closed a network socket - it won’t disconnect your PC from the network :)

I can’t reproduce the connection succeeding though - not unless your headphones do offer a serial port service for debugging or something.

My guess is that your entire program does nothing, because headphones don’t offer serial ports and also the OS normally will immediately attach an audio device to the headphones’ audio service, so you won’t be able to attach to it.

I think you’re overthinking this: you don’t need to use any Bluetooth APIs here. All you need is to use the audio device API to find the headphones and do audio I/O through them. I’m not sure that Qt will help you with that though. Look in the Qt’s multimedia API for a way to enumerate audio devices. Otherwise you will need to use OS API calls or some audio access library.