File: chatprofile.cpp

package info (click to toggle)
bluez-qt 5.116.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,932 kB
  • sloc: cpp: 16,676; xml: 554; ansic: 337; sh: 13; makefile: 8
file content (115 lines) | stat: -rw-r--r-- 2,963 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * SPDX-FileCopyrightText: 2015 David Rosca <nowrep@gmail.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
 */

#include "chatprofile.h"
#include "initmanagerjob.h"
#include "manager.h"
#include "pendingcall.h"

#include <QCoreApplication>
#include <QDBusObjectPath>
#include <QDBusUnixFileDescriptor>
#include <QDebug>
#include <QLocalSocket>
#include <QTimer>

// This is a server for BluetoothChat application from Android samples.
//
// On successful connection, it sends "test-data" message to the client
// and then writes all messages from client to the standard output.
//
// https://developer.android.com/samples/BluetoothChat/index.html

ChatProfile::ChatProfile(QObject *parent)
    : BluezQt::Profile(parent)
{
    setName(QStringLiteral("BluetoothChatSecure"));
    setChannel(0);
}

QDBusObjectPath ChatProfile::objectPath() const
{
    return QDBusObjectPath(QStringLiteral("/ChatProfile"));
}

QString ChatProfile::uuid() const
{
    return QStringLiteral("fa87c0d0-afac-11de-8a39-0800200c9a66");
}

void ChatProfile::newConnection(BluezQt::DevicePtr device, const QDBusUnixFileDescriptor &fd, const QVariantMap &properties, const BluezQt::Request<> &request)
{
    qDebug() << "Connect" << device->name() << properties;

    m_socket = createSocket(fd);
    if (!m_socket->isValid()) {
        request.cancel();
        return;
    }

    connect(m_socket.data(), &QLocalSocket::readyRead, this, &ChatProfile::socketReadyRead);
    connect(m_socket.data(), &QLocalSocket::disconnected, this, &ChatProfile::socketDisconnected);
    QTimer::singleShot(1000, this, SLOT(writeToSocket()));

    request.accept();
}

void ChatProfile::requestDisconnection(BluezQt::DevicePtr device, const BluezQt::Request<> &request)
{
    qDebug() << "Disconnect" << device->name();

    request.accept();
}

void ChatProfile::release()
{
    qDebug() << "Release";
}

void ChatProfile::socketReadyRead()
{
    qDebug() << "Read:" << m_socket->socketDescriptor() << m_socket->readAll();
}

void ChatProfile::socketDisconnected()
{
    qDebug() << "Socket disconnected";
    m_socket.clear();
}

void ChatProfile::writeToSocket()
{
    qDebug() << "Writing" << m_socket->socketDescriptor();
    m_socket->write(QByteArrayLiteral("test-data"));
}

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);

    BluezQt::Manager *manager = new BluezQt::Manager();
    BluezQt::InitManagerJob *initJob = manager->init();
    initJob->exec();

    if (initJob->error()) {
        qWarning() << "Error initializing manager:" << initJob->errorText();
        return 1;
    }

    BluezQt::PendingCall *call = manager->registerProfile(new ChatProfile(&app));
    call->waitForFinished();

    if (call->error()) {
        qWarning() << "Error registering profile" << call->errorText();
        return 1;
    }

    qDebug() << "Profile registered";

    return app.exec();
}

#include "moc_chatprofile.cpp"