nfcnppexample.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
  3. * All rights reserved.
  4. * This component and the accompanying materials are made available
  5. * under the terms of "Eclipse Public License v1.0"
  6. * which accompanies this distribution, and is available
  7. * at the URL "http://www.eclipse.org/legal/epl-v10.html".
  8. *
  9. * Initial Contributors:
  10. * Nokia Corporation - initial contribution.
  11. *
  12. * Contributors:
  13. *
  14. * Description: NfcNppExample.
  15. */
  16. #ifndef NFCNPPEXAMPLE_H
  17. #define NFCNPPEXAMPLE_H
  18. /*! \file
  19. * \brief NfcNppExample creates a peer-to-peer connection between two
  20. * NFC enabled devices and demonstrates how to send and receive NDEF messages
  21. * between the devices using NPP protocol.
  22. */
  23. #include <QObject>
  24. #include <QList>
  25. #include <qmobilityglobal.h>
  26. #include <qllcpsocket.h>
  27. #include <qndefmessage.h>
  28. QTM_BEGIN_NAMESPACE
  29. class QNearFieldManager;
  30. class QNearFieldTarget;
  31. class QLlcpServer;
  32. QTM_END_NAMESPACE
  33. QTM_USE_NAMESPACE // Use Qt Mobility namespace
  34. /*!
  35. * \brief
  36. * NfcNppExample class is used to create peer-to-peer connection between
  37. * between two NFC enabled devices. It notifies UI of successful connect
  38. * event, when the data is sent to other device and constructs and deconstructs
  39. * the sent and received NDEF messages using NPP format.
  40. */
  41. class NfcNppExample : public QObject
  42. {
  43. Q_OBJECT
  44. public:
  45. //! Constructor.
  46. /*!
  47. * \param parent Parent for the object.
  48. */
  49. explicit NfcNppExample(QObject *parent = 0);
  50. //! Desctructor.
  51. /*!
  52. */
  53. virtual ~NfcNppExample();
  54. Q_SIGNALS:
  55. //! Signal to close active client connection.
  56. /*!
  57. * Data has been written to remote device and the connection
  58. * can be closed. Signal is emitted to close connection.
  59. */
  60. void closeConnection();
  61. //! Signal to notify UI, that a new NDEF message has been received.
  62. /*!
  63. * Received NDEF Push Protocol message is parsed before the content data
  64. * is extracted from the NFC record included in the NDEF message.
  65. *
  66. * \param type Type of the NFC record. "U" for the NFC URI record and "T"
  67. * for the NFC Text record.
  68. * \param messageData Extracted NFC record data in string format.
  69. *
  70. */
  71. void messageReceived(const QString &type, const QString &messageData);
  72. //! Signal to notify UI, that the LLCP connection between devices has been succesfully created.
  73. /*!
  74. * Signal is emitted when the client socket provided by the LLCP server or the
  75. * server socket has connected succesfully.
  76. */
  77. void targetConnected();
  78. public Q_SLOTS:
  79. //! Sends a message
  80. /*!
  81. * Sends data to a remote device.
  82. *
  83. * \param type Type of the NFC record. "U" for the NFC URI record and "T"
  84. * for the NFC Text record.
  85. * \param text NFC record data in string format.
  86. */
  87. void sendMessage(const QString &type, const QString &text);
  88. private Q_SLOTS:
  89. //! Close the active client connection.
  90. /*!
  91. * Close the active connection.
  92. */
  93. void handleCloseConnection();
  94. //! Read message data.
  95. /*!
  96. * Reads data from the socket, that the remote device has sent.
  97. */
  98. void readMessage();
  99. //! Called when an NFC enabled device is in range.
  100. /*!
  101. * This slot is called whenever a target is detected. The \a target parameter
  102. * represents the detected target. Target is checked first, whether it supports
  103. * LLCP access.
  104. *
  105. * \param target Detected target.
  106. */
  107. void handleTargetDetected(QNearFieldTarget *target);
  108. //! Called when NFC enabled device is out of range.
  109. /*!
  110. * This slot is called whenever a target moves out of proximity. The \a target
  111. * parameter represents the lost target.
  112. *
  113. * \param target Lost target.
  114. */
  115. void handleTargetLost(QNearFieldTarget *target);
  116. //! Handles new connection.
  117. /*!
  118. * Creates a LLCP socket connection to remote device.
  119. */
  120. void handleNewConnection();
  121. //! Parse NDEF Push Protocol (NPP) message.
  122. /*!
  123. * NDEF messages are extracted from the NPP message.
  124. *
  125. * \param array NPP message as binary data.
  126. */
  127. QList<QNdefMessage> parseNppMessage(const QByteArray &array) const;
  128. //! Create NDEF Push Protocol (NPP) message.
  129. /*!
  130. * Creates an NPP message from the NDEF messages contained in the \a messages parameter.
  131. *
  132. * \param messages List of NDEF messages to be using NPP protocol.
  133. * \return NPP message in binary data format.
  134. */
  135. QByteArray createNppMessage(const QList<QNdefMessage> &messages) const;
  136. //! Handle socket state changes.
  137. /*!
  138. * This slot is called whenever the state of the client or the server socket changes.
  139. * Mainly the ConnectedState and UnconnectedState states are handled to emit the
  140. * corresponding signals to the UI.
  141. *
  142. * \param socketState State of the socket.
  143. * \sa targetConnected() targetDisconnected()
  144. */
  145. void handleSocketStateChanged(QLlcpSocket::SocketState socketState);
  146. private:
  147. /*! \brief Pointer to the QNearFieldManager class instance, owned. */
  148. QNearFieldManager *nfcManager;
  149. /*! \brief Pointer to the QLlcpServer class instance, owned. */
  150. QLlcpServer *nfcServer;
  151. /*! \brief Pointer to the QLlcpSocket class instance, owned. */
  152. QLlcpSocket *nfcClientSocket;
  153. /*! \brief Pointer to the QLlcpSocket class instance provided by the QLlcpServer instance, owned. */
  154. QLlcpSocket *nfcServerSocket;
  155. /*! \brief Message buffer, that is sent. */
  156. QByteArray ndefMessage;
  157. };
  158. #endif // NFCNPPEXAMPLE_H