Qt Bluetooth Overview#
The Qt Bluetooth API enables connectivity with other regular Bluetooth and Bluetooth Low Energy enabled devices.
With the Qt Bluetooth API typical use cases are:
Retrieve information about the local Bluetooth device.
Scan for other Bluetooth devices in range and retrieve information about them.
Push files to remote devices using the OBEX Object Push Profile (OPP)
Connect to remote devices through a RFCOMM channel using the Serial Port Profile (SPP).
Create a RFCOMM server that allows incoming connections using SPP.
Retrieve specification about Bluetooth Low Energy device.
Connect to Bluetooth Low Energy device.
Receive advertisement from Bluetooth Low Energy device.
Note that the Object Push Profile is not supported on Android and Windows.
Parts of RFCOMM functionality cannot be configured by Qt on Windows. A service’s
ProtocolDescriptorList are filled automatically. Therefore, registering a service with custom values for these fields might not yield the expected result on Windows.
The Received Signal Strength Indicator (RSSI), as well as the Manufacturer Specific Data advertised by Bluetooth LE devices are not supported by the Win32 backend. Also, it is only possible to find devices that have been previously paired through Windows Settings.
The following sections describe how to use the Qt Bluetooth C++ API classes for the above use cases.
Retrieving Local Device Information#
The Qt Bluetooth API has three main purposes. The first one is to obtain local and remote device information. The first steps in retrieving device information are to check if Bluetooth is available on the device and read the local device address and name.
QBluetoothLocalDevice is the class that provides all of this information. Additionally you can use it to turn Bluetooth on/off, set the visibility of the device and determine the current connections.
localDevice = QBluetoothLocalDevice() localDeviceName = QString() # Check if Bluetooth is available on this device if localDevice.isValid(): # Turn Bluetooth on localDevice.powerOn() # Read local device name localDeviceName = localDevice.name() # Make it visible to others localDevice.setHostMode(QBluetoothLocalDevice.HostDiscoverable) # Get connected devices remotes = QList() remotes = localDevice.connectedDevices()
Scanning for Bluetooth Devices#
Similar to the
QBluetoothLocalDevice , the API offers
QBluetoothDeviceInfo which provides similar information for remote devices. Although you can just create
QBluetoothDeviceInfo objects on your own and fill them with data, the easier way is to use the
QBluetoothDeviceDiscoveryAgent to start an automated search for visible Bluetooth devices within the connectable range.
def startDeviceDiscovery(self): # Create a discovery agent and connect to its signals discoveryAgent = QBluetoothDeviceDiscoveryAgent(self) connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), self, SLOT(deviceDiscovered(QBluetoothDeviceInfo))) # Start a discovery discoveryAgent.start() #... # In your local slot, read information about the found devices def deviceDiscovered(self, device): print("Found device():", device.name(), '(', device.address().toString(), ')')
Exchanging Data Between Devices#
The more flexible approach for communication between two Bluetooth enabled devices, is to create a virtual serial port connection and freely exchange data over that connection. This can be done by the Serial Port Profile (SPP). The Serial Port Profile emulates a serial connection over the Bluetooth transport protocol RFCOMM.
To be able to receive incoming SPP connections, you need to listen to incoming connections using
rfcommServer = QBluetoothServer(QBluetoothServiceInfo.RfcommProtocol, self) rfcommServer.newConnection.connect( self, QOverload<>.of(ChatServer.clientConnected)) result = rfcommServer.listen(localAdapter) if not result: qWarning() << "Cannot bind chat server to" << localAdapter.toString() return
Connect to this server from another device playing the client role by using a
def startClient(self, remoteService): if socket: return # Connect to service socket = QBluetoothSocket(QBluetoothServiceInfo.RfcommProtocol) print("Create socket") socket.connectToService(remoteService) print("ConnectToService done") socket.readyRead.connect(self.readSocket) socket.connected.connect(this, QOverload<>::of(&ChatClient::connected)) socket.disconnected.connect(self.disconnected) socket.errorOccurred.connect(self.onSocketErrorOccurred)
Using such a connection allows to exchange any form of data in both directions. It is perfectly suited for gaming or for syncing the state between two instances of an application on two devices. For more detailed descriptions on how to configure the server and client, please refer to the detailed description sections in the
QBluetoothSocket classes. A good example to start with SPP is the Bluetooth Chat example.
Bluetooth Low Energy#
Bluetooth Low Energy, also known as Bluetooth Smart, is a new technology enabling devices with low energy consumption to communicate with each other. More details about this technology and the related Qt APIs can be found in the Bluetooth Low Energy Overview .