Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qbluetoothsocket.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QBLUETOOTHSOCKET_H
5#define QBLUETOOTHSOCKET_H
6
7#include <QtBluetooth/qtbluetoothglobal.h>
8
9#include <QtBluetooth/qbluetooth.h>
10#include <QtBluetooth/qbluetoothaddress.h>
11#include <QtBluetooth/qbluetoothuuid.h>
12#include <QtBluetooth/qbluetoothserviceinfo.h>
13
14#include <QtCore/qiodevice.h>
15
17
18
19class QBluetoothSocketBasePrivate;
20
21class Q_BLUETOOTH_EXPORT QBluetoothSocket : public QIODevice
22{
23 Q_OBJECT
24
25 Q_DECLARE_PRIVATE(QBluetoothSocketBase)
26
27 friend class QBluetoothServer;
28 friend class QBluetoothServerPrivate;
29 friend class QBluetoothSocketPrivateDarwin;
30 friend class QBluetoothSocketPrivateAndroid;
31 friend class QBluetoothSocketPrivateBluez;
32 friend class QBluetoothSocketPrivateBluezDBus;
33 friend class QBluetoothSocketPrivateDummy;
34 friend class QBluetoothSocketPrivateWin;
35 friend class QBluetoothSocketPrivateWinRT;
36 friend class QBluetoothSocketPrivateOhos;
37
38public:
39
40 enum class SocketState {
41 UnconnectedState,
42 ServiceLookupState,
43 ConnectingState,
44 ConnectedState,
45 BoundState,
46 ClosingState,
47 ListeningState
48 };
49 Q_ENUM(SocketState)
50
51 enum class SocketError {
52 NoSocketError,
53 UnknownSocketError,
54 RemoteHostClosedError,
55 HostNotFoundError,
56 ServiceNotFoundError,
57 NetworkError,
58 UnsupportedProtocolError,
59 OperationError,
60 MissingPermissionsError
61 };
62 Q_ENUM(SocketError)
63
64 explicit QBluetoothSocket(QBluetoothServiceInfo::Protocol socketType, QObject *parent = nullptr); // create socket of type socketType
65 explicit QBluetoothSocket(QObject *parent = nullptr); // create a blank socket
66 virtual ~QBluetoothSocket();
67
68 void abort();
69
70 void close() override;
71
72 bool isSequential() const override;
73
74 qint64 bytesAvailable() const override;
75 qint64 bytesToWrite() const override;
76
77 bool canReadLine() const override;
78
79 void connectToService(const QBluetoothServiceInfo &service, OpenMode openMode = ReadWrite);
80 void connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid, OpenMode openMode = ReadWrite);
81 void connectToService(const QBluetoothAddress &address, quint16 port, OpenMode openMode = ReadWrite);
82 inline void connectToService(const QBluetoothAddress &address, QBluetoothUuid::ServiceClassUuid uuid,
83 OpenMode mode = ReadWrite)
84 {
85 connectToService(address, QBluetoothUuid(uuid), mode);
86 }
87 void disconnectFromService();
88
89 //bool flush();
90 //bool isValid() const;
91
92 QString localName() const;
93 QBluetoothAddress localAddress() const;
94 quint16 localPort() const;
95
96 QString peerName() const;
97 QBluetoothAddress peerAddress() const;
98 quint16 peerPort() const;
99 //QBluetoothServiceInfo peerService() const;
100
101 //qint64 readBufferSize() const;
102 //void setReadBufferSize(qint64 size);
103
104 bool setSocketDescriptor(int socketDescriptor, QBluetoothServiceInfo::Protocol socketType,
105 SocketState socketState = SocketState::ConnectedState,
106 OpenMode openMode = ReadWrite);
107 int socketDescriptor() const;
108
109 QBluetoothServiceInfo::Protocol socketType() const;
110 SocketState state() const;
111 SocketError error() const;
112 QString errorString() const;
113
114 //bool waitForConnected(int msecs = 30000);
115 //bool waitForDisconnected(int msecs = 30000);
116 //virtual bool waitForReadyRead(int msecs = 30000);
117
118 void setPreferredSecurityFlags(QBluetooth::SecurityFlags flags);
119 QBluetooth::SecurityFlags preferredSecurityFlags() const;
120
121Q_SIGNALS:
122 void connected();
123 void disconnected();
124 void errorOccurred(QBluetoothSocket::SocketError error);
125 void stateChanged(QBluetoothSocket::SocketState state);
126
127protected:
128 qint64 readData(char *data, qint64 maxSize) override;
129 qint64 writeData(const char *data, qint64 maxSize) override;
130
131 void setSocketState(SocketState state);
132 void setSocketError(SocketError error);
133
134 void doDeviceDiscovery(const QBluetoothServiceInfo &service, OpenMode openMode);
135
136private Q_SLOTS:
137 void serviceDiscovered(const QBluetoothServiceInfo &service);
138 void discoveryFinished();
139
140
141protected:
142#if QT_CONFIG(bluez)
143 //evil hack to enable QBluetoothServer on Bluez to set the desired d_ptr
144 explicit QBluetoothSocket(QBluetoothSocketBasePrivate *d,
145 QBluetoothServiceInfo::Protocol socketType,
146 QObject *parent = nullptr);
147#endif
148
149 QBluetoothSocketBasePrivate *d_ptr;
150
151private:
152 friend class QLowEnergyControllerPrivateBluez;
153};
154
155
156QT_END_NAMESPACE
157
158#endif
QBluetoothServiceInfo::Protocol serverType
QBluetooth::SecurityFlags securityFlags
QBluetoothSocket * socket
\inmodule QtBluetooth
\inmodule QtBluetooth
Combined button and popup list for selecting options.