×
Namespaces

Variants
Actions
(Difference between revisions)

Qt Mobility example application: Fall Detector

From Nokia Developer Wiki
Jump to: navigation, search
jimgilmour1 (Talk | contribs)
m (minor spelling all source code exists no sis binary)
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(12 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:Code Examples]][[Category:Qt]][[Category:Qt for Symbian]]
+
[[Category:Code Examples]][[Category:MeeGo Harmattan]][[Category:Symbian]][[Category:Qt Mobility]]
 +
{{ArticleMetaData <!-- v1.2 -->
 +
|sourcecode= [[Media:Falldetector.zip]]
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 +
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
 +
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 +
|devicecompatability= Qt Mobility Technical Preview
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= DevCert
 +
|capabilities= Location NetworkServices LocalServices ReadUserData WriteUserData UserEnvironment ReadDeviceData WriteDeviceData
 +
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20100305
 +
|author= [[User:Domgus]]
 +
}}
  
The Fall Detector demo application automatically detects when a person falls down and provides a simple notification facility for detected downfalls (e.g. to request first aid etc.). In general the given example application demonstrates the utilization of Location, Contacts and Messaging modules from the Qt Mobility APIs Beta[http://qt.nokia.com/developer/new-qt-apis] release. The application also uses the acceleration sensor of a device. But since the Sensor API module from Qt Mobility is still in technology preview state and dedicated backend implementations are not available yet for any platform, the sensor access is implemented based on parts of the [[Mobile Extensions]] package.
+
{{Abstract|visible=true|The Fall Detector demo application automatically detects when a person falls down and provides a simple notification facility for detected downfalls (e.g. to request first aid etc.). In general the given example application demonstrates the utilization of Sensor, Location, Contacts and Messaging modules from the Qt Mobility APIs Beta[http://qt.nokia.com/developer/qt-roadmap] release}}
  
  
== Capabilities and Dependencies ==
+
[[File:Falldetector--screens.png]]
 +
 
 +
== Build Instructions ==
 +
 
 +
Download the [[File:Falldetector.zip]] and unzip it somewhere. Move to that directory and build it with
 +
 
 +
* qmake
 +
* make <cfg>
 +
 
 +
where <cfg> is a wildcard for your current build configuration and target (for example ''debug-gcce'', ''release-gcce'' etc.)
 +
 
 +
=== Capabilities ===
  
 
The application requires the following capabilities:
 
The application requires the following capabilities:
Line 10: Line 43:
 
CAPABILITY = Location NetworkServices LocalServices ReadUserData WriteUserData UserEnvironment ReadDeviceData WriteDeviceData
 
CAPABILITY = Location NetworkServices LocalServices ReadUserData WriteUserData UserEnvironment ReadDeviceData WriteDeviceData
  
Project dependencies:
+
=== Using the fall detector application ===
 +
 
 +
* Start the fall detector application
 +
* If the application is started for the first time, you will be prompted to enter some notification details (name and email address). This details will be saved in your local contact store. In case of a detected fall the application uses the given email address for notification as well.
 +
 
 +
== '''Code Snippets''' ==
 +
 
 +
=== Using the Sensor API to get current device acceleration ===
 +
 
 +
<code cpp-qt>
 +
#include <QAccelerometer>
 +
 
 +
// Neccessary for Qt Mobility API usage
 +
QTM_USE_NAMESPACE
 +
 
 +
class AccelerationInfo : public QObject, public QAccelerometerFilter
 +
{
 +
    Q_OBJECT
 +
 
 +
public:
 +
 
 +
    AccelerationInfo(QObject* parent = 0) : QObject(parent)
 +
    {
 +
        m_sensor = new QAccelerometer(this);
 +
        m_sensor->addFilter(this);
 +
        m_sensor->start();
 +
    }
 +
 
 +
private slots:
 +
 
 +
    // Override of QAcclerometerFilter::filter(QAccelerometerReading*)
 +
    void filter(QAccelerometerReading* reading)
 +
    {
 +
        qreal x = reading->x();
 +
        qreal y = reading->y();
 +
        qreal z = reading->z();
 +
 
 +
        // Process acceleration sensor readings ...
 +
 
 +
        qDebug("Current device acceleration: x=%f y=%f z=%f", x, y, z);
 +
    }
 +
 
 +
private:
 +
 
 +
    QAccelerometer* m_sensor;
 +
};
 +
</code>
 +
 
 +
=== Using the Location API to get current position ===
 +
 
 +
<code cpp-qt>
 +
#include <QGeoPositionInfo>
 +
#include <QGeoPositionInfoSource>
 +
 
 +
// Neccessary for Qt Mobility API usage
 +
QTM_USE_NAMESPACE
 +
 
 +
class LocationInfo : public QObject
 +
{
 +
    Q_OBJECT
 +
 
 +
public:
 +
 
 +
    LocationInfo(QObject* parent = 0) : QObject(parent)
 +
    {
 +
        QGeoPositionInfoSource* src = QGeoPositionInfoSource::createDefaultSource(this);
 +
        if (src)
 +
        {
 +
            connect(src, SIGNAL(positionUpdated(QGeoPositionInfo)), this,
 +
                SLOT(updatePosition(QGeoPositionInfo));
 +
            connect(src, SIGNAL(updateTimeout()), this, SLOT(updateTimeout()));
 +
            src->requestUpdate(5000); // Start request for actual position
 +
        }
 +
    }
 +
 
 +
private slots:
 +
 
 +
    void updatePosition(const QGeoPositionInfo& info)
 +
    {
 +
        qDebug() << “Current position: ” << info;
 +
    }
 +
   
 +
    void updateTimeout()
 +
    {
 +
        // Current location could not be retrieved within the specified timeout of 5 seconds.
 +
        qWarning(“Failed to retrieve current position.”);
 +
    }
 +
};
 +
</code>
 +
 
 +
=== Using the Contacts API ===
 +
 
 +
Create an example contact and populate it with data
 +
 
 +
<code cpp-qt>
 +
// Construct contact manager for default contact backend
 +
QContactManager* cm = new QContactManager();
 +
// Create example contact
 +
QContact example;
 +
 
 +
// Add contact name
 +
QContactName name;
 +
name.setFirst(“John”);
 +
name.setLast(“Doe”);
 +
example.saveDetail(&name);
 +
 
 +
// Add contact email address
 +
QContactEmailAddress email;
 +
email.setContexts(QContactDetail::ContextHome);
 +
email.setEmailAddress(“john.doe@example.com”);
 +
example.saveDetail(&email);
 +
 
 +
// Finally, save the contact details
 +
cm->saveContact(&example);
 +
</code>
 +
 
 +
Add custom field ''Interests'' to a note detail associated with a contact
 +
 
 +
<code cpp-qt>
 +
// Get the contact definition we want to modify (cm is a
 +
// previously created QContactManager object)
 +
QMap<QString, QContactDetailDefinition> def = cm->detailDefinitions();
 +
QContactDetailDefinition modified = def.value(QContactNote::DefinitionName);
 +
 
 +
// Define new field ‘Interests’
 +
QContactDetailDefinitionField field;
 +
field.setDataType(QVariant::String);
 +
QMap<QString, QContactDetailDefinitionField> fields = modified.fields();
 +
 
 +
// Insert new field to the list of contact’s fields and update
 +
// the contact’s field definitions
 +
fields.insert(“Interests”, field);
 +
modified.setFields(fields);
 +
 
 +
// Save the updated field definitions back to the manager
 +
cm->saveDetailDefinition(modified);
 +
</code>
 +
 
 +
Search for a contact based on an incoming call ID
 +
 
 +
<code cpp-qt>
 +
// Create new contact detail filter
 +
QContactDetailFilter filter;
 +
filter.setDetailDefinitionName(QContactPhoneNumber::DefinitionName,
 +
    QContactPhoneNumber::FieldNumber);
 +
 
 +
// Specify the requested value and filter matching criteria
 +
QString incomingNbr(“+436641234567”);
 +
filter.setValue(incomingNbr);
 +
filter.setMatchFlags(QContactFilter::MatchExactly);
 +
 
 +
// Fetch matching contacts list for specified filter (cm is a
 +
// previously created QContactManager object)
 +
QList<QContactLocalId> matches = cm->contacts(filter);
 +
 
 +
for (int i = 0; i < matches.size(); ++i)
 +
    qDebug() << cm->contact(matches.at(i));
 +
</code>
 +
 
 +
 
 +
=== Using the Message API to send an email message with a photo attachment ===
 +
 
 +
<code cpp-qt>
 +
// Setup new email message
 +
QMessage msg;
 +
msg.setType(QMessage::Email);
 +
 
 +
// Set recipient for our email message
 +
QString recipient(“john.doe@example.com”);
 +
msg.setTo(QMessageAddress(QMessageAddress::Email, recipient));
 +
 
 +
// Define message subject, body and append attachment
 +
msg.setSubject(“Messaging API example”);
 +
msg.setBody(“Hello,\n\nthis is an example message.”);
  
Be sure to have the Qt Mobility Beta libraries installed to both your SDK and your device to compile and run the application. Additionally you may have to modify the INCLUDEPATH option within the ''qt_mobility.pri'' file to point to the actual location of your Mobility API source folder. This step may be necessary to overcome some inconsistencies concerning the include definitions of the new Mobility APIs if they are installed or configured properly for your SDK. By default it is assumed that the project's root folder is located in a folder on the same level with the sources of the Mobility APIs.
+
QStringList attachments;
 +
attachments << “images/example.png”;
 +
msg.appendAttachments(attachments);
  
 +
// Send message using a new service handle
 +
QMessageService* svc = new QMessageService();
  
The code of the Fall Detector sample application can be found at [[File:Falldetector.zip]]
+
if (svc->send(msg))
 +
    qDebug(“Successfully sent message.”);
 +
else
 +
    qWarning(“Failed to send message.”);
 +
</code>

Latest revision as of 04:23, 11 October 2012

Article Metadata
Code ExampleCompatibility
Platform(s):
Symbian
Device(s): Qt Mobility Technical Preview
Platform Security
Signing Required: DevCert
Capabilities: Location NetworkServices LocalServices ReadUserData WriteUserData UserEnvironment ReadDeviceData WriteDeviceData
Article
Created: domgus (05 Mar 2010)
Last edited: hamishwillee (11 Oct 2012)

The Fall Detector demo application automatically detects when a person falls down and provides a simple notification facility for detected downfalls (e.g. to request first aid etc.). In general the given example application demonstrates the utilization of Sensor, Location, Contacts and Messaging modules from the Qt Mobility APIs Beta[1] release


Falldetector--screens.png

Contents

[edit] Build Instructions

Download the File:Falldetector.zip and unzip it somewhere. Move to that directory and build it with

  • qmake
  • make <cfg>

where <cfg> is a wildcard for your current build configuration and target (for example debug-gcce, release-gcce etc.)

[edit] Capabilities

The application requires the following capabilities:

CAPABILITY = Location NetworkServices LocalServices ReadUserData WriteUserData UserEnvironment ReadDeviceData WriteDeviceData

[edit] Using the fall detector application

  • Start the fall detector application
  • If the application is started for the first time, you will be prompted to enter some notification details (name and email address). This details will be saved in your local contact store. In case of a detected fall the application uses the given email address for notification as well.

[edit] Code Snippets

[edit] Using the Sensor API to get current device acceleration

#include <QAccelerometer>
 
// Neccessary for Qt Mobility API usage
QTM_USE_NAMESPACE
 
class AccelerationInfo : public QObject, public QAccelerometerFilter
{
Q_OBJECT
 
public:
 
AccelerationInfo(QObject* parent = 0) : QObject(parent)
{
m_sensor = new QAccelerometer(this);
m_sensor->addFilter(this);
m_sensor->start();
}
 
private slots:
 
// Override of QAcclerometerFilter::filter(QAccelerometerReading*)
void filter(QAccelerometerReading* reading)
{
qreal x = reading->x();
qreal y = reading->y();
qreal z = reading->z();
 
// Process acceleration sensor readings ...
 
qDebug("Current device acceleration: x=%f y=%f z=%f", x, y, z);
}
 
private:
 
QAccelerometer* m_sensor;
};

[edit] Using the Location API to get current position

#include <QGeoPositionInfo>
#include <QGeoPositionInfoSource>
 
// Neccessary for Qt Mobility API usage
QTM_USE_NAMESPACE
 
class LocationInfo : public QObject
{
Q_OBJECT
 
public:
 
LocationInfo(QObject* parent = 0) : QObject(parent)
{
QGeoPositionInfoSource* src = QGeoPositionInfoSource::createDefaultSource(this);
if (src)
{
connect(src, SIGNAL(positionUpdated(QGeoPositionInfo)), this,
SLOT(updatePosition(QGeoPositionInfo));
connect(src, SIGNAL(updateTimeout()), this, SLOT(updateTimeout()));
src->requestUpdate(5000); // Start request for actual position
}
}
 
private slots:
 
void updatePosition(const QGeoPositionInfo& info)
{
qDebug() << “Current position:<< info;
}
 
void updateTimeout()
{
// Current location could not be retrieved within the specified timeout of 5 seconds.
qWarning(“Failed to retrieve current position.”);
}
};

[edit] Using the Contacts API

Create an example contact and populate it with data

// Construct contact manager for default contact backend
QContactManager* cm = new QContactManager();
// Create example contact
QContact example;
 
// Add contact name
QContactName name;
name.setFirst(“John”);
name.setLast(“Doe”);
example.saveDetail(&name);
 
// Add contact email address
QContactEmailAddress email;
email.setContexts(QContactDetail::ContextHome);
email.setEmailAddress(“john.doe@example.com);
example.saveDetail(&email);
 
// Finally, save the contact details
cm->saveContact(&example);

Add custom field Interests to a note detail associated with a contact

// Get the contact definition we want to modify (cm is a 
// previously created QContactManager object)
QMap<QString, QContactDetailDefinition> def = cm->detailDefinitions();
QContactDetailDefinition modified = def.value(QContactNote::DefinitionName);
 
// Define new field ‘Interests’
QContactDetailDefinitionField field;
field.setDataType(QVariant::String);
QMap<QString, QContactDetailDefinitionField> fields = modified.fields();
 
// Insert new field to the list of contact’s fields and update
// the contact’s field definitions
fields.insert(“Interests”, field);
modified.setFields(fields);
 
// Save the updated field definitions back to the manager
cm->saveDetailDefinition(modified);

Search for a contact based on an incoming call ID

// Create new contact detail filter
QContactDetailFilter filter;
filter.setDetailDefinitionName(QContactPhoneNumber::DefinitionName,
QContactPhoneNumber::FieldNumber);
 
// Specify the requested value and filter matching criteria
QString incomingNbr(+436641234567);
filter.setValue(incomingNbr);
filter.setMatchFlags(QContactFilter::MatchExactly);
 
// Fetch matching contacts list for specified filter (cm is a
// previously created QContactManager object)
QList<QContactLocalId> matches = cm->contacts(filter);
 
for (int i = 0; i < matches.size(); ++i)
qDebug() << cm->contact(matches.at(i));


[edit] Using the Message API to send an email message with a photo attachment

// Setup new email message
QMessage msg;
msg.setType(QMessage::Email);
 
// Set recipient for our email message
QString recipient(“john.doe@example.com);
msg.setTo(QMessageAddress(QMessageAddress::Email, recipient));
 
// Define message subject, body and append attachment
msg.setSubject(“Messaging API example”);
msg.setBody(“Hello,\n\nthis is an example message.”);
 
QStringList attachments;
attachments << “images/example.png;
msg.appendAttachments(attachments);
 
// Send message using a new service handle
QMessageService* svc = new QMessageService();
 
if (svc->send(msg))
qDebug(“Successfully sent message.”);
else
qWarning(“Failed to send message.”);
This page was last modified on 11 October 2012, at 04:23.
105 page views in the last 30 days.
×