diff options
author | Michael Zanetti <michael.zanetti@nokia.com> | 2011-12-09 15:27:12 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-01-23 15:57:18 +0100 |
commit | ebd7a5d2935b5ab9dd7910dfb104a9e0c0819677 (patch) | |
tree | 2bc160feb75abdf72d29591efe301c55ba9b3132 /examples | |
parent | f54bd71301325f7e5681be49d81823986f5cbfcd (diff) |
added documentation and examples
Change-Id: Ia99e5f8f9579c189379bce1a22de3d58b12ce5fc
Reviewed-by: Alex <alex.blasche@nokia.com>
Sanity-Review: Alex <alex.blasche@nokia.com>
Diffstat (limited to 'examples')
70 files changed, 6024 insertions, 0 deletions
diff --git a/examples/annotatedurl/annotatedurl.cpp b/examples/annotatedurl/annotatedurl.cpp new file mode 100644 index 00000000..9b608931 --- /dev/null +++ b/examples/annotatedurl/annotatedurl.cpp @@ -0,0 +1,119 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNfc module. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "annotatedurl.h" + +#include <qnearfieldtarget.h> +#include <qndefmessage.h> +#include <qndefrecord.h> +#include <qndefnfctextrecord.h> +#include <qndefnfcurirecord.h> + +#include <QtCore/QUrl> +#include <QtCore/QLocale> + +#include <QGridLayout> +#include <QLabel> +#include <QMouseEvent> +#include <QDesktopServices> + +AnnotatedUrl::AnnotatedUrl(QObject *parent) +: QObject(parent) +{ +} + +AnnotatedUrl::~AnnotatedUrl() +{ +} + +void AnnotatedUrl::handleMessage(const QNdefMessage &message, QNearFieldTarget *target) +{ + Q_UNUSED(target); + + enum { + MatchedNone, + MatchedFirst, + MatchedEnglish, + MatchedLanguage, + MatchedLanguageAndCountry + } bestMatch = MatchedNone; + + QLocale defaultLocale; + + QString title; + QUrl url; + QPixmap pixmap; + + foreach (const QNdefRecord &record, message) { + if (record.isRecordType<QNdefNfcTextRecord>()) { + QNdefNfcTextRecord textRecord(record); + + QLocale locale(textRecord.locale()); + + // already found best match + if (bestMatch == MatchedLanguageAndCountry) { + // do nothing + } else if (bestMatch <= MatchedLanguage && locale == defaultLocale) { + title = textRecord.text(); + bestMatch = MatchedLanguageAndCountry; + } else if (bestMatch <= MatchedEnglish && + locale.language() == defaultLocale.language()) { + title = textRecord.text(); + bestMatch = MatchedLanguage; + } else if (bestMatch <= MatchedFirst && locale.language() == QLocale::English) { + title = textRecord.text(); + bestMatch = MatchedEnglish; + } else if (bestMatch == MatchedNone) { + title = textRecord.text(); + bestMatch = MatchedFirst; + } + } else if (record.isRecordType<QNdefNfcUriRecord>()) { + QNdefNfcUriRecord uriRecord(record); + + url = uriRecord.uri(); + } else if (record.typeNameFormat() == QNdefRecord::Mime && + record.type().startsWith("image/")) { + pixmap = QPixmap::fromImage(QImage::fromData(record.payload())); + } + } + + emit annotatedUrl(url, title, pixmap); +} + diff --git a/examples/annotatedurl/annotatedurl.h b/examples/annotatedurl/annotatedurl.h new file mode 100644 index 00000000..5b050105 --- /dev/null +++ b/examples/annotatedurl/annotatedurl.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNfc module. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ANNOTATEDURL_H +#define ANNOTATEDURL_H + +#include <qnfcglobal.h> + +#include <QtCore/QObject> + +QT_FORWARD_DECLARE_CLASS(QUrl) +QT_FORWARD_DECLARE_CLASS(QPixmap) + +QTNFC_BEGIN_NAMESPACE +class QNearFieldTarget; +class QNdefMessage; +QTNFC_END_NAMESPACE + +QTNFC_USE_NAMESPACE + +class AnnotatedUrl : public QObject +{ + Q_OBJECT + +public: + explicit AnnotatedUrl(QObject *parent = 0); + ~AnnotatedUrl(); + +signals: + void annotatedUrl(const QUrl &url, const QString &title, const QPixmap &pixmap); + +public slots: + void handleMessage(const QNdefMessage &message, QNearFieldTarget *target); +}; + +#endif // ANNOTATEDURL_H diff --git a/examples/annotatedurl/annotatedurl.pro b/examples/annotatedurl/annotatedurl.pro new file mode 100644 index 00000000..c9a22afa --- /dev/null +++ b/examples/annotatedurl/annotatedurl.pro @@ -0,0 +1,18 @@ +QT += nfc widgets + +CONFIG += strict_flags + +INCLUDEPATH += $$PWD/../../src/connectivity/nfc +DEPENDPATH += $$PWD/../../src/connectivity/nfc + +TARGET = annotatedurl + +SOURCES += main.cpp \ + mainwindow.cpp \ + annotatedurl.cpp + +HEADERS += mainwindow.h \ + annotatedurl.h + +FORMS += mainwindow.ui + diff --git a/examples/annotatedurl/main.cpp b/examples/annotatedurl/main.cpp new file mode 100644 index 00000000..55dc6571 --- /dev/null +++ b/examples/annotatedurl/main.cpp @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNfc module. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "annotatedurl.h" +#include "mainwindow.h" + +#include <qnearfieldmanager.h> +#include <qndefnfctextrecord.h> +#include <qndefnfcurirecord.h> + +#include <QtCore/QLocale> + +#include <QApplication> + + +int main(int argc, char *argv[]) +{ + //QLocale::setDefault(QLocale(QLocale::Japanese)); + + QApplication a(argc, argv); + MainWindow mainWindow; + + QNearFieldManager manager; + AnnotatedUrl annotatedUrl; + + QNdefFilter filter; + filter.setOrderMatch(false); + filter.appendRecord<QNdefNfcTextRecord>(1, UINT_MAX); + filter.appendRecord<QNdefNfcUriRecord>(); + manager.registerNdefMessageHandler(filter, &annotatedUrl, + SLOT(handleMessage(QNdefMessage,QNearFieldTarget*))); + + QObject::connect(&annotatedUrl, SIGNAL(annotatedUrl(QUrl,QString,QPixmap)), + &mainWindow, SLOT(displayAnnotatedUrl(QUrl,QString,QPixmap))); + +#if defined(Q_WS_S60) || defined(Q_WS_MAEMO_6) || defined(Q_WS_MEEGO) + mainWindow.showFullScreen(); +#else + mainWindow.show(); +#endif + + return a.exec(); +} diff --git a/examples/annotatedurl/mainwindow.cpp b/examples/annotatedurl/mainwindow.cpp new file mode 100644 index 00000000..6cd4f00a --- /dev/null +++ b/examples/annotatedurl/mainwindow.cpp @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNfc module. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "mainwindow.h" +#include "ui_mainwindow.h" + +#include <QtCore/QUrl> +#include <QtGui/QMouseEvent> +#include <QtGui/QDesktopServices> + +MainWindow::MainWindow(QWidget *parent) +: QMainWindow(parent), ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::displayAnnotatedUrl(const QUrl &url, const QString &title, const QPixmap &pixmap) +{ + ui->m_help->setHidden(true); + + ui->m_url->setText(url.toString()); + ui->m_title->setText(title); + ui->m_image->setPixmap(pixmap); +} + +void MainWindow::mouseReleaseEvent(QMouseEvent *event) +{ + if (ui->centralWidget->rect().contains(event->pos())) + QDesktopServices::openUrl(QUrl(ui->m_url->text())); +} diff --git a/examples/annotatedurl/mainwindow.h b/examples/annotatedurl/mainwindow.h new file mode 100644 index 00000000..d612c13a --- /dev/null +++ b/examples/annotatedurl/mainwindow.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNfc module. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +QT_FORWARD_DECLARE_CLASS(QUrl) + +namespace Ui { + class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +public slots: + void displayAnnotatedUrl(const QUrl &url, const QString &title, const QPixmap &pixmap); + +protected: + void mouseReleaseEvent(QMouseEvent *event); + +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/examples/annotatedurl/mainwindow.ui b/examples/annotatedurl/mainwindow.ui new file mode 100644 index 00000000..26e3b302 --- /dev/null +++ b/examples/annotatedurl/mainwindow.ui @@ -0,0 +1,61 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>MainWindow</class> + <widget class="QMainWindow" name="MainWindow"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>590</width> + <height>420</height> + </rect> + </property> + <property name="windowTitle"> + <string>MainWindow</string> + </property> + <widget class="QWidget" name="centralWidget"> + <layout class="QGridLayout" name="gridLayout" rowstretch="1,0,1" columnstretch="0,0,0"> + <item row="0" column="1" colspan="2"> + <widget class="QLabel" name="m_title"> + <property name="font"> + <font> + <weight>75</weight> + <bold>true</bold> + </font> + </property> + <property name="text"> + <string/> + </property> + </widget> + </item> + <item row="0" column="0" rowspan="3"> + <widget class="QLabel" name="m_image"> + <property name="text"> + <string/> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QLabel" name="m_help"> + <property name="text"> + <string>Touch a tag</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + <item row="2" column="1" colspan="2"> + <widget class="QLabel" name="m_url"> + <property name="text"> + <string/> + </property> + </widget> + </item> + </layout> + </widget> + </widget> + <layoutdefault spacing="6" margin="11"/> + <resources/> + <connections/> +</ui> diff --git a/examples/bluetoothtransferplugin/bluetoothtransfer.cpp b/examples/bluetoothtransferplugin/bluetoothtransfer.cpp new file mode 100644 index 00000000..87843ffc --- /dev/null +++ b/examples/bluetoothtransferplugin/bluetoothtransfer.cpp @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtCore> + +#include "bluetoothtransfer.h" + +BluetoothTransfer::BluetoothTransfer(QObject *parent) + : QObject(parent) +{ +} + +//! [sendFile] +void BluetoothTransfer::sendFile(const QString &path) +{ + qDebug() << "BluetoothTransfer::sendFile()" << path; +} +//! [sendFile] diff --git a/examples/bluetoothtransferplugin/bluetoothtransfer.h b/examples/bluetoothtransferplugin/bluetoothtransfer.h new file mode 100644 index 00000000..95a8969f --- /dev/null +++ b/examples/bluetoothtransferplugin/bluetoothtransfer.h @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef BLUETOOTHTRANSFER_H +#define BLUETOOTHTRANSFER_H + +#include <QObject> + +class BluetoothTransfer : public QObject +{ + Q_OBJECT +public: + BluetoothTransfer(QObject *parent = 0); + +public slots: + void sendFile(const QString &path); +}; + + +#endif diff --git a/examples/bluetoothtransferplugin/bluetoothtransferplugin.cpp b/examples/bluetoothtransferplugin/bluetoothtransferplugin.cpp new file mode 100644 index 00000000..a656ff6a --- /dev/null +++ b/examples/bluetoothtransferplugin/bluetoothtransferplugin.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <qserviceinterfacedescriptor.h> + +#include "bluetoothtransferplugin.h" +#include "bluetoothtransfer.h" + +QObject* BluetoothTransferPlugin::createInstance(const QServiceInterfaceDescriptor& descriptor) +{ + Q_UNUSED(descriptor); + //! [createinstance] + return new BluetoothTransfer(this); + //! [createinstance] +} + +Q_EXPORT_PLUGIN2(serviceframework_bluetoothtransferplugin, BluetoothTransferPlugin) diff --git a/examples/bluetoothtransferplugin/bluetoothtransferplugin.h b/examples/bluetoothtransferplugin/bluetoothtransferplugin.h new file mode 100644 index 00000000..814df199 --- /dev/null +++ b/examples/bluetoothtransferplugin/bluetoothtransferplugin.h @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef BLUETOOTHTRANSFERPLUGIN_H +#define BLUETOOTHTRANSFERPLUGIN_H + +#include <QObject> + +#include <qserviceplugininterface.h> + +class BluetoothTransferPlugin : public QObject, + public QServicePluginInterface +{ + Q_OBJECT + Q_INTERFACES(QServicePluginInterface) +public: + QObject* createInstance(const QServiceInterfaceDescriptor& descriptor); +}; + +#endif diff --git a/examples/bluetoothtransferplugin/bluetoothtransferplugin.pro b/examples/bluetoothtransferplugin/bluetoothtransferplugin.pro new file mode 100644 index 00000000..214deb41 --- /dev/null +++ b/examples/bluetoothtransferplugin/bluetoothtransferplugin.pro @@ -0,0 +1,16 @@ +TEMPLATE = lib +CONFIG += plugin +PLUGIN_TYPE=serviceframework + +QT += bluetooth serviceframework + +HEADERS += bluetoothtransferplugin.h \ + bluetoothtransfer.h +SOURCES += bluetoothtransferplugin.cpp \ + bluetoothtransfer.cpp +TARGET = serviceframework_bluetoothtransferplugin +DESTDIR = . + +xml.path = $$QT_MOBILITY_EXAMPLES/xmldata +xml.files = bluetoothtransferservice.xml +INSTALLS += xml diff --git a/examples/bluetoothtransferplugin/bluetoothtransferservice.xml b/examples/bluetoothtransferplugin/bluetoothtransferservice.xml new file mode 100644 index 00000000..eeeb76be --- /dev/null +++ b/examples/bluetoothtransferplugin/bluetoothtransferservice.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="utf-8" ?> +<SFW version="1.1"> +<service> + <name>BluetoothTransferService</name> + <filepath>serviceframework_bluetoothtransferplugin</filepath> + <description>Example Bluetooth transfer service</description> + <interface> + <name>com.nokia.qt.examples.FileTransfer</name> + <version>1.0</version> + <description>Implementation of FileTransferInterface</description> + </interface> +</service> +</SFW> diff --git a/examples/btchat/btchat.pro b/examples/btchat/btchat.pro new file mode 100644 index 00000000..3df3527d --- /dev/null +++ b/examples/btchat/btchat.pro @@ -0,0 +1,27 @@ +TARGET = btchat + +QT += bluetooth widgets + +INCLUDEPATH += ../../src/connectivity/bluetooth +DEPENDPATH += ../../src/connectivity/bluetooth + +SOURCES = \ + main.cpp \ + chat.cpp \ + remoteselector.cpp \ + chatserver.cpp \ + chatclient.cpp + +HEADERS = \ + chat.h \ + remoteselector.h \ + chatserver.h \ + chatclient.h + +FORMS = \ + chat.ui \ + remoteselector.ui + +symbian { + TARGET.CAPABILITY = LocalServices UserEnvironment ReadUserData WriteUserData NetworkServices NetworkControl ReadDeviceData WriteDeviceData SwEvent +} diff --git a/examples/btchat/chat.cpp b/examples/btchat/chat.cpp new file mode 100644 index 00000000..4dcb135e --- /dev/null +++ b/examples/btchat/chat.cpp @@ -0,0 +1,179 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "chat.h" +#include "remoteselector.h" +#include "chatserver.h" +#include "chatclient.h" + +#include <qbluetoothuuid.h> +#include <qrfcommserver.h> +#include <qbluetoothservicediscoveryagent.h> +#include <qbluetoothdeviceinfo.h> +#include <qbluetoothlocaldevice.h> + +#include <QTimer> + +#include <QDebug> + +static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8"); + +Chat::Chat(QWidget *parent) +: QDialog(parent), ui(new Ui_Chat) +{ + //! [Construct UI] + ui->setupUi(this); + +#if defined (Q_OS_SYMBIAN) || defined(Q_OS_WINCE) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6) + setWindowState(Qt::WindowFullScreen); +#endif + + connect(ui->quitButton, SIGNAL(clicked()), this, SLOT(accept())); + connect(ui->connectButton, SIGNAL(clicked()), this, SLOT(connectClicked())); + connect(ui->sendButton, SIGNAL(clicked()), this, SLOT(sendClicked())); + //! [Construct UI] + + //! [Create Chat Server] + server = new ChatServer(this); + connect(server, SIGNAL(clientConnected(QString)), this, SLOT(clientConnected(QString))); + connect(server, SIGNAL(clientDisconnected(QString)), this, SLOT(clientDisconnected(QString))); + connect(server, SIGNAL(messageReceived(QString,QString)), + this, SLOT(showMessage(QString,QString))); + connect(this, SIGNAL(sendMessage(QString)), server, SLOT(sendMessage(QString))); + server->startServer(); + //! [Create Chat Server] + + //! [Get local device name] + localName = QBluetoothLocalDevice().name(); + //! [Get local device name] +} + +Chat::~Chat() +{ + qDeleteAll(clients); + delete server; +} + +//! [clientConnected clientDisconnected] +void Chat::clientConnected(const QString &name) +{ + ui->chat->insertPlainText(QString::fromLatin1("%1 has joined chat.\n").arg(name)); +} + +void Chat::clientDisconnected(const QString &name) +{ + ui->chat->insertPlainText(QString::fromLatin1("%1 has left.\n").arg(name)); +} +//! [clientConnected clientDisconnected] + +//! [connected] +void Chat::connected(const QString &name) +{ + ui->chat->insertPlainText(QString::fromLatin1("Joined chat with %1.\n").arg(name)); +} +//! [connected] + +//! [clientDisconnected] +void Chat::clientDisconnected() +{ + ChatClient *client = qobject_cast<ChatClient *>(sender()); + if (client) { + clients.removeOne(client); + client->deleteLater(); + } +} +//! [clientDisconnected] + +//! [Connect to remote service] +void Chat::connectClicked() +{ + ui->connectButton->setEnabled(false); + + // scan for services + RemoteSelector remoteSelector; + remoteSelector.startDiscovery(QBluetoothUuid(serviceUuid)); + if (remoteSelector.exec() == QDialog::Accepted) { + QBluetoothServiceInfo service = remoteSelector.service(); + + qDebug() << "Connecting to service 2" << service.serviceName() + << "on" << service.device().name(); + + // Create client + qDebug() << "Going to create client"; + ChatClient *client = new ChatClient(this); +qDebug() << "Connecting..."; + + connect(client, SIGNAL(messageReceived(QString,QString)), + this, SLOT(showMessage(QString,QString))); + connect(client, SIGNAL(disconnected()), this, SLOT(clientDisconnected())); + connect(client, SIGNAL(connected(QString)), this, SLOT(connected(QString))); + connect(this, SIGNAL(sendMessage(QString)), client, SLOT(sendMessage(QString))); +qDebug() << "Start client"; + client->startClient(service); + + clients.append(client); + } + + ui->connectButton->setEnabled(true); +} +//! [Connect to remote service] + +//! [sendClicked] +void Chat::sendClicked() +{ + ui->sendButton->setEnabled(false); + ui->sendText->setEnabled(false); + + showMessage(localName, ui->sendText->text()); + emit sendMessage(ui->sendText->text()); + + ui->sendText->clear(); + + ui->sendText->setEnabled(true); + ui->sendButton->setEnabled(true); +} +//! [sendClicked] + +//! [showMessage] +void Chat::showMessage(const QString &sender, const QString &message) +{ + ui->chat->insertPlainText(QString::fromLatin1("%1: %2\n").arg(sender, message)); +} +//! [showMessage] diff --git a/examples/btchat/chat.h b/examples/btchat/chat.h new file mode 100644 index 00000000..ff46fff0 --- /dev/null +++ b/examples/btchat/chat.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "ui_chat.h" + +#include <QDialog> + +#include <qbluetoothserviceinfo.h> +#include <qbluetoothsocket.h> + +#include <QDebug> + +QTBLUETOOTH_BEGIN_NAMESPACE +class QRfcommServer; +QTBLUETOOTH_END_NAMESPACE + +QTBLUETOOTH_USE_NAMESPACE + +class ChatServer; +class ChatClient; + +//! [declaration] +class Chat : public QDialog +{ + Q_OBJECT + +public: + Chat(QWidget *parent = 0); + ~Chat(); + +signals: + void sendMessage(const QString &message); + +private slots: + void connectClicked(); + void sendClicked(); + + void showMessage(const QString &sender, const QString &message); + + void clientConnected(const QString &name); + void clientDisconnected(const QString &name); + void clientDisconnected(); + void connected(const QString &name); + +private: + Ui_Chat *ui; + + ChatServer *server; + QList<ChatClient *> clients; + + QString localName; +}; +//! [declaration] diff --git a/examples/btchat/chat.ui b/examples/btchat/chat.ui new file mode 100644 index 00000000..acebc937 --- /dev/null +++ b/examples/btchat/chat.ui @@ -0,0 +1,76 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>Chat</class> + <widget class="QDialog" name="Chat"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>400</width> + <height>300</height> + </rect> + </property> + <property name="windowTitle"> + <string>Bluetooth Chat</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QTextEdit" name="chat"> + <property name="focusPolicy"> + <enum>Qt::NoFocus</enum> + </property> + </widget> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <widget class="QLineEdit" name="sendText"/> + </item> + <item> + <widget class="QPushButton" name="sendButton"> + <property name="text"> + <string>Send</string> + </property> + <property name="default"> + <bool>true</bool> + </property> + </widget> + </item> + </layout> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_2"> + <item> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QPushButton" name="connectButton"> + <property name="text"> + <string>Connect</string> + </property> + </widget> + </item> + </layout> + </item> + <item> + <widget class="QPushButton" name="quitButton"> + <property name="text"> + <string>Quit</string> + </property> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/examples/btchat/chatclient.cpp b/examples/btchat/chatclient.cpp new file mode 100644 index 00000000..ceeec732 --- /dev/null +++ b/examples/btchat/chatclient.cpp @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "chatclient.h" + +#include <qbluetoothsocket.h> + +ChatClient::ChatClient(QObject *parent) +: QObject(parent), socket(0) +{ +} + +ChatClient::~ChatClient() +{ + stopClient(); +} + +//! [startClient] +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())); +} +//! [startClient] + +//! [stopClient] +void ChatClient::stopClient() +{ + delete socket; + socket = 0; +} +//! [stopClient] + +//! [readSocket] +void ChatClient::readSocket() +{ + if (!socket) + return; + + while (socket->canReadLine()) { + QByteArray line = socket->readLine(); + emit messageReceived(socket->peerName(), + QString::fromUtf8(line.constData(), line.length())); + } +} +//! [readSocket] + +//! [sendMessage] +void ChatClient::sendMessage(const QString &message) +{ + QByteArray text = message.toUtf8() + '\n'; + socket->write(text); +} +//! [sendMessage] + +//! [connected] +void ChatClient::connected() +{ + emit connected(socket->peerName()); +} +//! [connected] diff --git a/examples/btchat/chatclient.h b/examples/btchat/chatclient.h new file mode 100644 index 00000000..975abb06 --- /dev/null +++ b/examples/btchat/chatclient.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef CHATCLIENT_H +#define CHATCLIENT_H + +#include <qbluetoothserviceinfo.h> + +#include <QtCore/QObject> + +QTBLUETOOTH_BEGIN_NAMESPACE +class QBluetoothSocket; +QTBLUETOOTH_END_NAMESPACE + +QTBLUETOOTH_USE_NAMESPACE + +//! [declaration] +class ChatClient : public QObject +{ + Q_OBJECT + +public: + explicit ChatClient(QObject *parent = 0); + ~ChatClient(); + + void startClient(const QBluetoothServiceInfo &remoteService); + void stopClient(); + +public slots: + void sendMessage(const QString &message); + +signals: + void messageReceived(const QString &sender, const QString &message); + void connected(const QString &name); + void disconnected(); + +private slots: + void readSocket(); + void connected(); + +private: + QBluetoothSocket *socket; +}; +//! [declaration] + +#endif // CHATCLIENT_H diff --git a/examples/btchat/chatserver.cpp b/examples/btchat/chatserver.cpp new file mode 100644 index 00000000..9a9ac0b1 --- /dev/null +++ b/examples/btchat/chatserver.cpp @@ -0,0 +1,182 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "chatserver.h" + +#include <qrfcommserver.h> +#include <qbluetoothsocket.h> + +//! [Service UUID] +static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8"); +//! [Service UUID] + +ChatServer::ChatServer(QObject *parent) +: QObject(parent), rfcommServer(0) +{ +} + +ChatServer::~ChatServer() +{ + stopServer(); +} + +void ChatServer::startServer() +{ + if (rfcommServer) + return; + + //! [Create the server] + rfcommServer = new QRfcommServer(this); + connect(rfcommServer, SIGNAL(newConnection()), this, SLOT(clientConnected())); + rfcommServer->listen(); + //! [Create the server] + + serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceRecordHandle, (uint)0x00010010); + + //! [Class Uuuid must contain at least 1 entry] + QBluetoothServiceInfo::Sequence classId; + classId << QVariant::fromValue(QBluetoothUuid(serviceUuid)); + serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId); + //! [Class Uuuid must contain at least 1 entry] + + + //! [Service name, description and provider] + serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, tr("Bt Chat Server")); + serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceDescription, + tr("Example bluetooth chat server")); + serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider, tr("Nokia, QtDF")); + //! [Service name, description and provider] + + //! [Service UUID set] + serviceInfo.setServiceUuid(QBluetoothUuid(serviceUuid)); + //! [Service UUID set] + + //! [Service Discoverability] + serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList, + QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup)); + //! [Service Discoverability] + + //! [Protocol descriptor list] + QBluetoothServiceInfo::Sequence protocolDescriptorList; + QBluetoothServiceInfo::Sequence protocol; + protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap)); + protocolDescriptorList.append(QVariant::fromValue(protocol)); + protocol.clear(); + protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)) + << QVariant::fromValue(quint8(rfcommServer->serverPort())); + protocolDescriptorList.append(QVariant::fromValue(protocol)); + serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, + protocolDescriptorList); + //! [Protocol descriptor list] + + //! [Register service] + serviceInfo.registerService(); + //! [Register service] +} + +//! [stopServer] +void ChatServer::stopServer() +{ + // Unregister service + serviceInfo.unregisterService(); + + // Close sockets + qDeleteAll(clientSockets); + + // Close server + delete rfcommServer; + rfcommServer = 0; +} +//! [stopServer] + +//! [sendMessage] +void ChatServer::sendMessage(const QString &message) +{ + QByteArray text = message.toUtf8() + '\n'; + + foreach (QBluetoothSocket *socket, clientSockets) + socket->write(text); +} +//! [sendMessage] + +//! [clientConnected] +void ChatServer::clientConnected() +{ + QBluetoothSocket *socket = rfcommServer->nextPendingConnection(); + if (!socket) + return; + + connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket())); + connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected())); + clientSockets.append(socket); + + emit clientConnected(socket->peerName()); +} +//! [clientConnected] + +//! [clientDisconnected] +void ChatServer::clientDisconnected() +{ + QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender()); + if (!socket) + return; + + emit clientDisconnected(socket->peerName()); + + clientSockets.removeOne(socket); + + socket->deleteLater(); +} +//! [clientDisconnected] + +//! [readSocket] +void ChatServer::readSocket() +{ + QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender()); + if (!socket) + return; + + while (socket->canReadLine()) { + QByteArray line = socket->readLine().trimmed(); + emit messageReceived(socket->peerName(), + QString::fromUtf8(line.constData(), line.length())); + } +} +//! [readSocket] diff --git a/examples/btchat/chatserver.h b/examples/btchat/chatserver.h new file mode 100644 index 00000000..cdab7310 --- /dev/null +++ b/examples/btchat/chatserver.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef CHATSERVER_H +#define CHATSERVER_H + +#include <qbluetoothserviceinfo.h> + +#include <QtCore/QObject> +#include <QtCore/QList> + +QTBLUETOOTH_BEGIN_NAMESPACE +class QRfcommServer; +class QBluetoothSocket; +QTBLUETOOTH_END_NAMESPACE + +QTBLUETOOTH_USE_NAMESPACE + +//! [declaration] +class ChatServer : public QObject +{ + Q_OBJECT + +public: + explicit ChatServer(QObject *parent = 0); + ~ChatServer(); + + void startServer(); + void stopServer(); + +public slots: + void sendMessage(const QString &message); + +signals: + void messageReceived(const QString &sender, const QString &message); + void clientConnected(const QString &name); + void clientDisconnected(const QString &name); + +private slots: + void clientConnected(); + void clientDisconnected(); + void readSocket(); + +private: + QRfcommServer *rfcommServer; + QBluetoothServiceInfo serviceInfo; + QList<QBluetoothSocket *> clientSockets; +}; +//! [declaration] + +#endif // CHATSERVER_H diff --git a/examples/btchat/main.cpp b/examples/btchat/main.cpp new file mode 100644 index 00000000..10032ca8 --- /dev/null +++ b/examples/btchat/main.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "chat.h" + +#include <QApplication> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + Chat d; + QObject::connect(&d, SIGNAL(accepted()), &app, SLOT(quit())); + +#if defined(Q_WS_MAEMO_6) || defined(Q_OS_SYMBIAN) + d.showFullScreen(); +#else + d.show(); +#endif + + app.exec(); + + return 0; +} + diff --git a/examples/btchat/remoteselector.cpp b/examples/btchat/remoteselector.cpp new file mode 100644 index 00000000..cc009dd6 --- /dev/null +++ b/examples/btchat/remoteselector.cpp @@ -0,0 +1,146 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "remoteselector.h" +#include "ui_remoteselector.h" + +#include <qbluetoothdeviceinfo.h> +#include <qbluetoothaddress.h> + +QTBLUETOOTH_USE_NAMESPACE + +RemoteSelector::RemoteSelector(QWidget *parent) +: QDialog(parent), ui(new Ui::RemoteSelector), + m_discoveryAgent(new QBluetoothServiceDiscoveryAgent) +{ + ui->setupUi(this); + +#if defined (Q_OS_SYMBIAN) || defined(Q_OS_WINCE) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6) + setWindowState(Qt::WindowFullScreen); +#endif + + connect(m_discoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), + this, SLOT(serviceDiscovered(QBluetoothServiceInfo))); + connect(m_discoveryAgent, SIGNAL(finished()), this, SLOT(discoveryFinished())); +} + +RemoteSelector::~RemoteSelector() +{ + delete ui; +} + +void RemoteSelector::startDiscovery(const QBluetoothUuid &uuid) +{ + if (m_discoveryAgent->isActive()) + m_discoveryAgent->stop(); + + ui->remoteDevices->clear(); + + m_discoveryAgent->setUuidFilter(uuid); + m_discoveryAgent->start(); + + ui->status->setText(tr("Scanning...")); +} + +void RemoteSelector::stopDiscovery() +{ + if (m_discoveryAgent){ + m_discoveryAgent->stop(); + } +} + +QBluetoothServiceInfo RemoteSelector::service() const +{ + return m_service; +} + +void RemoteSelector::serviceDiscovered(const QBluetoothServiceInfo &serviceInfo) +{ +#if 0 + qDebug() << "Discovered service on" + << serviceInfo.device().name() << serviceInfo.device().address().toString(); + qDebug() << "\tService name:" << serviceInfo.serviceName(); + qDebug() << "\tDescription:" + << serviceInfo.attribute(QBluetoothServiceInfo::ServiceDescription).toString(); + qDebug() << "\tProvider:" + << serviceInfo.attribute(QBluetoothServiceInfo::ServiceProvider).toString(); + qDebug() << "\tL2CAP protocol service multiplexer:" + << serviceInfo.protocolServiceMultiplexer(); + qDebug() << "\tRFCOMM server channel:" << serviceInfo.serverChannel(); +#endif + QMapIterator<QListWidgetItem *, QBluetoothServiceInfo> i(m_discoveredServices); + while (i.hasNext()){ + i.next(); + if (serviceInfo.device().address() == i.value().device().address()){ + return; + } + } + + QString remoteName; + if (serviceInfo.device().name().isEmpty()) + remoteName = serviceInfo.device().address().toString(); + else + remoteName = serviceInfo.device().name(); + + QListWidgetItem *item = + new QListWidgetItem(QString::fromLatin1("%1 %2").arg(remoteName, + serviceInfo.serviceName())); + + m_discoveredServices.insert(item, serviceInfo); + ui->remoteDevices->addItem(item); +} + +void RemoteSelector::discoveryFinished() +{ + ui->status->setText(tr("Select the chat service to connect to.")); +} + +void RemoteSelector::on_remoteDevices_itemActivated(QListWidgetItem *item) +{ + qDebug() << "got click" << item->text(); + m_service = m_discoveredServices.value(item); + + accept(); +} + +void RemoteSelector::on_cancelButton_clicked() +{ + reject(); +} diff --git a/examples/btchat/remoteselector.h b/examples/btchat/remoteselector.h new file mode 100644 index 00000000..8125de0d --- /dev/null +++ b/examples/btchat/remoteselector.h @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef REMOTESELECTOR_H +#define REMOTESELECTOR_H + +#include <QDialog> + +#include <qbluetoothuuid.h> +#include <qbluetoothserviceinfo.h> +#include <qbluetoothservicediscoveryagent.h> + +QT_FORWARD_DECLARE_CLASS(QModelIndex) +QT_FORWARD_DECLARE_CLASS(QListWidgetItem) + +QTBLUETOOTH_USE_NAMESPACE + +namespace Ui { + class RemoteSelector; +} + +class RemoteSelector : public QDialog +{ + Q_OBJECT + +public: + explicit RemoteSelector(QWidget *parent = 0); + ~RemoteSelector(); + + void startDiscovery(const QBluetoothUuid &uuid); + void stopDiscovery(); + QBluetoothServiceInfo service() const; + +private: + Ui::RemoteSelector *ui; + + QBluetoothServiceDiscoveryAgent *m_discoveryAgent; + QBluetoothServiceInfo m_service; + QMap<QListWidgetItem *, QBluetoothServiceInfo> m_discoveredServices; + +private slots: + void serviceDiscovered(const QBluetoothServiceInfo &serviceInfo); + void discoveryFinished(); + void on_remoteDevices_itemActivated(QListWidgetItem *item); + void on_cancelButton_clicked(); +}; + +#endif // REMOTESELECTOR_H diff --git a/examples/btchat/remoteselector.ui b/examples/btchat/remoteselector.ui new file mode 100644 index 00000000..d415f416 --- /dev/null +++ b/examples/btchat/remoteselector.ui @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>RemoteSelector</class> + <widget class="QDialog" name="RemoteSelector"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>400</width> + <height>300</height> + </rect> + </property> + <property name="windowTitle"> + <string>Available chat services</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QLabel" name="status"> + <property name="text"> + <string>Scanning...</string> + </property> + </widget> + </item> + <item> + <widget class="QListWidget" name="remoteDevices"/> + </item> + <item> + <widget class="QPushButton" name="cancelButton"> + <property name="text"> + <string>Cancel</string> + </property> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/examples/btfiletransfer/btfiletransfer.pro b/examples/btfiletransfer/btfiletransfer.pro new file mode 100644 index 00000000..7d823a17 --- /dev/null +++ b/examples/btfiletransfer/btfiletransfer.pro @@ -0,0 +1,34 @@ +TARGET = btfiletransfer + +QT += bluetooth widgets + +INCLUDEPATH += ../../src/connectivity/bluetooth +DEPENDPATH += ../../src/connectivity/bluetooth + +SOURCES = \ + main.cpp \ + remoteselector.cpp \ + progress.cpp \ + pindisplay.cpp + +HEADERS = \ + remoteselector.h \ + progress.h \ + pindisplay.h + +FORMS = \ + remoteselector.ui \ + progress.ui \ + pindisplay.ui + + +symbian { + TARGET.CAPABILITY = LocalServices UserEnvironment ReadUserData WriteUserData NetworkServices ReadDeviceData WriteDeviceData +} + +OTHER_FILES += \ + busy.gif \ + pairing.gif + +RESOURCES += \ + btfiletransfer.qrc diff --git a/examples/btfiletransfer/btfiletransfer.qrc b/examples/btfiletransfer/btfiletransfer.qrc new file mode 100644 index 00000000..10697d5b --- /dev/null +++ b/examples/btfiletransfer/btfiletransfer.qrc @@ -0,0 +1,6 @@ +<RCC> + <qresource prefix="/icons"> + <file>busy.gif</file> + <file>pairing.gif</file> + </qresource> +</RCC> diff --git a/examples/btfiletransfer/busy.gif b/examples/btfiletransfer/busy.gif Binary files differnew file mode 100644 index 00000000..e192ca89 --- /dev/null +++ b/examples/btfiletransfer/busy.gif diff --git a/examples/btfiletransfer/main.cpp b/examples/btfiletransfer/main.cpp new file mode 100644 index 00000000..09621b23 --- /dev/null +++ b/examples/btfiletransfer/main.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "remoteselector.h" + +#include <QApplication> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + RemoteSelector d; + QObject::connect(&d, SIGNAL(accepted()), &app, SLOT(quit())); + d.startDiscovery(); +#ifdef Q_OS_SYMBIAN + d.showFullScreen(); +#else + d.show(); +#endif + + app.exec(); + + return 0; +} + diff --git a/examples/btfiletransfer/pairing.gif b/examples/btfiletransfer/pairing.gif Binary files differnew file mode 100644 index 00000000..e147f3ec --- /dev/null +++ b/examples/btfiletransfer/pairing.gif diff --git a/examples/btfiletransfer/pindisplay.cpp b/examples/btfiletransfer/pindisplay.cpp new file mode 100644 index 00000000..13bd091a --- /dev/null +++ b/examples/btfiletransfer/pindisplay.cpp @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "pindisplay.h" +#include "ui_pindisplay.h" + +pinDisplay::pinDisplay(QString title, QString pin, QWidget *parent) : + QDialog(parent), + ui(new Ui::pinDisplay) +{ + ui->setupUi(this); + ui->label->setText(title); + ui->pin->setText(pin); + + connect(ui->buttonBox, SIGNAL(accepted()), this, SIGNAL(accepted())); + connect(ui->buttonBox, SIGNAL(rejected()), this, SIGNAL(rejected())); +} + +pinDisplay::~pinDisplay() +{ + delete ui; +} + +void pinDisplay::on_buttonBox_accepted() +{ + deleteLater(); +} + +void pinDisplay::setOkCancel() +{ + ui->buttonBox->addButton(QDialogButtonBox::Cancel); +} diff --git a/examples/btfiletransfer/pindisplay.h b/examples/btfiletransfer/pindisplay.h new file mode 100644 index 00000000..9a251042 --- /dev/null +++ b/examples/btfiletransfer/pindisplay.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PINDISPLAY_H +#define PINDISPLAY_H + +#include <QDialog> + +namespace Ui { + class pinDisplay; +} + +class pinDisplay : public QDialog +{ + Q_OBJECT + +public: + explicit pinDisplay(QString title, QString pin, QWidget *parent = 0); + ~pinDisplay(); + + void setOkCancel(); + +private Q_SLOTS: + void on_buttonBox_accepted(); + +private: + Ui::pinDisplay *ui; +}; + +#endif // PINDISPLAY_H diff --git a/examples/btfiletransfer/pindisplay.ui b/examples/btfiletransfer/pindisplay.ui new file mode 100644 index 00000000..02bf7d50 --- /dev/null +++ b/examples/btfiletransfer/pindisplay.ui @@ -0,0 +1,89 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>pinDisplay</class> + <widget class="QDialog" name="pinDisplay"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>240</width> + <height>153</height> + </rect> + </property> + <property name="windowTitle"> + <string>Enter Pin Code</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QLabel" name="pin"> + <property name="font"> + <font> + <family>Monospace</family> + <pointsize>24</pointsize> + <weight>75</weight> + <bold>true</bold> + </font> + </property> + <property name="text"> + <string>012345</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + <item> + <widget class="QLabel" name="label"> + <property name="text"> + <string>TextLabel</string> + </property> + </widget> + </item> + <item> + <widget class="QDialogButtonBox" name="buttonBox"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="standardButtons"> + <set>QDialogButtonBox::Ok</set> + </property> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections> + <connection> + <sender>buttonBox</sender> + <signal>accepted()</signal> + <receiver>pinDisplay</receiver> + <slot>accept()</slot> + <hints> + <hint type="sourcelabel"> + <x>248</x> + <y>254</y> + </hint> + <hint type="destinationlabel"> + <x>157</x> + <y>274</y> + </hint> + </hints> + </connection> + <connection> + <sender>buttonBox</sender> + <signal>rejected()</signal> + <receiver>pinDisplay</receiver> + <slot>reject()</slot> + <hints> + <hint type="sourcelabel"> + <x>316</x> + <y>260</y> + </hint> + <hint type="destinationlabel"> + <x>286</x> + <y>274</y> + </hint> + </hints> + </connection> + </connections> +</ui> diff --git a/examples/btfiletransfer/progress.cpp b/examples/btfiletransfer/progress.cpp new file mode 100644 index 00000000..77f16c1a --- /dev/null +++ b/examples/btfiletransfer/progress.cpp @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "progress.h" +#include "ui_progress.h" + +#include <qbluetoothdeviceinfo.h> +#include <qbluetoothaddress.h> +#include <qbluetoothtransferrequest.h> +#include <qbluetoothtransferreply.h> + +QTBLUETOOTH_USE_NAMESPACE + +Progress::Progress(QWidget *parent) : + QDialog(parent), + ui(new Ui::Progress) +{ + ui->setupUi(this); + ui->progressBar->setRange(0, 1); + connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(close())); +} + +Progress::~Progress() +{ + delete ui; +} + +void Progress::setStatus(QString title, QString filename) { + ui->titleLabel->setText(title); + ui->statusLabel->setText(filename); +} + +void Progress::finished(QBluetoothTransferReply *reply){ + if (reply->error() != QBluetoothTransferReply::NoError){ + ui->progressBar->setDisabled(true); + ui->statusLabel->setText(tr("Failed: %1").arg(reply->errorString())); + } + else { + ui->statusLabel->setText(tr("Transfer complete")); + } + ui->cancelButton->setText(tr("Dismiss")); +} + +void Progress::uploadProgress(qint64 bytesSent, qint64 bytesTotal) +{ + if (bytesSent == 0){ + start.start(); + } + + ui->progressBar->setMaximum(bytesTotal); + ui->progressBar->setValue(bytesSent); + if (bytesSent && bytesTotal && + (start.elapsed() > 1000) && + (bytesSent > start.elapsed()/1000)) { + + ui->statusLabel->setText(tr("Transferring...ETA: %1s") + .arg(((bytesTotal-bytesSent)/(bytesSent/(start.elapsed()/1000))))); + } +} + diff --git a/examples/btfiletransfer/progress.h b/examples/btfiletransfer/progress.h new file mode 100644 index 00000000..07faa76e --- /dev/null +++ b/examples/btfiletransfer/progress.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PROGRESS_H +#define PROGRESS_H + +#include <QDialog> +#include <qbluetoothglobal.h> +#include <QTime> + +QTBLUETOOTH_BEGIN_NAMESPACE +class QBluetoothTransferReply; +QTBLUETOOTH_END_NAMESPACE + +QTBLUETOOTH_USE_NAMESPACE + +namespace Ui { + class Progress; +} + +class Progress : public QDialog +{ + Q_OBJECT + +public: + explicit Progress(QWidget *parent = 0); + ~Progress(); + + void setStatus(QString title, QString filename); + +public Q_SLOTS: + void finished(QBluetoothTransferReply *); + void uploadProgress(qint64 bytesSent, qint64 bytesTotal); + +private: + Ui::Progress *ui; + + QTime start; +}; + +#endif // PROGRESS_H diff --git a/examples/btfiletransfer/progress.ui b/examples/btfiletransfer/progress.ui new file mode 100644 index 00000000..3b151f06 --- /dev/null +++ b/examples/btfiletransfer/progress.ui @@ -0,0 +1,55 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>Progress</class> + <widget class="QDialog" name="Progress"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>408</width> + <height>115</height> + </rect> + </property> + <property name="windowTitle"> + <string>Transfer Progress</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QLabel" name="titleLabel"> + <property name="text"> + <string>Filename</string> + </property> + </widget> + </item> + <item> + <widget class="QProgressBar" name="progressBar"> + <property name="minimumSize"> + <size> + <width>390</width> + <height>0</height> + </size> + </property> + <property name="value"> + <number>24</number> + </property> + </widget> + </item> + <item> + <widget class="QLabel" name="statusLabel"> + <property name="text"> + <string>Status</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="cancelButton"> + <property name="text"> + <string>Cancel</string> + </property> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/examples/btfiletransfer/remoteselector.cpp b/examples/btfiletransfer/remoteselector.cpp new file mode 100644 index 00000000..ca49d9c3 --- /dev/null +++ b/examples/btfiletransfer/remoteselector.cpp @@ -0,0 +1,372 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "remoteselector.h" +#include "ui_remoteselector.h" + +#include <qbluetoothdeviceinfo.h> +#include <qbluetoothaddress.h> +#include <qbluetoothtransferrequest.h> +#include <qbluetoothtransferreply.h> +#include <qbluetoothlocaldevice.h> + +#include <QMovie> +#include <QMessageBox> +#include <QFileDialog> +#include <QCheckBox> + +#include "progress.h" +#include "pindisplay.h" + +QTBLUETOOTH_USE_NAMESPACE + +RemoteSelector::RemoteSelector(QWidget *parent) +: QDialog(parent), ui(new Ui::RemoteSelector), + m_discoveryAgent(new QBluetoothServiceDiscoveryAgent), m_localDevice(new QBluetoothLocalDevice), m_pindisplay(0) +{ + ui->setupUi(this); + + connect(m_discoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), + this, SLOT(serviceDiscovered(QBluetoothServiceInfo))); + connect(m_discoveryAgent, SIGNAL(finished()), this, SLOT(discoveryFinished())); + + ui->remoteDevices->setColumnWidth(3, 75); + ui->remoteDevices->setColumnWidth(4, 100); + + connect(m_localDevice, SIGNAL(pairingDisplayPinCode(const QBluetoothAddress &,QString)), this, SLOT(displayPin(const QBluetoothAddress &, QString))); + connect(m_localDevice, SIGNAL(pairingDisplayConfirmation(QBluetoothAddress,QString)), this, SLOT(displayConfirmation(QBluetoothAddress,QString))); + connect(m_localDevice, SIGNAL(pairingFinished(QBluetoothAddress,QBluetoothLocalDevice::Pairing)), this, SLOT(pairingFinished(QBluetoothAddress,QBluetoothLocalDevice::Pairing))); + + ui->busyWidget->setMovie(new QMovie(":/icons/busy.gif")); + ui->busyWidget->movie()->start(); + + ui->pairingBusy->setMovie(new QMovie(":/icons/pairing.gif")); + ui->pairingBusy->hide(); + + ui->remoteDevices->clearContents(); + ui->remoteDevices->setRowCount(0); + + +} + +RemoteSelector::~RemoteSelector() +{ + delete ui; + delete m_discoveryAgent; + delete m_localDevice; +} + +void RemoteSelector::startDiscovery(const QBluetoothUuid &uuid) +{ + ui->stopButton->setDisabled(false); + if (m_discoveryAgent->isActive()) + m_discoveryAgent->stop(); + + m_discoveryAgent->setUuidFilter(uuid); + m_discoveryAgent->start(); + + ui->status->setText(tr("Scanning...")); + ui->busyWidget->show(); + ui->busyWidget->movie()->start(); +} + +QBluetoothServiceInfo RemoteSelector::service() const +{ + return m_service; +} + +void RemoteSelector::serviceDiscovered(const QBluetoothServiceInfo &serviceInfo) +{ +#if 0 + qDebug() << "Discovered service on" + << serviceInfo.device().name() << serviceInfo.device().address().toString(); + qDebug() << "\tService name:" << serviceInfo.serviceName(); + qDebug() << "\tDescription:" + << serviceInfo.attribute(QBluetoothServiceInfo::ServiceDescription).toString(); + qDebug() << "\tProvider:" + << serviceInfo.attribute(QBluetoothServiceInfo::ServiceProvider).toString(); + qDebug() << "\tL2CAP protocol service multiplexer:" + << serviceInfo.protocolServiceMultiplexer(); + qDebug() << "\tRFCOMM server channel:" << serviceInfo.serverChannel(); +#endif + + QString remoteName; + if (serviceInfo.device().name().isEmpty()) + remoteName = serviceInfo.device().address().toString(); + else + remoteName = serviceInfo.device().name(); + +// QListWidgetItem *item = +// new QListWidgetItem(QString::fromLatin1("%1\t%2\t%3").arg(serviceInfo.device().address().toString(), +// serviceInfo.device().name(), serviceInfo.serviceName())); + + QMutableMapIterator<int, QBluetoothServiceInfo> i(m_discoveredServices); + while (i.hasNext()){ + i.next(); + if (serviceInfo.device().address() == i.value().device().address()){ + i.setValue(serviceInfo); + return; + } + } + + int row = ui->remoteDevices->rowCount(); + ui->remoteDevices->insertRow(row); + QTableWidgetItem *item = new QTableWidgetItem(serviceInfo.device().address().toString()); + ui->remoteDevices->setItem(row, 0, item); + item = new QTableWidgetItem(serviceInfo.device().name()); + ui->remoteDevices->setItem(row, 1, item); + item = new QTableWidgetItem(serviceInfo.serviceName()); + + ui->remoteDevices->setItem(row, 2, item); + + QBluetoothLocalDevice::Pairing p; + + p = m_localDevice->pairingStatus(serviceInfo.device().address()); + + ui->remoteDevices->blockSignals(true); + + item = new QTableWidgetItem(); + if ((p&QBluetoothLocalDevice::Paired) || (p&QBluetoothLocalDevice::AuthorizedPaired)) + item->setCheckState(Qt::Checked); + else + item->setCheckState(Qt::Unchecked); + ui->remoteDevices->setItem(row, 3, item); + + item = new QTableWidgetItem(); + if (p&QBluetoothLocalDevice::AuthorizedPaired) + item->setCheckState(Qt::Checked); + else + item->setCheckState(Qt::Unchecked); + + ui->remoteDevices->setItem(row, 4, item); + + ui->remoteDevices->blockSignals(false); + + + m_discoveredServices.insert(row, serviceInfo); +} + +void RemoteSelector::discoveryFinished() +{ + ui->status->setText(tr("Select the device to send to.")); + ui->stopButton->setDisabled(true); + ui->busyWidget->movie()->stop(); + ui->busyWidget->hide(); +} + +void RemoteSelector::startDiscovery() +{ + startDiscovery(QBluetoothUuid(QBluetoothUuid::ObexObjectPush)); +} + +void RemoteSelector::on_refreshPB_clicked() +{ + startDiscovery(); + ui->stopButton->setDisabled(false); +} + +void RemoteSelector::on_fileSelectPB_clicked() +{ + ui->fileName->setText(QFileDialog::getOpenFileName()); + if (m_service.isValid()) + ui->sendButton->setDisabled(false); +} + +void RemoteSelector::on_sendButton_clicked() +{ + QBluetoothTransferManager mgr; + QBluetoothTransferRequest req(m_service.device().address()); + + m_file = new QFile(ui->fileName->text()); + + Progress *p = new Progress; + p->setStatus("Sending to: " + m_service.device().name(), "Waiting for start"); + p->show(); + + QBluetoothTransferReply *reply = mgr.put(req, m_file); + if (reply->error()){ + qDebug() << "Failed to send file"; + delete reply; + p->finished(reply); + return; + } + + connect(reply, SIGNAL(uploadProgress(qint64,qint64)), p, SLOT(uploadProgress(qint64,qint64))); + connect(reply, SIGNAL(finished(QBluetoothTransferReply*)), p, SLOT(finished(QBluetoothTransferReply*))); + connect(p, SIGNAL(rejected()), reply, SLOT(abort())); +} + +void RemoteSelector::on_stopButton_clicked() +{ + m_discoveryAgent->stop(); +} + +QString RemoteSelector::addressToName(const QBluetoothAddress &address) +{ + QMapIterator<int, QBluetoothServiceInfo> i(m_discoveredServices); + while (i.hasNext()){ + i.next(); + if (i.value().device().address() == address) + return i.value().device().name(); + } + return address.toString(); +} + +void RemoteSelector::displayPin(const QBluetoothAddress &address, QString pin) +{ + if (m_pindisplay) + m_pindisplay->deleteLater(); + m_pindisplay = new pinDisplay(QString("Enter pairing pin on: %1").arg(addressToName(address)), pin, this); + m_pindisplay->show(); +} + +void RemoteSelector::displayConfirmation(const QBluetoothAddress &address, QString pin) +{ + Q_UNUSED(address); + + if (m_pindisplay) + m_pindisplay->deleteLater(); + m_pindisplay = new pinDisplay(QString("Confirm this pin is the same"), pin, this); + connect(m_pindisplay, SIGNAL(accepted()), this, SLOT(displayConfAccepted())); + connect(m_pindisplay, SIGNAL(rejected()), this, SLOT(displayConfReject())); + m_pindisplay->setOkCancel(); + m_pindisplay->show(); +} + +void RemoteSelector::displayConfAccepted() +{ + m_localDevice->pairingConfirmation(true); +} +void RemoteSelector::displayConfReject() +{ + m_localDevice->pairingConfirmation(false); +} + +void RemoteSelector::pairingFinished(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing status) +{ + QBluetoothServiceInfo service; + int row = 0; + + ui->pairingBusy->hide(); + ui->pairingBusy->movie()->stop(); + + ui->remoteDevices->blockSignals(true); + + for (int i = 0; i < m_discoveredServices.count(); i++){ + if (m_discoveredServices.value(i).device().address() == address){ + service = m_discoveredServices.value(i); + row = i; + break; + } + } + + if (m_pindisplay) + delete m_pindisplay; + + QMessageBox msgBox; + if (status == QBluetoothLocalDevice::Paired || + status == QBluetoothLocalDevice::AuthorizedPaired){ + msgBox.setText("Paired successfully with" + address.toString()); + } + else { + msgBox.setText("Pairing failed with " + address.toString()); + } + + if (service.isValid()){ + if (status == QBluetoothLocalDevice::AuthorizedPaired){ + ui->remoteDevices->item(row, 3)->setCheckState(Qt::Checked); + ui->remoteDevices->item(row, 4)->setCheckState(Qt::Checked); + } + else if (status == QBluetoothLocalDevice::Paired){ + ui->remoteDevices->item(row, 3)->setCheckState(Qt::Checked); + ui->remoteDevices->item(row, 4)->setCheckState(Qt::Unchecked); + } + else { + ui->remoteDevices->item(row, 3)->setCheckState(Qt::Unchecked); + ui->remoteDevices->item(row, 4)->setCheckState(Qt::Unchecked); + } + } + + msgBox.exec(); + + ui->remoteDevices->blockSignals(false); + + +} + +void RemoteSelector::on_remoteDevices_cellClicked(int row, int column) +{ + Q_UNUSED(column); + + m_service = m_discoveredServices.value(row); + if (!ui->fileName->text().isEmpty()) { + ui->sendButton->setDisabled(false); + } +} + +void RemoteSelector::on_remoteDevices_itemChanged(QTableWidgetItem* item) +{ + int row = item->row(); + int column = item->column(); + m_service = m_discoveredServices.value(row); + + if (column < 3) + return; + + if (item->checkState() == Qt::Unchecked && column == 3){ + m_localDevice->requestPairing(m_service.device().address(), QBluetoothLocalDevice::Unpaired); + return; // don't continue and start movie + } + else if ((item->checkState() == Qt::Checked && column == 3) || + (item->checkState() == Qt::Unchecked && column == 4)){ + m_localDevice->requestPairing(m_service.device().address(), QBluetoothLocalDevice::Paired); + ui->remoteDevices->blockSignals(true); + ui->remoteDevices->item(row, column)->setCheckState(Qt::PartiallyChecked); + ui->remoteDevices->blockSignals(false); + } + else if (item->checkState() == Qt::Checked && column == 4){ + m_localDevice->requestPairing(m_service.device().address(), QBluetoothLocalDevice::AuthorizedPaired); + ui->remoteDevices->blockSignals(true); + ui->remoteDevices->item(row, column)->setCheckState(Qt::PartiallyChecked); + ui->remoteDevices->blockSignals(false); + } + ui->pairingBusy->show(); + ui->pairingBusy->movie()->start(); +} diff --git a/examples/btfiletransfer/remoteselector.h b/examples/btfiletransfer/remoteselector.h new file mode 100644 index 00000000..bb52c6f9 --- /dev/null +++ b/examples/btfiletransfer/remoteselector.h @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef REMOTESELECTOR_H +#define REMOTESELECTOR_H + +#include <QDialog> +#include <QPointer> + +#include <qbluetoothuuid.h> +#include <qbluetoothserviceinfo.h> +#include <qbluetoothservicediscoveryagent.h> +#include <qbluetoothlocaldevice.h> + +QT_FORWARD_DECLARE_CLASS(QModelIndex) +QT_FORWARD_DECLARE_CLASS(QTableWidgetItem) +QT_FORWARD_DECLARE_CLASS(QFile) + +class pinDisplay; + +QTBLUETOOTH_USE_NAMESPACE + +namespace Ui { + class RemoteSelector; +} + +class RemoteSelector : public QDialog +{ + Q_OBJECT + +public: + explicit RemoteSelector(QWidget *parent = 0); + ~RemoteSelector(); + + void startDiscovery(const QBluetoothUuid &uuid); + QBluetoothServiceInfo service() const; + +private: + Ui::RemoteSelector *ui; + + QBluetoothServiceDiscoveryAgent *m_discoveryAgent; + QBluetoothServiceInfo m_service; + QMap<int, QBluetoothServiceInfo> m_discoveredServices; + QFile *m_file; + QBluetoothLocalDevice *m_localDevice; + QPointer<pinDisplay> m_pindisplay; + + QString addressToName(const QBluetoothAddress &address); + +public Q_SLOTS: + void startDiscovery(); + +private slots: + void serviceDiscovered(const QBluetoothServiceInfo &serviceInfo); + void discoveryFinished(); + void on_refreshPB_clicked(); + void on_fileSelectPB_clicked(); + void on_sendButton_clicked(); + void on_stopButton_clicked(); + + void pairingFinished(const QBluetoothAddress &address,QBluetoothLocalDevice::Pairing pairing); + void displayPin(const QBluetoothAddress &address, QString pin); + void displayConfirmation(const QBluetoothAddress &address, QString pin); + void displayConfReject(); + void displayConfAccepted(); + + void on_remoteDevices_cellClicked(int row, int column); + void on_remoteDevices_itemChanged(QTableWidgetItem* item); +}; + +#endif // REMOTESELECTOR_H diff --git a/examples/btfiletransfer/remoteselector.ui b/examples/btfiletransfer/remoteselector.ui new file mode 100644 index 00000000..f99deaa7 --- /dev/null +++ b/examples/btfiletransfer/remoteselector.ui @@ -0,0 +1,214 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>RemoteSelector</class> + <widget class="QDialog" name="RemoteSelector"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>681</width> + <height>394</height> + </rect> + </property> + <property name="windowTitle"> + <string>Available file transfer services</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <layout class="QGridLayout" name="gridLayout"> + <property name="horizontalSpacing"> + <number>6</number> + </property> + <item row="7" column="0" colspan="2"> + <widget class="QLineEdit" name="fileName"/> + </item> + <item row="7" column="2"> + <widget class="QPushButton" name="fileSelectPB"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Fixed" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>Select File</string> + </property> + </widget> + </item> + <item row="8" column="0" colspan="3"> + <widget class="QPushButton" name="sendButton"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="text"> + <string>Send</string> + </property> + </widget> + </item> + <item row="0" column="0" colspan="3"> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <widget class="QLabel" name="busyWidget"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Fixed" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="maximumSize"> + <size> + <width>16</width> + <height>16</height> + </size> + </property> + <property name="baseSize"> + <size> + <width>16</width> + <height>16</height> + </size> + </property> + <property name="text"> + <string>TextLabel</string> + </property> + </widget> + </item> + <item> + <widget class="QLabel" name="status"> + <property name="text"> + <string>Scanning...</string> + </property> + </widget> + </item> + <item> + <widget class="QLabel" name="pairingBusy"> + <property name="text"> + <string>TextLabel</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="stopButton"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="sizePolicy"> + <sizepolicy hsizetype="Fixed" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>Stop</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="refreshPB"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Fixed" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>Refresh</string> + </property> + </widget> + </item> + </layout> + </item> + <item row="6" column="0" colspan="3"> + <widget class="QTableWidget" name="remoteDevices"> + <property name="horizontalScrollBarPolicy"> + <enum>Qt::ScrollBarAsNeeded</enum> + </property> + <property name="editTriggers"> + <set>QAbstractItemView::NoEditTriggers</set> + </property> + <property name="showDropIndicator" stdset="0"> + <bool>false</bool> + </property> + <property name="alternatingRowColors"> + <bool>true</bool> + </property> + <property name="selectionMode"> + <enum>QAbstractItemView::SingleSelection</enum> + </property> + <property name="selectionBehavior"> + <enum>QAbstractItemView::SelectRows</enum> + </property> + <property name="showGrid"> + <bool>false</bool> + </property> + <property name="wordWrap"> + <bool>false</bool> + </property> + <property name="rowCount"> + <number>1</number> + </property> + <attribute name="horizontalHeaderCascadingSectionResizes"> + <bool>true</bool> + </attribute> + <attribute name="horizontalHeaderDefaultSectionSize"> + <number>150</number> + </attribute> + <attribute name="horizontalHeaderStretchLastSection"> + <bool>true</bool> + </attribute> + <attribute name="verticalHeaderVisible"> + <bool>false</bool> + </attribute> + <attribute name="verticalHeaderCascadingSectionResizes"> + <bool>false</bool> + </attribute> + <row> + <property name="text"> + <string>Test</string> + </property> + </row> + <column> + <property name="text"> + <string>MAC Address</string> + </property> + </column> + <column> + <property name="text"> + <string>Device Name</string> + </property> + </column> + <column> + <property name="text"> + <string>Device Service</string> + </property> + </column> + <column> + <property name="text"> + <string>Paired</string> + </property> + <property name="toolTip"> + <string comment="Paired" extracomment="Paired"/> + </property> + <property name="whatsThis"> + <string comment="Paired" extracomment="Paired"/> + </property> + <property name="textAlignment"> + <set>AlignHCenter|AlignVCenter|AlignCenter</set> + </property> + </column> + <column> + <property name="text"> + <string>Authorized</string> + </property> + <property name="textAlignment"> + <set>AlignHCenter|AlignVCenter|AlignCenter</set> + </property> + </column> + </widget> + </item> + </layout> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/examples/btscanner/btscanner.pro b/examples/btscanner/btscanner.pro new file mode 100644 index 00000000..629d7d8f --- /dev/null +++ b/examples/btscanner/btscanner.pro @@ -0,0 +1,21 @@ +TARGET = btscanner + +INCLUDEPATH += ../../src/connectivity/bluetooth +DEPENDPATH += ../../src/connectivity/bluetooth + +QT += bluetooth widgets +TEMPLATE = app + +SOURCES = \ + main.cpp \ + device.cpp \ + service.cpp + +HEADERS = \ + device.h \ + service.h + +FORMS = \ + device.ui \ + service.ui + diff --git a/examples/btscanner/device.cpp b/examples/btscanner/device.cpp new file mode 100644 index 00000000..ea9579d2 --- /dev/null +++ b/examples/btscanner/device.cpp @@ -0,0 +1,211 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "device.h" +#include "service.h" + +#include <qbluetoothaddress.h> +#include <qbluetoothdevicediscoveryagent.h> +#include <qbluetoothlocaldevice.h> +#include <QMenu> +#include <QDebug> + +DeviceDiscoveryDialog::DeviceDiscoveryDialog(QWidget *parent) +: QDialog(parent), discoveryAgent(new QBluetoothDeviceDiscoveryAgent), + localDevice(new QBluetoothLocalDevice), + ui(new Ui_DeviceDiscovery) +{ + ui->setupUi(this); + +#if defined (Q_OS_SYMBIAN) || defined(Q_OS_WINCE) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6) + setWindowState(Qt::WindowFullScreen); +#endif + + connect(ui->inquiryType, SIGNAL(toggled(bool)), this, SLOT(setGeneralUnlimited(bool))); + connect(ui->scan, SIGNAL(clicked()), this, SLOT(startScan())); + + connect(discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)), + this, SLOT(addDevice(const QBluetoothDeviceInfo&))); + connect(discoveryAgent, SIGNAL(finished()), this, SLOT(scanFinished())); + + connect(ui->list, SIGNAL(itemActivated(QListWidgetItem*)), + this, SLOT(itemActivated(QListWidgetItem*))); + + connect(localDevice, SIGNAL(hostModeStateChanged(QBluetoothLocalDevice::HostMode)), + this, SLOT(hostModeStateChanged(QBluetoothLocalDevice::HostMode))); + + hostModeStateChanged(localDevice->hostMode()); + // add context menu for devices to be able to pair device + ui->list->setContextMenuPolicy(Qt::CustomContextMenu); + connect(ui->list, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(displayPairingMenu(QPoint))); + connect(localDevice, SIGNAL(pairingFinished(const QBluetoothAddress&, QBluetoothLocalDevice::Pairing)) + , this, SLOT(pairingDone(const QBluetoothAddress&, QBluetoothLocalDevice::Pairing))); + +} + +DeviceDiscoveryDialog::~DeviceDiscoveryDialog() +{ + delete discoveryAgent; +} + +void DeviceDiscoveryDialog::addDevice(const QBluetoothDeviceInfo &info) +{ + QString label = QString("%1 %2").arg(info.address().toString()).arg(info.name()); + QList<QListWidgetItem *> items = ui->list->findItems(label, Qt::MatchExactly); + if (items.empty()) { + QListWidgetItem *item = new QListWidgetItem(label); + QBluetoothLocalDevice::Pairing pairingStatus = localDevice->pairingStatus(info.address()); + if (pairingStatus == QBluetoothLocalDevice::Paired || pairingStatus == QBluetoothLocalDevice::AuthorizedPaired ) + item->setTextColor(QColor(Qt::green)); + else + item->setTextColor(QColor(Qt::black)); + + ui->list->addItem(item); + } + +} + +void DeviceDiscoveryDialog::startScan() +{ + discoveryAgent->start(); + ui->scan->setEnabled(false); + ui->inquiryType->setEnabled(false); +} + +void DeviceDiscoveryDialog::scanFinished() +{ + ui->scan->setEnabled(true); + ui->inquiryType->setEnabled(true); +} + +void DeviceDiscoveryDialog::setGeneralUnlimited(bool unlimited) +{ + if (unlimited) + discoveryAgent->setInquiryType(QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry); + else + discoveryAgent->setInquiryType(QBluetoothDeviceDiscoveryAgent::LimitedInquiry); +} + +void DeviceDiscoveryDialog::itemActivated(QListWidgetItem *item) +{ + QString text = item->text(); + + int index = text.indexOf(' '); + + if (index == -1) + return; + + QBluetoothAddress address(text.left(index)); + QString name(text.mid(index + 1)); + + ServiceDiscoveryDialog d(name, address); + d.exec(); +} + +void DeviceDiscoveryDialog::on_discoverable_clicked(bool clicked) +{ + if (clicked) + localDevice->setHostMode(QBluetoothLocalDevice::HostDiscoverable); + else + localDevice->setHostMode(QBluetoothLocalDevice::HostConnectable); +} + +void DeviceDiscoveryDialog::on_power_clicked(bool clicked) +{ + if (clicked) + localDevice->powerOn(); + else + localDevice->setHostMode(QBluetoothLocalDevice::HostPoweredOff); +} + +void DeviceDiscoveryDialog::hostModeStateChanged(QBluetoothLocalDevice::HostMode mode) +{ + if (mode != QBluetoothLocalDevice::HostPoweredOff) + ui->power->setChecked(true); + else + ui->power->setChecked( false); + + if (mode == QBluetoothLocalDevice::HostDiscoverable) + ui->discoverable->setChecked(true); + else + ui->discoverable->setChecked(false); + + bool on = !(mode == QBluetoothLocalDevice::HostPoweredOff); + + + ui->scan->setEnabled(on); + ui->discoverable->setEnabled(on); +} +void DeviceDiscoveryDialog::displayPairingMenu(const QPoint &pos) +{ + QMenu menu(this); + QAction *pairAction = menu.addAction("Pair"); + QAction *removePairAction = menu.addAction("Remove Pairing"); + QAction *chosenAction = menu.exec(ui->list->viewport()->mapToGlobal(pos)); + QListWidgetItem *currentItem = ui->list->currentItem(); + + QString text = currentItem->text(); + int index = text.indexOf(' '); + if (index == -1) + return; + + QBluetoothAddress address (text.left(index)); + if (chosenAction == pairAction) { + localDevice->requestPairing(address, QBluetoothLocalDevice::Paired); + } else if (chosenAction == removePairAction) { + localDevice->requestPairing(address, QBluetoothLocalDevice::Unpaired); + } +} +void DeviceDiscoveryDialog::pairingDone(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing) +{ + QList<QListWidgetItem *> items = ui->list->findItems(address.toString(), Qt::MatchContains); + + if (pairing == QBluetoothLocalDevice::Paired || pairing == QBluetoothLocalDevice::AuthorizedPaired ) { + for (int var = 0; var < items.count(); ++var) { + QListWidgetItem *item = items.at(var); + item->setTextColor(QColor(Qt::green)); + } + } else { + for (int var = 0; var < items.count(); ++var) { + QListWidgetItem *item = items.at(var); + item->setTextColor(QColor(Qt::red)); + } + } +} diff --git a/examples/btscanner/device.h b/examples/btscanner/device.h new file mode 100644 index 00000000..ddeb5638 --- /dev/null +++ b/examples/btscanner/device.h @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DEVICE_H +#define DEVICE_H + +#include "ui_device.h" + +#include <qbluetoothglobal.h> +#include <qbluetoothlocaldevice.h> + +#include <QDialog> + +QTBLUETOOTH_BEGIN_NAMESPACE +class QBluetoothDeviceDiscoveryAgent; +class QBluetoothDeviceInfo; +QTBLUETOOTH_END_NAMESPACE + +QTBLUETOOTH_USE_NAMESPACE + +class DeviceDiscoveryDialog : public QDialog +{ + Q_OBJECT + +public: + DeviceDiscoveryDialog(QWidget *parent = 0); + ~DeviceDiscoveryDialog(); + +public slots: + void addDevice(const QBluetoothDeviceInfo&); + void on_power_clicked(bool clicked); + void on_discoverable_clicked(bool clicked); + void displayPairingMenu(const QPoint &pos); + void pairingDone(const QBluetoothAddress&, QBluetoothLocalDevice::Pairing); +private slots: + void startScan(); + void scanFinished(); + void setGeneralUnlimited(bool unlimited); + void itemActivated(QListWidgetItem *item); + void hostModeStateChanged(QBluetoothLocalDevice::HostMode); + +private: + QBluetoothDeviceDiscoveryAgent *discoveryAgent; + QBluetoothLocalDevice *localDevice; + Ui_DeviceDiscovery *ui; +}; + +#endif diff --git a/examples/btscanner/device.ui b/examples/btscanner/device.ui new file mode 100644 index 00000000..1f62dd96 --- /dev/null +++ b/examples/btscanner/device.ui @@ -0,0 +1,121 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>DeviceDiscovery</class> + <widget class="QDialog" name="DeviceDiscovery"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>400</width> + <height>411</height> + </rect> + </property> + <property name="windowTitle"> + <string>Bluetooth Scanner</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QListWidget" name="list"/> + </item> + <item> + <widget class="QGroupBox" name="groupBox"> + <property name="title"> + <string>Local Device</string> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_2"> + <item> + <widget class="QCheckBox" name="power"> + <property name="text"> + <string>Bluetooth Powered On</string> + </property> + <property name="checked"> + <bool>true</bool> + </property> + </widget> + </item> + <item> + <widget class="QCheckBox" name="discoverable"> + <property name="text"> + <string>Discoverable</string> + </property> + <property name="checked"> + <bool>true</bool> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QCheckBox" name="inquiryType"> + <property name="text"> + <string>General Unlimited Inquiry</string> + </property> + <property name="checked"> + <bool>true</bool> + </property> + </widget> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <widget class="QPushButton" name="scan"> + <property name="text"> + <string>Scan</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="clear"> + <property name="text"> + <string>Clear</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="quit"> + <property name="text"> + <string>Quit</string> + </property> + </widget> + </item> + </layout> + </item> + </layout> + </widget> + <resources/> + <connections> + <connection> + <sender>quit</sender> + <signal>clicked()</signal> + <receiver>DeviceDiscovery</receiver> + <slot>accept()</slot> + <hints> + <hint type="sourcelabel"> + <x>323</x> + <y>275</y> + </hint> + <hint type="destinationlabel"> + <x>396</x> + <y>268</y> + </hint> + </hints> + </connection> + <connection> + <sender>clear</sender> + <signal>clicked()</signal> + <receiver>list</receiver> + <slot>clear()</slot> + <hints> + <hint type="sourcelabel"> + <x>188</x> + <y>276</y> + </hint> + <hint type="destinationlabel"> + <x>209</x> + <y>172</y> + </hint> + </hints> + </connection> + </connections> +</ui> diff --git a/examples/btscanner/main.cpp b/examples/btscanner/main.cpp new file mode 100644 index 00000000..fc1cf3d4 --- /dev/null +++ b/examples/btscanner/main.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "device.h" + +#include <QApplication> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + DeviceDiscoveryDialog d; + QObject::connect(&d, SIGNAL(accepted()), &app, SLOT(quit())); + d.show(); + + app.exec(); + + return 0; +} + diff --git a/examples/btscanner/service.cpp b/examples/btscanner/service.cpp new file mode 100644 index 00000000..c8d35b50 --- /dev/null +++ b/examples/btscanner/service.cpp @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "service.h" + +#include <qbluetoothaddress.h> +#include <qbluetoothservicediscoveryagent.h> +#include <qbluetoothserviceinfo.h> + + +ServiceDiscoveryDialog::ServiceDiscoveryDialog(const QString &name, + const QBluetoothAddress &address, QWidget *parent) +: QDialog(parent), discoveryAgent(new QBluetoothServiceDiscoveryAgent(address)), + ui(new Ui_ServiceDiscovery) +{ + ui->setupUi(this); + +#if defined (Q_OS_SYMBIAN) || defined(Q_OS_WINCE) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6) + setWindowState(Qt::WindowMaximized); +#endif + + setWindowTitle(name); + + connect(discoveryAgent, SIGNAL(serviceDiscovered(const QBluetoothServiceInfo&)), + this, SLOT(addService(const QBluetoothServiceInfo&))); + connect(discoveryAgent, SIGNAL(finished()), ui->status, SLOT(hide())); + + discoveryAgent->start(); +} + +ServiceDiscoveryDialog::~ServiceDiscoveryDialog() +{ + delete discoveryAgent; + delete ui; +} + +void ServiceDiscoveryDialog::addService(const QBluetoothServiceInfo &info) +{ + if (info.serviceName().isEmpty()) + return; + + QString line = info.serviceName(); + if (!info.serviceDescription().isEmpty()) + line.append("\n\t" + info.serviceDescription()); + if (!info.serviceProvider().isEmpty()) + line.append("\n\t" + info.serviceProvider()); + + ui->list->addItem(line); +} + diff --git a/examples/btscanner/service.h b/examples/btscanner/service.h new file mode 100644 index 00000000..dd231f1d --- /dev/null +++ b/examples/btscanner/service.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtBluetooth module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SERVICE_H +#define SERVICE_H + +#include "ui_service.h" + +#include <qbluetoothglobal.h> + +#include <QDialog> + +QTBLUETOOTH_BEGIN_NAMESPACE +class QBluetoothServiceDiscoveryAgent; +class QBluetoothServiceInfo; +class QBluetoothAddress; +QTBLUETOOTH_END_NAMESPACE + +QTBLUETOOTH_USE_NAMESPACE + +class ServiceDiscoveryDialog : public QDialog +{ + Q_OBJECT + +public: + ServiceDiscoveryDialog(const QString &name, const QBluetoothAddress &address, QWidget *parent = 0); + ~ServiceDiscoveryDialog(); + +public slots: + void addService(const QBluetoothServiceInfo&); + +private: + QBluetoothServiceDiscoveryAgent *discoveryAgent; + Ui_ServiceDiscovery *ui; +}; + +#endif diff --git a/examples/btscanner/service.ui b/examples/btscanner/service.ui new file mode 100644 index 00000000..68ebc5dc --- /dev/null +++ b/examples/btscanner/service.ui @@ -0,0 +1,76 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>ServiceDiscovery</class> + <widget class="QDialog" name="ServiceDiscovery"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>400</width> + <height>300</height> + </rect> + </property> + <property name="windowTitle"> + <string>Available Services</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QListWidget" name="list"/> + </item> + <item> + <widget class="QLabel" name="status"> + <property name="text"> + <string>Querying...</string> + </property> + </widget> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QPushButton" name="close"> + <property name="text"> + <string>Close</string> + </property> + </widget> + </item> + </layout> + </item> + </layout> + </widget> + <tabstops> + <tabstop>list</tabstop> + <tabstop>close</tabstop> + </tabstops> + <resources/> + <connections> + <connection> + <sender>close</sender> + <signal>clicked()</signal> + <receiver>ServiceDiscovery</receiver> + <slot>accept()</slot> + <hints> + <hint type="sourcelabel"> + <x>350</x> + <y>277</y> + </hint> + <hint type="destinationlabel"> + <x>237</x> + <y>269</y> + </hint> + </hints> + </connection> + </connections> +</ui> diff --git a/examples/ndefeditor/main.cpp b/examples/ndefeditor/main.cpp new file mode 100644 index 00000000..78089ff2 --- /dev/null +++ b/examples/ndefeditor/main.cpp @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNfc module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + +#if defined(Q_WS_S60) || defined(Q_WS_MAEMO_6) || defined(Q_WS_MEEGO) + w.showFullScreen(); +#else + w.show(); +#endif + + return a.exec(); +} diff --git a/examples/ndefeditor/mainwindow.cpp b/examples/ndefeditor/mainwindow.cpp new file mode 100644 index 00000000..1b227210 --- /dev/null +++ b/examples/ndefeditor/mainwindow.cpp @@ -0,0 +1,361 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNfc module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "mainwindow.h" +#include "ui_mainwindow.h" + +#include "textrecordeditor.h" +#include "urirecordeditor.h" +#include "mimeimagerecordeditor.h" + +#include <QtCore/QTime> +#include <QMenu> +#include <QVBoxLayout> +#include <QFrame> +#include <QLabel> +#include <QFileDialog> + +#include <qnearfieldmanager.h> +#include <qnearfieldtarget.h> +#include <qndefrecord.h> +#include <qndefnfctextrecord.h> +#include <qndefnfcurirecord.h> +#include <qndefmessage.h> + +#include <QtCore/QDebug> + +class EmptyRecordLabel : public QLabel +{ + Q_OBJECT + +public: + EmptyRecordLabel() : QLabel(tr("Empty Record")) { } + ~EmptyRecordLabel() { } + + void setRecord(const QNdefRecord &record) + { + Q_UNUSED(record); + } + + QNdefRecord record() const + { + return QNdefRecord(); + } +}; + +class UnknownRecordLabel : public QLabel +{ + Q_OBJECT + +public: + UnknownRecordLabel() : QLabel(tr("Unknown Record Type")) { } + ~UnknownRecordLabel() { } + + void setRecord(const QNdefRecord &record) { m_record = record; } + QNdefRecord record() const { return m_record; } + +private: + QNdefRecord m_record; +}; + +template <typename T> +void addRecord(Ui::MainWindow *ui, const QNdefRecord &record = QNdefRecord()) +{ + QVBoxLayout *vbox = qobject_cast<QVBoxLayout *>(ui->scrollAreaWidgetContents->layout()); + if (!vbox) + return; + + if (!vbox->isEmpty()) { + QFrame *hline = new QFrame; + hline->setFrameShape(QFrame::HLine); + hline->setObjectName(QLatin1String("line-spacer")); + + vbox->addWidget(hline); + } + + T *recordEditor = new T; + recordEditor->setObjectName(QLatin1String("record-editor")); + + if (!record.isEmpty()) + recordEditor->setRecord(record); + + vbox->addWidget(recordEditor); +} + +MainWindow::MainWindow(QWidget *parent) +: QMainWindow(parent), ui(new Ui::MainWindow), m_manager(new QNearFieldManager(this)), + m_touchAction(NoAction) +{ + ui->setupUi(this); + + QMenu *addRecordMenu = new QMenu(this); + addRecordMenu->addAction(tr("NFC Text Record"), this, SLOT(addNfcTextRecord())); + addRecordMenu->addAction(tr("NFC URI Record"), this, SLOT(addNfcUriRecord())); + addRecordMenu->addAction(tr("MIME Image Record"), this, SLOT(addMimeImageRecord())); + addRecordMenu->addAction(tr("Empty Record"), this, SLOT(addEmptyRecord())); + ui->addRecord->setMenu(addRecordMenu); + + QVBoxLayout *vbox = new QVBoxLayout; + ui->scrollAreaWidgetContents->setLayout(vbox); + + connect(m_manager, SIGNAL(targetDetected(QNearFieldTarget*)), + this, SLOT(targetDetected(QNearFieldTarget*))); + connect(m_manager, SIGNAL(targetLost(QNearFieldTarget*)), + this, SLOT(targetLost(QNearFieldTarget*))); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::addNfcTextRecord() +{ + addRecord<TextRecordEditor>(ui); +} + +void MainWindow::addNfcUriRecord() +{ + addRecord<UriRecordEditor>(ui); +} + +void MainWindow::addMimeImageRecord() +{ + addRecord<MimeImageRecordEditor>(ui); +} + +void MainWindow::addEmptyRecord() +{ + addRecord<EmptyRecordLabel>(ui); +} + +void MainWindow::loadMessage() +{ + QString filename = QFileDialog::getOpenFileName(this, tr("Select NDEF Message")); + if (filename.isEmpty()) + return; + + QFile file(filename); + if (!file.open(QIODevice::ReadOnly)) + return; + + QByteArray ndef = file.readAll(); + + ndefMessageRead(QNdefMessage::fromByteArray(ndef)); + + file.close(); +} + +void MainWindow::saveMessage() +{ + QString filename = QFileDialog::getSaveFileName(this, tr("Select NDEF Message")); + if (filename.isEmpty()) + return; + + QFile file(filename); + if (!file.open(QIODevice::WriteOnly)) + return; + + file.write(ndefMessage().toByteArray()); + + file.close(); +} + +void MainWindow::touchReceive() +{ + ui->status->setStyleSheet(QLatin1String("background: blue")); + + m_touchAction = ReadNdef; + + m_manager->setTargetAccessModes(QNearFieldManager::NdefReadTargetAccess); + m_manager->startTargetDetection(); +} + +void MainWindow::touchStore() +{ + ui->status->setStyleSheet(QLatin1String("background: yellow")); + + m_touchAction = WriteNdef; + + m_manager->setTargetAccessModes(QNearFieldManager::NdefWriteTargetAccess); + m_manager->startTargetDetection(); +} + +void MainWindow::targetDetected(QNearFieldTarget *target) +{ + switch (m_touchAction) { + case NoAction: + break; + case ReadNdef: + connect(target, SIGNAL(ndefMessageRead(QNdefMessage)), + this, SLOT(ndefMessageRead(QNdefMessage))); + connect(target, SIGNAL(error(QNearFieldTarget::Error,QNearFieldTarget::RequestId)), + this, SLOT(targetError(QNearFieldTarget::Error,QNearFieldTarget::RequestId))); + + m_request = target->readNdefMessages(); + break; + case WriteNdef: + connect(target, SIGNAL(ndefMessagesWritten()), this, SLOT(ndefMessageWritten())); + connect(target, SIGNAL(error(QNearFieldTarget::Error,QNearFieldTarget::RequestId)), + this, SLOT(targetError(QNearFieldTarget::Error,QNearFieldTarget::RequestId))); + + m_request = target->writeNdefMessages(QList<QNdefMessage>() << ndefMessage()); + break; + } +} + +void MainWindow::targetLost(QNearFieldTarget *target) +{ + target->deleteLater(); +} + +void MainWindow::ndefMessageRead(const QNdefMessage &message) +{ + clearMessage(); + + foreach (const QNdefRecord &record, message) { + if (record.isRecordType<QNdefNfcTextRecord>()) { + addRecord<TextRecordEditor>(ui, record); + } else if (record.isRecordType<QNdefNfcUriRecord>()) { + addRecord<UriRecordEditor>(ui, record); + } else if (record.typeNameFormat() == QNdefRecord::Mime && + record.type().startsWith("image/")) { + addRecord<MimeImageRecordEditor>(ui, record); + } else if (record.isEmpty()) { + addRecord<EmptyRecordLabel>(ui); + } else { + addRecord<UnknownRecordLabel>(ui, record); + } + } + + ui->status->setStyleSheet(QString()); + m_manager->setTargetAccessModes(QNearFieldManager::NoTargetAccess); + m_manager->stopTargetDetection(); + m_request = QNearFieldTarget::RequestId(); + ui->statusBar->clearMessage(); +} + +void MainWindow::ndefMessageWritten() +{ + ui->status->setStyleSheet(QString()); + m_manager->setTargetAccessModes(QNearFieldManager::NoTargetAccess); + m_manager->stopTargetDetection(); + m_request = QNearFieldTarget::RequestId(); + ui->statusBar->clearMessage(); +} + +void MainWindow::targetError(QNearFieldTarget::Error error, const QNearFieldTarget::RequestId &id) +{ + Q_UNUSED(error); + Q_UNUSED(id); + + if (m_request == id) { + switch (error) { + case QNearFieldTarget::NoError: + ui->statusBar->clearMessage(); + break; + case QNearFieldTarget::UnsupportedError: + ui->statusBar->showMessage(tr("Unsupported tag")); + break; + case QNearFieldTarget::TargetOutOfRangeError: + ui->statusBar->showMessage(tr("Tag removed from field")); + break; + case QNearFieldTarget::NoResponseError: + ui->statusBar->showMessage(tr("No response from tag")); + break; + case QNearFieldTarget::ChecksumMismatchError: + ui->statusBar->showMessage(tr("Checksum mismatch")); + break; + case QNearFieldTarget::InvalidParametersError: + ui->statusBar->showMessage(tr("Invalid parameters")); + break; + case QNearFieldTarget::NdefReadError: + ui->statusBar->showMessage(tr("NDEF read error")); + break; + case QNearFieldTarget::NdefWriteError: + ui->statusBar->showMessage(tr("NDEF write error")); + break; + default: + ui->statusBar->showMessage(tr("Unknown error")); + } + + ui->status->setStyleSheet(QString()); + m_manager->setTargetAccessModes(QNearFieldManager::NoTargetAccess); + m_manager->stopTargetDetection(); + m_request = QNearFieldTarget::RequestId(); + } +} + +void MainWindow::clearMessage() +{ + QWidget *scrollArea = ui->scrollAreaWidgetContents; + + qDeleteAll(scrollArea->findChildren<QWidget *>(QLatin1String("line-spacer"))); + qDeleteAll(scrollArea->findChildren<QWidget *>(QLatin1String("record-editor"))); +} + +QNdefMessage MainWindow::ndefMessage() const +{ + QVBoxLayout *vbox = qobject_cast<QVBoxLayout *>(ui->scrollAreaWidgetContents->layout()); + if (!vbox) + return QNdefMessage(); + + QNdefMessage message; + + for (int i = 0; i < vbox->count(); ++i) { + QWidget *widget = vbox->itemAt(i)->widget(); + + if (TextRecordEditor *editor = qobject_cast<TextRecordEditor *>(widget)) { + message.append(editor->record()); + } else if (UriRecordEditor *editor = qobject_cast<UriRecordEditor *>(widget)) { + message.append(editor->record()); + } else if (MimeImageRecordEditor *editor = qobject_cast<MimeImageRecordEditor *>(widget)) { + message.append(editor->record()); + } else if (qobject_cast<EmptyRecordLabel *>(widget)) { + message.append(QNdefRecord()); + } else if (UnknownRecordLabel *label = qobject_cast<UnknownRecordLabel *>(widget)) { + message.append(label->record()); + } + } + + return message; +} + +#include "mainwindow.moc" diff --git a/examples/ndefeditor/mainwindow.h b/examples/ndefeditor/mainwindow.h new file mode 100644 index 00000000..bd9330e2 --- /dev/null +++ b/examples/ndefeditor/mainwindow.h @@ -0,0 +1,107 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNfc module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +#include <qnfcglobal.h> + +#include <qnearfieldtarget.h> + +QTNFC_BEGIN_NAMESPACE +class QNearFieldManager; +class QNdefMessage; +QTNFC_END_NAMESPACE + +QTNFC_USE_NAMESPACE + +namespace Ui { + class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +private slots: + void addNfcTextRecord(); + void addNfcUriRecord(); + void addMimeImageRecord(); + void addEmptyRecord(); + + void clearMessage(); + + void loadMessage(); + void saveMessage(); + + void touchReceive(); + void touchStore(); + + void targetDetected(QNearFieldTarget *target); + void targetLost(QNearFieldTarget *target); + + void ndefMessageRead(const QNdefMessage &message); + void ndefMessageWritten(); + void targetError(QNearFieldTarget::Error error, const QNearFieldTarget::RequestId &id); + +private: + enum TouchAction { + NoAction, + ReadNdef, + WriteNdef + }; + + QNdefMessage ndefMessage() const; + +private: + Ui::MainWindow *ui; + + QNearFieldManager *m_manager; + TouchAction m_touchAction; + QNearFieldTarget::RequestId m_request; +}; + +#endif // MAINWINDOW_H diff --git a/examples/ndefeditor/mainwindow.ui b/examples/ndefeditor/mainwindow.ui new file mode 100644 index 00000000..adffa331 --- /dev/null +++ b/examples/ndefeditor/mainwindow.ui @@ -0,0 +1,225 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>MainWindow</class> + <widget class="QMainWindow" name="MainWindow"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>553</width> + <height>397</height> + </rect> + </property> + <property name="windowTitle"> + <string>NDEF Message Editor</string> + </property> + <widget class="QWidget" name="centralWidget"> + <layout class="QVBoxLayout" name="verticalLayout"> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <layout class="QHBoxLayout" name="horizontalLayout"> + <property name="leftMargin"> + <number>9</number> + </property> + <property name="rightMargin"> + <number>9</number> + </property> + <item> + <widget class="QWidget" name="status" native="true"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Fixed" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="minimumSize"> + <size> + <width>25</width> + <height>25</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>25</width> + <height>25</height> + </size> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="touchRetrieve"> + <property name="text"> + <string>Retrieve</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="touchStore"> + <property name="text"> + <string>Store</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="loadMessage"> + <property name="text"> + <string>Load</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="saveMessage"> + <property name="text"> + <string>Save</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="clearMessage"> + <property name="text"> + <string>Clear</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="addRecord"> + <property name="text"> + <string>Add</string> + </property> + </widget> + </item> + </layout> + </item> + <item> + <widget class="QScrollArea" name="scrollArea"> + <property name="frameShape"> + <enum>QFrame::NoFrame</enum> + </property> + <property name="frameShadow"> + <enum>QFrame::Plain</enum> + </property> + <property name="verticalScrollBarPolicy"> + <enum>Qt::ScrollBarAsNeeded</enum> + </property> + <property name="horizontalScrollBarPolicy"> + <enum>Qt::ScrollBarAlwaysOff</enum> + </property> + <property name="widgetResizable"> + <bool>true</bool> + </property> + <widget class="QWidget" name="scrollAreaWidgetContents"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>553</width> + <height>335</height> + </rect> + </property> + </widget> + </widget> + </item> + </layout> + </widget> + <widget class="QStatusBar" name="statusBar"/> + </widget> + <layoutdefault spacing="6" margin="11"/> + <resources/> + <connections> + <connection> + <sender>saveMessage</sender> + <signal>clicked()</signal> + <receiver>MainWindow</receiver> + <slot>saveMessage()</slot> + <hints> + <hint type="sourcelabel"> + <x>419</x> + <y>47</y> + </hint> + <hint type="destinationlabel"> + <x>275</x> + <y>198</y> + </hint> + </hints> + </connection> + <connection> + <sender>touchRetrieve</sender> + <signal>clicked()</signal> + <receiver>MainWindow</receiver> + <slot>touchReceive()</slot> + <hints> + <hint type="sourcelabel"> + <x>142</x> + <y>47</y> + </hint> + <hint type="destinationlabel"> + <x>275</x> + <y>198</y> + </hint> + </hints> + </connection> + <connection> + <sender>touchStore</sender> + <signal>clicked()</signal> + <receiver>MainWindow</receiver> + <slot>touchStore()</slot> + <hints> + <hint type="sourcelabel"> + <x>244</x> + <y>47</y> + </hint> + <hint type="destinationlabel"> + <x>275</x> + <y>198</y> + </hint> + </hints> + </connection> + <connection> + <sender>loadMessage</sender> + <signal>clicked()</signal> + <receiver>MainWindow</receiver> + <slot>loadMessage()</slot> + <hints> + <hint type="sourcelabel"> + <x>334</x> + <y>47</y> + </hint> + <hint type="destinationlabel"> + <x>275</x> + <y>198</y> + </hint> + </hints> + </connection> + <connection> + <sender>clearMessage</sender> + <signal>clicked()</signal> + <receiver>MainWindow</receiver> + <slot>clearMessage()</slot> + <hints> + <hint type="sourcelabel"> + <x>419</x> + <y>22</y> + </hint> + <hint type="destinationlabel"> + <x>275</x> + <y>198</y> + </hint> + </hints> + </connection> + </connections> + <slots> + <slot>saveMessage()</slot> + <slot>loadMessage()</slot> + <slot>touchReceive()</slot> + <slot>touchStore()</slot> + <slot>clearMessage()</slot> + </slots> +</ui> diff --git a/examples/ndefeditor/mimeimagerecordeditor.cpp b/examples/ndefeditor/mimeimagerecordeditor.cpp new file mode 100644 index 00000000..f67239c8 --- /dev/null +++ b/examples/ndefeditor/mimeimagerecordeditor.cpp @@ -0,0 +1,139 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNfc module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "mimeimagerecordeditor.h" +#include "ui_mimeimagerecordeditor.h" + +#include <QBuffer> +#include <QFileDialog> +#include <QImageReader> + +static QString imageFormatToMimeType(const QByteArray &format) +{ + if (format == "bmp") + return QLatin1String("image/bmp"); + else if (format == "gif") + return QLatin1String("image/gif"); + else if (format == "jpg" || format == "jpeg") + return QLatin1String("image/jpeg"); + else if (format == "mng") + return QLatin1String("video/x-mng"); + else if (format == "png") + return QLatin1String("image/png"); + else if (format == "pbm") + return QLatin1String("image/x-portable-bitmap"); + else if (format == "pgm") + return QLatin1String("image/x-portable-graymap"); + else if (format == "ppm") + return QLatin1String("image/x-portable-pixmap"); + else if (format == "tiff") + return QLatin1String("image/tiff"); + else if (format == "xbm") + return QLatin1String("image/x-xbitmap"); + else if (format == "xpm") + return QLatin1String("image/x-xpixmap"); + else if (format == "svg") + return QLatin1String("image/svg+xml"); + else + return QString(); +} + +MimeImageRecordEditor::MimeImageRecordEditor(QWidget *parent) : + QWidget(parent), + ui(new Ui::MimeImageRecordEditor) +{ + ui->setupUi(this); +} + +MimeImageRecordEditor::~MimeImageRecordEditor() +{ + delete ui; +} + +void MimeImageRecordEditor::setRecord(const QNdefRecord &record) +{ + m_record = record; + + QByteArray data = record.payload(); + QBuffer buffer(&data); + buffer.open(QIODevice::ReadOnly); + + QImageReader reader(&buffer); + + ui->mimeImageType->setText(imageFormatToMimeType(reader.format())); + + ui->mimeImageImage->setPixmap(QPixmap::fromImage(reader.read())); + ui->mimeImageFile->clear(); +} + +QNdefRecord MimeImageRecordEditor::record() const +{ + return m_record; +} + +void MimeImageRecordEditor::on_mimeImageOpen_clicked() +{ + QString mimeDataFile = QFileDialog::getOpenFileName(this, tr("Select Image File")); + if (mimeDataFile.isEmpty()) + return; + + QFile imageFile(mimeDataFile); + if (!imageFile.open(QIODevice::ReadOnly)) { + ui->mimeImageFile->clear(); + ui->mimeImageImage->clear(); + } + + QByteArray imageData = imageFile.readAll(); + + QBuffer buffer(&imageData); + buffer.open(QIODevice::ReadOnly); + + QImageReader reader(&buffer); + QString mimeType = imageFormatToMimeType(reader.format()); + ui->mimeImageType->setText(mimeType); + + QImage image = reader.read(); + + ui->mimeImageFile->setText(mimeDataFile); + ui->mimeImageImage->setPixmap(QPixmap::fromImage(image)); + + m_record.setType(mimeType.toAscii()); + m_record.setPayload(imageData); +} diff --git a/examples/ndefeditor/mimeimagerecordeditor.h b/examples/ndefeditor/mimeimagerecordeditor.h new file mode 100644 index 00000000..a62cce13 --- /dev/null +++ b/examples/ndefeditor/mimeimagerecordeditor.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNfc module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#ifndef MIMEIMAGERECORDEDITOR_H +#define MIMEIMAGERECORDEDITOR_H + +#include <qnfcglobal.h> + +#include <QWidget> + +#include <qndefrecord.h> + +QTNFC_USE_NAMESPACE + +namespace Ui { + class MimeImageRecordEditor; +} + +class MimeImageRecordEditor : public QWidget +{ + Q_OBJECT + +public: + explicit MimeImageRecordEditor(QWidget *parent = 0); + ~MimeImageRecordEditor(); + + void setRecord(const QNdefRecord &record); + QNdefRecord record() const; + +private: + Ui::MimeImageRecordEditor *ui; + QNdefRecord m_record; + +private slots: + void on_mimeImageOpen_clicked(); +}; + +#endif // MIMEIMAGERECORDEDITOR_H diff --git a/examples/ndefeditor/mimeimagerecordeditor.ui b/examples/ndefeditor/mimeimagerecordeditor.ui new file mode 100644 index 00000000..4cde216f --- /dev/null +++ b/examples/ndefeditor/mimeimagerecordeditor.ui @@ -0,0 +1,83 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>MimeImageRecordEditor</class> + <widget class="QWidget" name="MimeImageRecordEditor"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>403</width> + <height>106</height> + </rect> + </property> + <property name="windowTitle"> + <string>Form</string> + </property> + <layout class="QFormLayout" name="formLayout"> + <item row="0" column="0" colspan="2"> + <widget class="QLabel" name="label_4"> + <property name="text"> + <string>MIME Record <image/*></string> + </property> + </widget> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="label_11"> + <property name="text"> + <string>Type:</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QLabel" name="mimeImageType"> + <property name="sizePolicy"> + <sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>image/</string> + </property> + </widget> + </item> + <item row="2" column="0"> + <widget class="QLabel" name="label_12"> + <property name="text"> + <string>File:</string> + </property> + </widget> + </item> + <item row="2" column="1"> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <widget class="QLineEdit" name="mimeImageFile"> + <property name="sizePolicy"> + <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + </widget> + </item> + <item> + <widget class="QToolButton" name="mimeImageOpen"> + <property name="text"> + <string>...</string> + </property> + </widget> + </item> + </layout> + </item> + <item row="3" column="1"> + <widget class="QLabel" name="mimeImageImage"> + <property name="text"> + <string>TextLabel</string> + </property> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/examples/ndefeditor/ndefeditor.pro b/examples/ndefeditor/ndefeditor.pro new file mode 100644 index 00000000..8ed0d7e5 --- /dev/null +++ b/examples/ndefeditor/ndefeditor.pro @@ -0,0 +1,28 @@ +INCLUDEPATH += $$PWD/../../src/connectivity/nfc +DEPENDPATH += $$PWD/../../src/connectivity/nfc + +QT += nfc widgets + +TARGET = ndefeditor +TEMPLATE = app + +SOURCES += \ + main.cpp \ + mainwindow.cpp \ + textrecordeditor.cpp \ + urirecordeditor.cpp \ + mimeimagerecordeditor.cpp + +HEADERS += \ + mainwindow.h \ + textrecordeditor.h \ + urirecordeditor.h \ + mimeimagerecordeditor.h + +FORMS += \ + mainwindow.ui \ + textrecordeditor.ui \ + urirecordeditor.ui \ + mimeimagerecordeditor.ui + +symbian:TARGET.CAPABILITY += LocalServices diff --git a/examples/ndefeditor/textrecord.ui b/examples/ndefeditor/textrecord.ui new file mode 100644 index 00000000..eedd2d83 --- /dev/null +++ b/examples/ndefeditor/textrecord.ui @@ -0,0 +1,21 @@ +<ui version="4.0"> + <author/> + <comment/> + <exportmacro/> + <class>TextRecord</class> + <widget class="QWidget" name="TextRecord"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>400</width> + <height>300</height> + </rect> + </property> + <property name="windowTitle"> + <string>Form</string> + </property> + </widget> + <pixmapfunction/> + <connections/> +</ui> diff --git a/examples/ndefeditor/textrecordeditor.cpp b/examples/ndefeditor/textrecordeditor.cpp new file mode 100644 index 00000000..37d66b14 --- /dev/null +++ b/examples/ndefeditor/textrecordeditor.cpp @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNfc module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "textrecordeditor.h" +#include "ui_textrecordeditor.h" + +#include <QtCore/QDebug> + +TextRecordEditor::TextRecordEditor(QWidget *parent) : + QWidget(parent), + ui(new Ui::TextRecordEditor) +{ + ui->setupUi(this); +} + +TextRecordEditor::~TextRecordEditor() +{ + delete ui; +} + +void TextRecordEditor::setRecord(const QNdefNfcTextRecord &textRecord) +{ + ui->text->setText(textRecord.text()); + ui->locale->setText(textRecord.locale()); + + if (textRecord.encoding() == QNdefNfcTextRecord::Utf8) + ui->encoding->setCurrentIndex(0); + else if (textRecord.encoding() == QNdefNfcTextRecord::Utf16) + ui->encoding->setCurrentIndex(1); +} + +QNdefNfcTextRecord TextRecordEditor::record() const +{ + QNdefNfcTextRecord record; + + if (ui->encoding->currentIndex() == 0) + record.setEncoding(QNdefNfcTextRecord::Utf8); + else if (ui->encoding->currentIndex() == 1) + record.setEncoding(QNdefNfcTextRecord::Utf16); + + record.setLocale(ui->locale->text()); + + record.setText(ui->text->text()); + + return record; +} diff --git a/examples/ndefeditor/textrecordeditor.h b/examples/ndefeditor/textrecordeditor.h new file mode 100644 index 00000000..5f6218d1 --- /dev/null +++ b/examples/ndefeditor/textrecordeditor.h @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNfc module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TEXTRECORDEDITOR_H +#define TEXTRECORDEDITOR_H + +#include <QWidget> + +#include <qndefnfctextrecord.h> + +namespace Ui { + class TextRecordEditor; +} + +QTNFC_USE_NAMESPACE + +class TextRecordEditor : public QWidget +{ + Q_OBJECT + +public: + explicit TextRecordEditor(QWidget *parent = 0); + ~TextRecordEditor(); + + void setRecord(const QNdefNfcTextRecord &textRecord); + QNdefNfcTextRecord record() const; + +private: + Ui::TextRecordEditor *ui; +}; + +#endif // TEXTRECORDEDITOR_H diff --git a/examples/ndefeditor/textrecordeditor.ui b/examples/ndefeditor/textrecordeditor.ui new file mode 100644 index 00000000..41950e7d --- /dev/null +++ b/examples/ndefeditor/textrecordeditor.ui @@ -0,0 +1,101 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>TextRecordEditor</class> + <widget class="QWidget" name="TextRecordEditor"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>344</width> + <height>115</height> + </rect> + </property> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Minimum"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="windowTitle"> + <string>Form</string> + </property> + <layout class="QFormLayout" name="formLayout"> + <property name="rowWrapPolicy"> + <enum>QFormLayout::WrapLongRows</enum> + </property> + <item row="0" column="0" colspan="2"> + <widget class="QLabel" name="label_5"> + <property name="text"> + <string>NFC Text Record</string> + </property> + </widget> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="label_7"> + <property name="text"> + <string>Text:</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QLineEdit" name="text"> + <property name="sizePolicy"> + <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + </widget> + </item> + <item row="2" column="0"> + <widget class="QLabel" name="label_6"> + <property name="text"> + <string>Locale:</string> + </property> + </widget> + </item> + <item row="2" column="1"> + <widget class="QLineEdit" name="locale"> + <property name="sizePolicy"> + <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string/> + </property> + </widget> + </item> + <item row="3" column="0"> + <widget class="QLabel" name="label"> + <property name="text"> + <string>Encoding:</string> + </property> + </widget> + </item> + <item row="3" column="1"> + <widget class="QComboBox" name="encoding"> + <property name="sizePolicy"> + <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <item> + <property name="text"> + <string>UTF-8</string> + </property> + </item> + <item> + <property name="text"> + <string>UTF-16</string> + </property> + </item> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/examples/ndefeditor/urirecordeditor.cpp b/examples/ndefeditor/urirecordeditor.cpp new file mode 100644 index 00000000..a8f30c71 --- /dev/null +++ b/examples/ndefeditor/urirecordeditor.cpp @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNfc module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "urirecordeditor.h" +#include "ui_urirecordeditor.h" + +#include <QtCore/QUrl> + +UriRecordEditor::UriRecordEditor(QWidget *parent) : + QWidget(parent), + ui(new Ui::UriRecordEditor) +{ + ui->setupUi(this); +} + +UriRecordEditor::~UriRecordEditor() +{ + delete ui; +} + +void UriRecordEditor::setRecord(const QNdefNfcUriRecord &uriRecord) +{ + ui->uri->setText(uriRecord.uri().toString()); +} + +QNdefNfcUriRecord UriRecordEditor::record() const +{ + QNdefNfcUriRecord record; + + record.setUri(ui->uri->text()); + + return record; +} diff --git a/examples/ndefeditor/urirecordeditor.h b/examples/ndefeditor/urirecordeditor.h new file mode 100644 index 00000000..e92309df --- /dev/null +++ b/examples/ndefeditor/urirecordeditor.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNfc module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#ifndef URIRECORDEDITOR_H +#define URIRECORDEDITOR_H + +#include <QWidget> + +#include <qndefnfcurirecord.h> + +namespace Ui { + class UriRecordEditor; +} + +QTNFC_USE_NAMESPACE + +class UriRecordEditor : public QWidget +{ + Q_OBJECT + +public: + explicit UriRecordEditor(QWidget *parent = 0); + ~UriRecordEditor(); + + void setRecord(const QNdefNfcUriRecord &uriRecord); + QNdefNfcUriRecord record() const; + +private: + Ui::UriRecordEditor *ui; +}; + +#endif // URIRECORDEDITOR_H diff --git a/examples/ndefeditor/urirecordeditor.ui b/examples/ndefeditor/urirecordeditor.ui new file mode 100644 index 00000000..929c1eb4 --- /dev/null +++ b/examples/ndefeditor/urirecordeditor.ui @@ -0,0 +1,45 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>UriRecordEditor</class> + <widget class="QWidget" name="UriRecordEditor"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>439</width> + <height>59</height> + </rect> + </property> + <property name="windowTitle"> + <string>Form</string> + </property> + <layout class="QFormLayout" name="formLayout"> + <item row="0" column="0" colspan="2"> + <widget class="QLabel" name="label_8"> + <property name="text"> + <string>NFC URI Record</string> + </property> + </widget> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="label_9"> + <property name="text"> + <string>Uri:</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QLineEdit" name="uri"> + <property name="sizePolicy"> + <sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/examples/poster/poster.pro b/examples/poster/poster.pro new file mode 100644 index 00000000..de2e1a05 --- /dev/null +++ b/examples/poster/poster.pro @@ -0,0 +1,13 @@ +QT += declarative quick network nfc widgets + +SOURCES += \ + qmlposter.cpp + +TARGET = qml_poster +TEMPLATE = app + +RESOURCES += \ + poster.qrc + +OTHER_FILES += \ + poster.qml diff --git a/examples/poster/poster.qml b/examples/poster/poster.qml new file mode 100644 index 00000000..c521eb43 --- /dev/null +++ b/examples/poster/poster.qml @@ -0,0 +1,186 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNfc module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import Qt 4.7 +import QtNfc 5.0 + +Rectangle { + id: root + width: 640 + height: 360 + + NearField { + id: nearfield + + filter: [ + NdefFilter { type: "urn:nfc:wkt:U"; minimum: 1; maximum: 1 }, + NdefFilter { type: "urn:nfc:wkt:T"; minimum: 1 } + ] + + onMessageRecordsChanged: { + posterText.text = ""; + posterImage.source = ""; + posterUrl.text = ""; + + var currentLocaleMatch = NdefTextRecord.LocaleMatchedNone; + var i; + for (i = 0; i < messageRecords.length; ++i) { + if (messageRecords[i].recordType == "urn:nfc:wkt:T") { + if (messageRecords[i].localeMatch > currentLocaleMatch) { + currentLocaleMatch = messageRecords[i].localeMatch; + posterText.text = messageRecords[i].text; + } + } else if (messageRecords[i].recordType == "urn:nfc:wkt:U") { + posterUrl.text = messageRecords[i].uri; + } else if (messageRecords[i].recordType.substr(0, 19) == "urn:nfc:mime:image/") { + posterImage.source = messageRecords[i].uri; + } + } + + root.state = "show"; + } + } + + Text { + id: touchText + anchors.horizontalCenter: parent.horizontalCenter + anchors.verticalCenter: parent.verticalCenter + text: "Touch an NFC tag" + font.bold: true + font.pointSize: 18 + } + + Text { + id: posterText + anchors.horizontalCenter: parent.right + anchors.horizontalCenterOffset: - parent.width / 4 + y: -height + font.bold: true + font.pointSize: 18 + } + + Image { + id: posterImage + scale: Image.PreserveAspectFit + height: parent.height * 0.8 + width: height * sourceSize.width / sourceSize.height + anchors.verticalCenter: parent.verticalCenter + x: -width + smooth: true + } + + Text { + id: posterUrl + anchors.horizontalCenter: parent.right + anchors.horizontalCenterOffset: - parent.width / 4 + y: parent.height + font.italic: true + font.pointSize: 14 + } + + MouseArea { + id: openMouseArea + anchors.fill: parent + enabled: root.state == "show" + + onClicked: Qt.openUrlExternally(posterUrl.text) + + Rectangle { + id: testTouch + width: 50 + height: 50 + color: "lightsteelblue" + opacity: 0.3 + anchors.top: parent.top + anchors.right: close.left + anchors.rightMargin: 10 + + MouseArea { + id: touchMouseArea + anchors.fill: parent + + onClicked: { + if (root.state == "") { + root.state = "show"; + } else { + root.state = ""; + } + } + } + } + + Rectangle { + id: close + width: 50 + height: 50 + color: "black" + radius: 0 + opacity: 0.3 + anchors.top: parent.top + anchors.topMargin: 0 + anchors.right: parent.right + anchors.rightMargin: 0 + + MouseArea { + id: closeMouseArea + anchors.fill: parent + + onClicked: Qt.quit(); + } + } + } + + states: [ + State { + name: "show" + PropertyChanges { target: posterText; y: root.height / 3 } + PropertyChanges { target: posterUrl; y: 2 * root.height / 3 } + PropertyChanges { target: posterImage; x: root.width / 20 } + PropertyChanges { target: touchText; opacity: 0 } + } + ] + + transitions: [ + Transition { + PropertyAnimation { easing.type: Easing.OutQuad; properties: "x,y" } + PropertyAnimation { property: "opacity"; duration: 125 } + } + ] +} diff --git a/examples/poster/poster.qrc b/examples/poster/poster.qrc new file mode 100644 index 00000000..d8286e71 --- /dev/null +++ b/examples/poster/poster.qrc @@ -0,0 +1,6 @@ +<RCC> + <qresource prefix="/"> + <file>poster.qml</file> + </qresource> + <qresource prefix="/icons"/> +</RCC> diff --git a/examples/poster/qmlposter.cpp b/examples/poster/qmlposter.cpp new file mode 100644 index 00000000..ccbf9700 --- /dev/null +++ b/examples/poster/qmlposter.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNfc module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets/QApplication> +#include <QtQuick/QQuickView> +#include <QtDeclarative/QDeclarativeEngine> + +int main(int argc, char *argv[]) +{ + QApplication application(argc, argv); + const QString mainQmlApp = QLatin1String("qrc:/poster.qml"); + QQuickView view; + view.setSource(QUrl(mainQmlApp)); + view.setResizeMode(QQuickView::SizeRootObjectToView); + // Qt.quit() called in embedded .qml by default only emits + // quit() signal, so do this (optionally use Qt.exit()). + QObject::connect(view.engine(), SIGNAL(quit()), qApp, SLOT(quit())); +#if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_6) || defined(Q_WS_SIMULATOR) + view.showFullScreen(); +#else // Q_OS_SYMBIAN + view.setGeometry(QRect(100, 100, 640, 360)); + view.show(); +#endif // Q_OS_SYMBIAN + return application.exec(); +} diff --git a/examples/scanner/qmlscanner.cpp b/examples/scanner/qmlscanner.cpp new file mode 100644 index 00000000..47dcb15e --- /dev/null +++ b/examples/scanner/qmlscanner.cpp @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the QtBluetooth module. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtWidgets/QApplication> +#include <QtQuick/QQuickView> +#include <QtDeclarative/QDeclarativeEngine> + +int main(int argc, char *argv[]) +{ + QApplication application(argc, argv); + const QString mainQmlApp = QLatin1String("qrc:/scanner.qml"); + QQuickView view; + view.setSource(QUrl(mainQmlApp)); + view.setResizeMode(QQuickView::SizeRootObjectToView); + // Qt.quit() called in embedded .qml by default only emits + // quit() signal, so do this (optionally use Qt.exit()). + QObject::connect(view.engine(), SIGNAL(quit()), qApp, SLOT(quit())); +#if defined(Q_OS_SYMBIAN) + view.showFullScreen(); +#elif defined(Q_WS_MAEMO_6) + view.showFullScreen(); +#else // Q_OS_SYMBIAN + view.setGeometry(QRect(100, 100, 360, 640)); + view.show(); +#endif // Q_OS_SYMBIAN + return application.exec(); +} diff --git a/examples/scanner/scanner.pro b/examples/scanner/scanner.pro new file mode 100644 index 00000000..39eb453b --- /dev/null +++ b/examples/scanner/scanner.pro @@ -0,0 +1,11 @@ +QT += widgets declarative network bluetooth quick +SOURCES += qmlscanner.cpp + +TARGET = qml_scanner +TEMPLATE = app + +RESOURCES += \ + scanner.qrc + +OTHER_FILES += \ + scanner.qml diff --git a/examples/scanner/scanner.qml b/examples/scanner/scanner.qml new file mode 100644 index 00000000..ac16504d --- /dev/null +++ b/examples/scanner/scanner.qml @@ -0,0 +1,251 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the QtBluetooth module. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import Qt 4.7 +import QtBluetooth 5.0 + +Rectangle { + id: top + + property BluetoothService currentService + property alias minimalDiscovery: myModel.minimalDiscovery + property alias uuidFilder: myModel.uuidFilter + + anchors.fill: parent + + BluetoothDiscoveryModel { + id: myModel + minimalDiscovery: true + onDiscoveryChanged: busy.running = discovery; +// onNewServiceDiscovered: console.log("Found new service " + service.deviceAddress + " " + service.deviceName + " " + service.serviceName); +// uuidFilter: "e8e10f95-1a70-4b27-9ccf-02010264e9c9" + } + + Rectangle { + id: busy + property bool running: true + + width: top.width/1.2; + x: top.width/2-width/2 + anchors.top: top.top; + height: 20; + radius: 5 + color: "#1c56f3" + + Text { + id: text + text: "<b>Scanning</b>" + x: busy.width/2-paintedWidth/2 + y: busy.height/2-paintedHeight/2 + } + + SequentialAnimation on color { + id: busyThrobber + ColorAnimation { easing.type: Easing.InOutSine; from: "#1c56f3"; to: "white"; duration: 1000; } + ColorAnimation { easing.type: Easing.InOutSine; to: "#1c56f3"; from: "white"; duration: 1000 } + loops: Animation.Infinite + } + states: [ + State { + name: "stopped" + when: !busy.running + PropertyChanges { target: busy; height: 0; } + PropertyChanges { target: busyThrobber; running: false } + PropertyChanges { target: text; visible: false } + } + ] + transitions: [ + Transition { + from: "*" + to: "stopped" + reversible: true + NumberAnimation { target: busy; property: "height"; to: 0; duration: 200 } + } + ] + } + + Component { + id: del; + + Item { + id: item + + function item_clicked(service) { + console.log("Clicked " + service.deviceName + service.deviceAddress); + top.currentService = service; + } + + property int text_height: 5+(bticon.height > bttext.height ? bticon.height : bttext.height) + + height: text_height + width: parent.width + + Column { + anchors.fill: item + Row { + width: parent.width + Image { + id: bticon + source: icon; + width: del.height; + } + Text { + id: bttext + text: name; + font.family: "FreeSerif" + font.pointSize: 12 + } + } + Text { + function get_details(s) { + var str = "Address: " + s.deviceAddress; + if (s.serviceName) { str += "<br>Service: " + s.serviceName; } + if (s.serviceDescription) { str += "<br>Description: " + s.serviceDescription; } + if (s.serviceProtocol) { str += "<br>Port: " + s.serviceProtocol; } + if (s.servicePort) { str += "<br>Port: " + s.servicePort; } + return str; + } + + id: details + opacity: 0.0 + text: get_details(service) + font: bttext.font + x: bticon.width + } + } + + MouseArea { + anchors.fill: parent + onClicked: item.state = ((item.state == "details") ? "" : "details") + } + + states: [ + State { + name: "details" + PropertyChanges { target: item; height: item.text_height+details.height; } + PropertyChanges { target: details; opacity: 1.0 } + } + ] + transitions: [ + Transition { + from: "*" + to: "details" + reversible: true + NumberAnimation { target: item; property: "height"; duration: 200 } + NumberAnimation { target: details; property: "lopacity"; duration: 200 } + } + ] + } + } + + Component { + id: highlightBox + + Rectangle { + id: background + anchors.fill: del + border.color: "#34ca57" + radius: 5 + border.width: 2 + } + + } + + ListView { + id: mainList + width: top.width + anchors.top: busy.bottom +// anchors.bottom: top.bottom + anchors.bottom: fullbutton.top + + model: myModel + highlight: highlightBox + delegate: del + focus: true + } + + Rectangle { + id: fullbutton + + function button_clicked() { + myModel.minimalDiscovery = !myModel.minimalDiscovery; + fullbutton.state = fullbutton.state == "clicked" ? "" : "clicked"; + myModel.setDiscovery(true); + } + + anchors.bottom: top.bottom + anchors.margins: 3 + + width: top.width-6 + x: 3 + height: 20 + + radius: 5 + border.width: 1 + color: "white" + + Text { + id: label + text: "Full Discovery" + x: parent.width/2-paintedWidth/2; + y: parent.height/2-paintedHeight/2; + } + + MouseArea { + anchors.fill: parent + onClicked: fullbutton.button_clicked(); + } + + states: [ + State { + name: "clicked" + PropertyChanges { target: fullbutton; color: "#1c56f3" } + } + ] + transitions: [ + Transition { + from: "*" + to: "details" + reversible: true + ColorAnimation { duration: 200 } + } + ] + } +} diff --git a/examples/scanner/scanner.qrc b/examples/scanner/scanner.qrc new file mode 100644 index 00000000..93e9ce08 --- /dev/null +++ b/examples/scanner/scanner.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/"> + <file>scanner.qml</file> + </qresource> +</RCC> |