0

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()? I'm interested in Linux/BlueZ as well as Android (where it is not even clear whether multiple Bluetooth adapters are supported by the Bluetooth stack).

4

1 回答 1

0

QBluetoothLocalDevice(QBluetoothAddress)从 Qt 5.6.2 开始,除了、和 之外QBluetoothDeviceDiscoveryAgent(QBluetoothAddress),还没有这样的功能。这些仅在 Linux 上有意义,在 Android 上无效,因为 Android 蓝牙堆栈似乎不支持多个加密狗,至少目前如此。QBluetoothServiceDiscoveryAgent(QBluetoothAddress)QBluetoothServer::listen(QBluetoothAddress)

但是,在带有 BlueZ 的 Linux 上,可以使用 BlueZ c API 选择本地适配器:

#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

...

QBluetoothSocket* socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);

struct sockaddr_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;
}

const char* localMacAddr = "XX:XX:XX:XX:XX:XX"; //MAC address of the local adapter
str2ba(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(...);

该项目.pro必须包含以下内容:

CONFIG += link_pkgconfig
PKGCONFIG += bluez

可能将其集成到 Qt API 中的相应功能请求:https ://bugreports.qt.io/browse/QTBUG-57382

于 2016-12-06T16:40:24.693 回答