| Home · All Classes · Grouped Classes · Annotated · Functions |
The QBluetoothAbstractService class provides a base interface class for Bluetooth services within Qtopia. More...
#include <QBluetoothAbstractService>
Inherits QObject.
Inherited by QBluetoothHandsfreeService, QBluetoothHeadsetService, and QBluetoothSerialPortService.
The QBluetoothAbstractService class provides a base interface class for Bluetooth services within Qtopia.
This class makes it easier to implement Qtopia Bluetooth services. If you subclass QBluetoothAbstractService to create a Bluetooth service, the service is automatically registered and accessible as a system Bluetooth service within Qtopia. The service can then be accessed externally by programmers through the QBluetoothServiceController class. It will also be listed in the system Bluetooth settings application, allowing end users to modify the service's settings.
Following is an example of a Qtopia Bluetooth service that is created by subclassing QBluetoothAbstractService. The service will publish the Serial Port Profile, and the service name will be "MySerialPortService".
This is the interface for the class:
class MyBluetoothService : public QBluetoothAbstractService
{
Q_OBJECT
public:
MyBluetoothService(QObject *parent = 0);
virtual void start();
virtual void stop();
virtual void setSecurityOptions(QBluetooth::SecurityOptions options);
private:
quint32 m_serviceRecordHandle;
};
Now, to the implementation. Firstly, the constructor might look something like this:
MyBluetoothService::MyBluetoothService(QObject *parent)
: QBluetoothAbstractService("MySerialPortService", tr("Serial Port Service"), parent)
{
m_serviceRecordHandle = 0;
}
This creates a service named "MySerialPortService". The name must be unique among Qtopia Bluetooth services, as it is used internally to uniquely identify the service (for example, for storing service settings).
The second string argument is an internationalized, human-readable name that can be displayed to the end user. This string will be used, for example, for listing local Bluetooth services in Qtopia's Bluetooth settings application.
The m_serviceRecordHandle member will store the handle for the service's associated SDP service record, as you will see below in the start() and stop() implementations.
Then, to implement the start() method:
void MyBluetoothService::start()
{
// Register an SDP service for this Bluetooth service
quint32 handle = registerRecord("SerialPortService.xml");
if (handle == 0) {
// registration failed
emit started(true, tr("Error registering the SDP service"));
return;
}
// Call some method to start the service
if (!startMyService()) {
// The service failed to start, so unregister the SDP service
// that was previously registered
unregisterRecord(handle);
// notify the system that the service was not started
emit started(true, tr("Unable to start MySerialPortService"));
return;
}
// The service was successfully started
m_serviceRecordHandle = handle;
emit started(false, QString());
}
The start() method is called by Qtopia when the service should be started. Note how the example emits the started() signal when the service fails to start, and also when it does start successfully.
The class also needs to have a stop() method:
void MyBluetoothService::stop()
{
// call some method to shut down the service
stopMyService();
if (!unregisterRecord(m_serviceRecordHandle))
qWarning() << "Error unregistering the SDP service";
// notify the system that the service has stopped
emit stopped();
}
Finally, the class will need to implement the setSecurityOptions() method to provide the ability to modify the security settings for the service.
Constructs a Bluetooth service. The name is a unique name that identifies the service, and displayName is a user-friendly, internationalized name for this service that can be displayed to the end user. The parent is the QObject parent for this service.
Destroys a Bluetooth service.
Returns the user-friendly, internationalized name for this service that can be displayed to the end user.
Returns the unique name that identifies this service.
Registers the SDP service record record for this Bluetooth service and returns the service record handle of the newly registered service. Returns zero if the registration failed.
See also unregisterRecord().
This is an overloaded member function, provided for convenience.
Uses the XML data from the file filename to register the SDP service record for this Bluetooth service and returns the service record handle of the newly registered service. Returns zero if the registration failed.
An example of the required XML data structure can be seen by running the sdptool get --xml command in a terminal shell. For example, this will add an Object Push service, look up the service to find its service record handle, and then use sdptool get --xml to show the XML structure:
$> sdptool add OPUSH
$> sdptool browse local
Browsing FF:FF:FF:00:00:00 ...
Service Name: OBEX Object Push
Service RecHandle: 0x10000
Service Class ID List:
"OBEX Object Push" (0x1105)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 9
"OBEX" (0x0008)
Profile Descriptor List:
"OBEX Object Push" (0x1105)
Version: 0x0100
$> sdptool get --xml 0x10000
Note: If you use sdptool get --xml to generate the SDP record XML file, you must remove the service record handle XML element from the generated output before passing it to this function. This element is usually near the start of the XML output and will look similar to this:
<attribute id="0x0000">
<uint32 value="..." />
</attribute>
If this element is not removed, the registration will fail, as the service record handle will be automatically generated by the SDP server and should not be provided by the programmer.
Warning: The given file must be UTF-8 encoded to be parsed correctly.
See also unregisterRecord().
Sets the security options for this service to the given options.
This method will be called by Qtopia when the security options should be changed.
Subclasses must override this to set the security options for this service.
Starts this service.
This method will be called by Qtopia when the service should be started. This may be because an external party has required that the service be started (for example, through QBluetoothServiceController, or through the Bluetooth Settings application) or because Qtopia has been configured to start the service automatically.
Subclasses must override this to start the service appropriately. The subclass must emit started() when the service has started, or failed while trying to start, to announce the result of the start() invocation.
Warning: This function must be implementated in such a way that any intermediate objects (which have been created up to the point where the error occurred) are cleaned up before the error signal is emitted.
See also started() and stop().
When implementing the start() function, this signal must be emitted by the subclass when the service has started or failed while attempting to start, to announce the result of the start() invocation.
If the service failed to start, error should be true and description should be a human-readable description of the error. Otherwise, error should be false and description should be a null QString.
See also start() and stopped().
Stops this service.
This method will be called by Qtopia when the service should be stopped.
Subclasses must override this to stop the service appropriately. The subclass must emit stopped() to announce that the service has stopped.
See also stopped() and start().
This signal must be emitted by the subclass inside the implementation of the stop() function, to announce that the service has stopped.
See also started() and stop().
Unregisters the SDP service record with the service record handle handle.
Returns whether the record was successfully unregistered.
See also registerRecord().
| Copyright © 2007 Trolltech | Trademarks | Qtopia 4.3.0 |