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?
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.
Ah I see, so there's no way to get the PC to disconnect the headphones once they're connect via socket? Also should I use a different Uuid other than the SerialPort for headphones?
@aman There is of course a way to disconnect the device – using operating system’s APIs. They are a mess on Windows though. As for connecting to the headphones: you have to enumerate the services they provide and choose from one of them. And if the OS has attached the headphones as an audio device then your application will fail to take over the connection. You’d need to get the OS to disconnect the audio device first. I think you don’t check any errors and your code probably doesn’t do anything at all. I can’t make it work here - nothing succeeds, as expected.
Alright that makes sense thank you for the helpful replies!