警告

本节包含从C++自动翻译到Python的代码片段,可能包含错误。

Qt 蓝牙概述

Qt蓝牙API支持与其他常规蓝牙和低功耗蓝牙设备的连接。

使用Qt蓝牙API的典型用例有:

  • 检索有关本地蓝牙设备的信息。

  • 扫描范围内的其他蓝牙设备并检索它们的信息。

  • 使用OBEX对象推送配置文件(OPP)将文件推送到远程设备

  • 通过使用串行端口配置文件(SPP)的RFCOMM通道连接到远程设备。

  • 创建一个RFCOMM服务器,允许使用SPP进行传入连接。

  • 检索关于蓝牙低功耗设备的规格。

  • 连接到低功耗蓝牙设备。

  • 从蓝牙低功耗设备接收广告。

请注意,Android 和 Windows 不支持对象推送配置文件。

注意

在Windows上,Qt无法配置RFCOMM功能的部分内容。服务的ServiceClassIdsProtocolDescriptorList会自动填充。因此,在这些字段中使用自定义值注册服务可能不会在Windows上产生预期的结果。

注意

Win32 后端不支持接收信号强度指示器(RSSI),也不支持蓝牙 LE 设备广播的制造商特定数据。此外,只能找到之前通过 Windows 设置配对过的设备。

以下部分描述了如何为上述用例使用Qt Bluetooth C++ API类。

检索本地设备信息

Qt Bluetooth API 有三个主要用途。第一个是获取本地和远程设备信息。检索设备信息的第一步是检查设备上是否可用蓝牙,并读取本地设备地址和名称。QBluetoothLocalDevice 是提供所有这些信息的类。此外,您还可以使用它来打开/关闭蓝牙,设置设备的可见性并确定当前的连接。

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()

扫描蓝牙设备

类似于QBluetoothLocalDevice,API提供了QBluetoothDeviceInfo,它为远程设备提供了类似的信息。虽然你可以自己创建QBluetoothDeviceInfo对象并填充数据,但更简单的方法是使用QBluetoothDeviceDiscoveryAgent来启动自动搜索可连接范围内的可见蓝牙设备。

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(), ')')

设备之间的数据交换

在两个支持蓝牙的设备之间进行通信的更灵活的方法是创建一个虚拟串行端口连接,并通过该连接自由交换数据。这可以通过串行端口配置文件(SPP)来实现。串行端口配置文件通过蓝牙传输协议RFCOMM模拟串行连接。

为了能够接收传入的SPP连接,您需要使用QBluetoothServer来监听传入的连接。

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

通过使用QBluetoothSocket从另一个设备连接到扮演客户端角色的服务器:

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)

使用这种连接可以双向交换任何形式的数据。它非常适合用于游戏或同步两个设备上应用程序实例之间的状态。有关如何配置服务器和客户端的更详细描述,请参阅QBluetoothServerQBluetoothSocket类中的详细描述部分。开始使用SPP的一个好例子是Bluetooth Chat示例。

蓝牙低功耗

蓝牙低功耗,也称为蓝牙智能,是一种使低能耗设备能够相互通信的新技术。有关此技术及相关Qt API的更多详细信息,请参阅蓝牙低功耗概述