Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

Qt Bluetooth Module

Overview

Bluetooth is a short-range (less than 100 meters) wireless technology. It has a reasonably high data transfer rate of 2.1 Mbit/s, which makes it ideal for transfering data between devices.

Bluetooth connectivity is based on basic device management like scanning for devices and gathering information about them. On top of that, there are a number of profiles which enable a certain specified interaction type on the Bluetooth connection.

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.

The following documentation leads you through what you can do, getting started, some simple examples and tutorials.

Getting Started

As shown in the above list, 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. The QBluetoothLocalDevice is the one to provide all of this information. Additionally you can use it to turn Bluetooth on and off and set the visibility on the device.

 QBluetoothLocalDevice localDevice;
 QString localDeviceName;

 // 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)
 }

Similar to the QBluetoothLocalDevice, the API offers QBluetoothDeviceInfo which provides that information for remote devices. While 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.

 // Create a discovery agent and connect to its signals
 QBluetoothDiscoveryAgent *discoveryAgent = new QBluetoothDiscoveryAgent(this);
 connect(discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)),
         this, SLOT(deviceDiscovered(const QBluetoothDeviceInfo&)));

 // Start a discovery
 discoveryAgent->start();

 ...

 // In your local slot, read information about the found devices
 void MyClass::deviceDiscovered(const QBluetoothDeviceInfo &device)
 {
     qDebug() << "Found new device:" << device.name() << '(' << device.address().toString() << ')';
 }

Once the desired device is found, there are two main use cases provided by Qt Bluetooth. The simpler 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 the QBluetoothTransferManager to push the file contained in the request by calling the put() function.

 // Create a transfer manager
 QBluetoothTransferManager *transferManager = new QBluetoothTransferManager(this);

 // Create the transfer request and file to be sent
 QBluetoothTransferRequest request(device.address());
 QFile *file = new QFile("testfile.txt");

 // Ask the transfer manager to send it
 QBluetoothTransferReply *reply = transferManager->put(request, file);

 // Connect to the reply's signals to be informed about the status and do cleanups when done
 connect(reply, SIGNAL(finished(QBluetoothTransferReply*)),
         this, SLOT(transferFinished(QBluetoothTransferReply*)));

The more flexible approach to do 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 create SPP connections, you need to register a Server one one device by using QRfcommServer.

 rfcommServer = new QRfcommServer(this);
 connect(rfcommServer, SIGNAL(newConnection()), this, SLOT(clientConnected()));
 rfcommServer->listen();

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 service
     socket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket);
     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 use cases like gaming or 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 QRfcommServer and QBluetoothSocket classes. A good example to start with SPP is the Bluetooth Chat example.

To get started with adding the Qt Bluetooth API to your project, see the Examples or check out the QML and C++ API's with their "Getting started" sections.

QML API

There are a number of QML Types available in the Qt Bluetooth API.

C++ API

The Qt Bluetooth C++ API requires gives a bit more flexibility on how to interact with remote devices.

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web