How To Choose Local Adapter When Connecting To Service With Qbluetoothsocket
In the presence of multiple Bluetooth adapters, is it possible to specify which local adapter to use when creating a QBluetoothSocket or calling QBluetoothSocket::connectToService(
Solution 1:
As of Qt 5.6.2, there is no such functionality yet apart from QBluetoothLocalDevice(QBluetoothAddress)
, QBluetoothDeviceDiscoveryAgent(QBluetoothAddress)
, QBluetoothServiceDiscoveryAgent(QBluetoothAddress)
and
QBluetoothServer::listen(QBluetoothAddress)
. These would only make sense on Linux and not on Android since the Android Bluetooth stack doesn't seem to support multiple dongles, at least yet.
However, on Linux with BlueZ, the following is possible to choose the local adapter using the BlueZ c API:
#include<sys/socket.h>#include<bluetooth/bluetooth.h>#include<bluetooth/rfcomm.h>
...
QBluetoothSocket* socket = newQBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
structsockaddr_rc loc_addr;
loc_addr.rc_family = AF_BLUETOOTH;
int socketDescriptor = ::socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
if(socketDescriptor < 0){
qCritical() << strerror(errno);
return;
}
constchar* localMacAddr = "XX:XX:XX:XX:XX:XX"; //MAC address of the local adapterstr2ba(localMacAddr, &(loc_addr.rc_bdaddr));
if(bind(socketDescriptor, (struct sockaddr*)&loc_addr, sizeof(loc_addr)) < 0){
qCritical() << strerror(errno);
return;
}
if(!socket->setSocketDescriptor(socketDescriptor, QBluetoothServiceInfo::RfcommProtocol, QBluetoothSocket::UnconnectedState)){
qCritical() << "Couldn't set socketDescriptor";
return;
}
socket->connectToService(...);
The project .pro
must contain the following:
CONFIG += link_pkgconfig
PKGCONFIG += bluez
The corresponding feature request to possibly integrate this into the Qt APIs: https://bugreports.qt.io/browse/QTBUG-57382
Post a Comment for "How To Choose Local Adapter When Connecting To Service With Qbluetoothsocket"