如何使用 Qt 5.6 在 Android 上启用 NFC
NFC 技术在 Android 应用开发中变得越来越重要。在本文中,我将介绍如何使用 Qt 5.6 来实现 Android 上的 NFC 功能。这个教程基于一个创建于 8 年 8 个月前的问题,并在 7 年 3 个月前进行了修改,讨论了如何在 Android 手机上使用 Qt 的 NFC 模块读取 NFC 标签。
环境设置
根据 Qt 官方文档,从 Qt 5.6 版本开始,Qt 将支持 Android 的 NFC 功能。以下是关键步骤:
1. 从源码构建 Qt 5.6 并安装
由于 Qt 5.6 版本当时尚未正式发布,需要从源码构建并安装到 Qt Creator。可以参考 Qt 的官方构建指南 来进行构建。
2. 创建 NFC 测试应用
在 Qt 应用中,我们需要一个 QNearFieldManager
实例来处理 NFC 的检测。以下是一个简单的示例应用代码:
#include <QLabel>
#include <QVBoxLayout>
#include <QNearFieldManager>
#include <QNearFieldTarget>
#include <QDebug>
#include "window.h"Window::Window(QWidget *parent)
: QWidget(parent)
{nfcLabel_ = new QLabel(this);QVBoxLayout *mainLayout = new QVBoxLayout;mainLayout->addWidget(nfcLabel_, 1);setLayout(mainLayout);setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));setWindowTitle(tr("NFC Test"));nfc_ = new QNearFieldManager(this);if (nfc_->isAvailable()) {nfcLabel_->setText("NFC available");} else {nfcLabel_->setText("NFC not available");qWarning() << "NFC not available";}nfc_->setTargetAccessModes(QNearFieldManager::NdefReadTargetAccess); // 可选nfc_->registerNdefMessageHandler(this, SLOT(handleNdefMessage(QNdefMessage,QNearFieldTarget*))); // 可选connect(nfc_, SIGNAL(targetDetected(QNearFieldTarget*)), this, SLOT(targetDetected(QNearFieldTarget*)));connect(nfc_, SIGNAL(targetLost(QNearFieldTarget*)), this, SLOT(targetLost(QNearFieldTarget*)));if (!nfc_->startTargetDetection()) {qWarning() << "NFC target detection could not be started";}
}Window::~Window()
{nfc_->stopTargetDetection();
}void Window::targetDetected(QNearFieldTarget * /*target*/)
{nfcLabel_->setText("Target detected");
}void Window::targetLost(QNearFieldTarget *target)
{nfcLabel_->setText("Target lost");target->deleteLater();
}void Window::handleNdefMessage(const QNdefMessage &/*message*/, QNearFieldTarget */*target*/)
{qDebug() << "Ndef Message";
}
3. 修改 AndroidManifest.xml
为了在 Android 上使用 NFC,我们需要修改 AndroidManifest.xml
文件。下面是一个示例文件:
<?xml version="1.0"?>
<manifest package="org.qtproject.example" xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.0" android:versionCode="1" android:installLocation="auto"><application android:hardwareAccelerated="true" android:name="org.qtproject.qt5.android.bindings.QtApplication" android:label="-- %%INSERT_APP_NAME%% --" android:theme="@android:style/Theme.Holo"><activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation" android:name="org.qtproject.qt5.android.bindings.QtActivity" android:label="-- %%INSERT_APP_NAME%% --" android:screenOrientation="unspecified" android:launchMode="singleTop"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></intent-filter><!-- 添加此 intent-filter 以触发 targetDetected 和 targetLost 事件 --><intent-filter><action android:name="android.nfc.action.TAG_DISCOVERED"/><category android:name="android.intent.category.DEFAULT"/></intent-filter><meta-data android:name="android.app.lib_name" android:value="-- %%INSERT_APP_LIB_NAME%% --"/><meta-data android:name="android.app.qt_sources_resource_id" android:resource="@array/qt_sources"/><meta-data android:name="android.app.repository" android:value="default"/><meta-data android:name="android.app.qt_libs_resource_id" android:resource="@array/qt_libs"/><meta-data android:name="android.app.bundled_libs_resource_id" android:resource="@array/bundled_libs"/><meta-data android:name="android.app.bundle_local_qt_libs" android:value="-- %%BUNDLE_LOCAL_QT_LIBS%% --"/><meta-data android:name="android.app.bundled_in_lib_resource_id" android:resource="@array/bundled_in_lib"/><meta-data android:name="android.app.bundled_in_assets_resource_id" android:resource="@array/bundled_in_assets"/><meta-data android:name="android.app.use_local_qt_libs" android:value="-- %%USE_LOCAL_QT_LIBS%% --"/><meta-data android:name="android.app.libs_prefix" android:value="/data/local/tmp/qt/"/><meta-data android:name="android.app.load_local_libs" android:value="-- %%INSERT_LOCAL_LIBS%% --"/><meta-data android:name="android.app.load_local_jars" android:value="-- %%INSERT_LOCAL_JARS%% --"/><meta-data android:name="android.app.static_init_classes" android:value="-- %%INSERT_INIT_CLASSES%% --"/><meta-data android:value="@string/ministro_not_found_msg" android:name="android.app.ministro_not_found_msg"/><meta-data android:value="@string/ministro_needed_msg" android:name="android.app.ministro_needed_msg"/><meta-data android:value="@string/fatal_error_msg" android:name="android.app.fatal_error_msg"/></activity></application><uses-sdk android:minSdkVersion="10" android:targetSdkVersion="14"/><supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" android:smallScreens="true"/><uses-feature android:name="android.hardware.nfc" android:required="true"/><uses-permission android:name="android.permission.NFC"/>
</manifest>
4. 解决应用启动问题
当 NFC 标签靠近设备时,应用会重新启动,这可能导致崩溃。为了解决这个问题,我们可以在 AndroidManifest.xml
中增加以下属性:
<activity ... android:alwaysRetainTaskState="true" android:launchMode="singleInstance">
5. 处理标签检测信号
确保在应用运行时处理标签检测信号,而不触发应用重新启动。
结论
通过修改和配置 AndroidManifest.xml
文件及调整代码逻辑,可以实现在 Android 上使用 Qt 5.6 成功读取 NFC 标签。如果你遇到问题,可能需要检查你的 NFC 硬件兼容性,或者为你的设备定制解决方案。希望这篇文章对你的开发工作有所帮助。如果你有其他问题或建议,欢迎留言讨论。