在QML中用NFC 初始化蓝牙连接聊天程序
这篇文章演示如何使用QML找到支持NFC的设备,并通过NFC建立蓝牙连接。
Article Metadata
Code Example
Source file: Media:NFCBtChat.zip
Tested with
Devices(s): Nokia C7
Article
Created: mahbub_s60
(15 Oct 2011)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
介绍
本文翻译自NFC initiated Bluetooth chat application in QML 这个应用通过NFC初始化一个蓝牙连接,然后通过这个蓝牙连接发送文本聊天信息。运行应用在前台的两台手机相碰时,应用建立两台手机之间的蓝牙连接。一旦建立了蓝牙连接,我们就可以使用socket来传送数据(通常的做法)。
如何把数据传递到QML中
QDeclarativeItem 类提供了在QML中可视物体的大多数基本功能。因此我们的类继承自QDeclarativeItem。
class ConnectionNotifier : public QDeclarativeItem
{
Q_OBJECT
Q_PROPERTY( QString msg READ msg WRITE setMsg )
public:
// Allow call directly C++ method from QML
Q_INVOKABLE void QuitApplication();
Q_INVOKABLE void SendMessage(const QString &aData);
public Q_SLOTS:
void showMessage(const QString & aMsg, const QString & aDevice);
void newDeviceFound();
Q_SIGNALS:
void notConnected();
void messageReceived(const QString & message);
private:
QString mMsg;
ConnectionEngine * mEngine;
};
QML 代码
在这个应用中我们创建若干长方形元素,和其他物品例如图, TextEdit 作为长方形的一部分。对话窗口用带model的list box实现。一收到的消息就添加到model中。
ConnectionNotifier
{
id: myBtConnotifier
onNotConnected:
{
textEdit.text = "Not connected"
}
onMessageReceived:
{
nfcbtchat.append({"myself": "", "other": "Other:"+ myBtConnotifier.msg})
}
onServiceDiscoveredMsg:
{
textEdit.text = "Service Found, Connecting..."
}
onNewConnection:
{
textEdit.text = "Device connected..."
}
onNewDeviceFoundMsg:
{
textEdit.text = "Device found, looking service..."
}
onConnectionClosed:
{
textEdit.text = "Connection Closed"
}
}
如何使用这个例子
工程中有一个自签名的sis文件(debug版),可直接安装这个sis文件。应用下方有3个图标,第一个发送消息,第二个清除消息,第三个推出程序。我们将sis文件安装到C7上, 打开蓝牙功能, 然后轻触两台手机,等待,hello world 文本会变成connected字样。连接成功后我们可以开始打字对话。下面是C7上的截屏。
示例代码
下面的代码用来在两台Belle系统的C7手机上传送消息。由于Qt Mobility的一些性能因素,建立连接的时间比普通蓝牙连接长很多。
下载示例: File:NFCBtChat.zip


(no comments yet)