File indexing completed on 2024-04-28 03:52:07
0001 /* 0002 * SPDX-FileCopyrightText: 2015 David Rosca <nowrep@gmail.com> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include "chatprofile.h" 0008 #include "initmanagerjob.h" 0009 #include "manager.h" 0010 #include "pendingcall.h" 0011 0012 #include <QCoreApplication> 0013 #include <QDBusObjectPath> 0014 #include <QDBusUnixFileDescriptor> 0015 #include <QDebug> 0016 #include <QLocalSocket> 0017 #include <QTimer> 0018 0019 // This is a server for BluetoothChat application from Android samples. 0020 // 0021 // On successful connection, it sends "test-data" message to the client 0022 // and then writes all messages from client to the standard output. 0023 // 0024 // https://developer.android.com/samples/BluetoothChat/index.html 0025 0026 ChatProfile::ChatProfile(QObject *parent) 0027 : BluezQt::Profile(parent) 0028 { 0029 setName(QStringLiteral("BluetoothChatSecure")); 0030 setChannel(0); 0031 } 0032 0033 QDBusObjectPath ChatProfile::objectPath() const 0034 { 0035 return QDBusObjectPath(QStringLiteral("/ChatProfile")); 0036 } 0037 0038 QString ChatProfile::uuid() const 0039 { 0040 return QStringLiteral("fa87c0d0-afac-11de-8a39-0800200c9a66"); 0041 } 0042 0043 void ChatProfile::newConnection(BluezQt::DevicePtr device, const QDBusUnixFileDescriptor &fd, const QVariantMap &properties, const BluezQt::Request<> &request) 0044 { 0045 qDebug() << "Connect" << device->name() << properties; 0046 0047 m_socket = createSocket(fd); 0048 if (!m_socket->isValid()) { 0049 request.cancel(); 0050 return; 0051 } 0052 0053 connect(m_socket.data(), &QLocalSocket::readyRead, this, &ChatProfile::socketReadyRead); 0054 connect(m_socket.data(), &QLocalSocket::disconnected, this, &ChatProfile::socketDisconnected); 0055 QTimer::singleShot(1000, this, SLOT(writeToSocket())); 0056 0057 request.accept(); 0058 } 0059 0060 void ChatProfile::requestDisconnection(BluezQt::DevicePtr device, const BluezQt::Request<> &request) 0061 { 0062 qDebug() << "Disconnect" << device->name(); 0063 0064 request.accept(); 0065 } 0066 0067 void ChatProfile::release() 0068 { 0069 qDebug() << "Release"; 0070 } 0071 0072 void ChatProfile::socketReadyRead() 0073 { 0074 qDebug() << "Read:" << m_socket->socketDescriptor() << m_socket->readAll(); 0075 } 0076 0077 void ChatProfile::socketDisconnected() 0078 { 0079 qDebug() << "Socket disconnected"; 0080 m_socket.clear(); 0081 } 0082 0083 void ChatProfile::writeToSocket() 0084 { 0085 qDebug() << "Writing" << m_socket->socketDescriptor(); 0086 m_socket->write(QByteArrayLiteral("test-data")); 0087 } 0088 0089 int main(int argc, char **argv) 0090 { 0091 QCoreApplication app(argc, argv); 0092 0093 BluezQt::Manager *manager = new BluezQt::Manager(); 0094 BluezQt::InitManagerJob *initJob = manager->init(); 0095 initJob->exec(); 0096 0097 if (initJob->error()) { 0098 qWarning() << "Error initializing manager:" << initJob->errorText(); 0099 return 1; 0100 } 0101 0102 BluezQt::PendingCall *call = manager->registerProfile(new ChatProfile(&app)); 0103 call->waitForFinished(); 0104 0105 if (call->error()) { 0106 qWarning() << "Error registering profile" << call->errorText(); 0107 return 1; 0108 } 0109 0110 qDebug() << "Profile registered"; 0111 0112 return app.exec(); 0113 } 0114 0115 #include "moc_chatprofile.cpp"