jnny_cn的个人主页

http://www.qtcn.org/bbs/u/7713  [收藏] [复制]

jnny_cn

  • 23

    关注

  • 124

    粉丝

  • 91

    访客

  • 等级:侠客
  • 总积分:165
  • 保密,1990-01-01

最后登录:2019-12-13

更多资料

日志

Qt Bluetooth Overview

2014-05-23 11:20


Qt Bluetooth Overview


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.

Note that the Object Push Profile is not supported on Android.
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 is 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.QBluetoothLocalDevice localDevice;QString localDeviceName;// Check if Bluetooth is available on this deviceif (localDevice.isValid()) {// Turn Bluetooth onlocalDevice.powerOn();// Read local device namelocalDeviceName = localDevice.name();// Make it visible to otherslocalDevice.setHostMode(QBluetoothLocalDevice::HostDiscoverable);// Get connected devicesQList<QBluetoothAddress> remotes;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.void MyClass::startDiscovery(){// Create a discovery agent and connect to its signalsQBluetoothDeviceDiscoveryAgent *discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),this, SLOT(deviceDiscovered(QBluetoothDeviceInfo)));// Start a discoverydiscoveryAgent->start();//...}// In your local slot, read information about the found devicesvoid MyClass::deviceDiscovered(const QBluetoothDeviceInfo &device){qDebug() << "Found new device:" << device.name() << '(' << device.address().toString() << ')';}

Pushing Files to Remote Devices


Once the desired device was found, there are two main use cases provided by Qt Bluetooth. The simpler one is to send files via the Obex Object Push Profile (OPP). As the name describes, this profile can only push files from one device to another, but not pull files or browse the remote file system. Because of this limitation, this profile does not require the two devices to be paired before exchanging data. To push files to remote devices, create a QBluetoothTransferRequest and ask theQBluetoothTransferManager to push the file contained in the request by calling its put() function.// Create a transfer managerQBluetoothTransferManager *transferManager = new QBluetoothTransferManager(this);// Create the transfer request and file to be sentQBluetoothAddress remoteAddress("00:11:22:33:44:55:66");QBluetoothTransferRequest request(remoteAddress);QFile *file = new QFile("testfile.txt");// Ask the transfer manager to send itQBluetoothTransferReply *reply = transferManager->put(request, file);// Connect to the reply's signals to be informed about the status and do cleanups when doneQObject::connect(reply, SIGNAL(finished(QBluetoothTransferReply*)),this, SLOT(transferFinished(QBluetoothTransferReply*)));

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 usingQBluetoothServer.rfcommServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this);connect(rfcommServer, SIGNAL(newConnection()), this, SLOT(clientConnected()));bool result = rfcommServer->listen(localAdapter);if (!result) {qWarning() << "Cannot bind chat server to" << localAdapter.toString();return;}
Connect to this server from another device playing the client role by using a QBluetoothSocket:void ChatClient::startClient(const QBluetoothServiceInfo &remoteService){if (socket)return;// Connect to servicesocket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);qDebug() << "Create socket";socket->connectToService(remoteService);qDebug() << "ConnectToService done";connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket()));connect(socket, SIGNAL(connected()), this, SLOT(connected()));connect(socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));}
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 QBluetoothServer and QBluetoothSocket classes. A good example to start with SPP is the Bluetooth Chat example.

分类:默认分类|回复:0|浏览:1919|全站可见|转载
 

下一篇: [转] Qt5 模块简介

上一篇: qt530 CMake Manual

Powered by phpwind v8.7 Certificate Copyright Time now is:04-29 05:32
©2005-2016 QTCN开发网 版权所有 Gzip disabled