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#include "qbluetoothaddress.h"
5
6#ifndef QT_NO_DEBUG_STREAM
7#include <QDebug>
8#endif
9
10QT_BEGIN_NAMESPACE
11
12QT_IMPL_METATYPE_EXTERN(QBluetoothAddress)
13
14/*!
15 \class QBluetoothAddress
16 \inmodule QtBluetooth
17 \brief The QBluetoothAddress class assigns an address to the Bluetooth
18 device.
19 \since 5.2
20
21 This class holds a Bluetooth address in a platform- and protocol-independent manner.
22*/
23
24void registerQBluetoothAddress()
25{
26 qRegisterMetaType<QBluetoothAddress>();
27}
28Q_CONSTRUCTOR_FUNCTION(registerQBluetoothAddress)
29
30/*!
31 \fn QBluetoothAddress::QBluetoothAddress()
32
33 Constructs a null Bluetooth Address.
34*/
35
36/*!
37 \fn QBluetoothAddress::QBluetoothAddress(quint64 address)
38
39 Constructs a new Bluetooth address and assigns \a address to it.
40*/
41
42/*!
43 Constructs a new Bluetooth address and assigns \a address to it.
44
45 The format of \a address can be either XX:XX:XX:XX:XX:XX or XXXXXXXXXXXX,
46 where X is a hexadecimal digit. Case is not important.
47*/
48QBluetoothAddress::QBluetoothAddress(const QString &address)
49{
50 QString a = address;
51
52 if (a.size() == 17)
53 a.remove(c: QLatin1Char(':'));
54
55 if (a.size() == 12) {
56 bool ok;
57 m_address = a.toULongLong(ok: &ok, base: 16);
58 if (!ok)
59 clear();
60 } else {
61 m_address = 0;
62 }
63}
64
65/*!
66 \fn QBluetoothAddress::qHash(QBluetoothAddress key, size_t seed)
67 \since 6.6
68
69 Returns the hash value for the \a key, using \a seed to seed the
70 calculation.
71*/
72
73/*!
74 \fn void QBluetoothAddress::clear()
75
76 Sets the Bluetooth address to 00:00:00:00:00:00.
77*/
78
79/*!
80 \fn bool QBluetoothAddress::isNull() const
81
82 Returns true if the Bluetooth address is null, otherwise returns false.
83*/
84
85/*!
86 \fn quint64 QBluetoothAddress::toUInt64() const
87
88 Returns this Bluetooth address as a quint64.
89*/
90
91/*!
92 Returns the Bluetooth address as a string of the form, XX:XX:XX:XX:XX:XX.
93*/
94QString QBluetoothAddress::toString() const
95{
96 QString s(QStringLiteral("%1:%2:%3:%4:%5:%6"));
97
98 for (int i = 5; i >= 0; --i) {
99 const quint8 a = (m_address >> (i*8)) & 0xff;
100 s = s.arg(a, fieldWidth: 2, base: 16, fillChar: QLatin1Char('0'));
101 }
102
103 return s.toUpper();
104}
105
106/*!
107 \fn bool QBluetoothAddress::operator<(const QBluetoothAddress &a,
108 const QBluetoothAddress &b)
109 \brief Returns true if the Bluetooth address \a a is less than \a b, otherwise
110 returns false.
111*/
112
113/*!
114 \fn bool QBluetoothAddress::operator==(const QBluetoothAddress &a,
115 const QBluetoothAddress &b)
116 \brief Returns \c true if the two Bluetooth addresses \a a and \a b are equal,
117 otherwise returns \c false.
118*/
119
120/*!
121 \fn bool QBluetoothAddress::operator!=(const QBluetoothAddress &a,
122 const QBluetoothAddress &b)
123 \brief Returns \c true if the two Bluetooth addresses \a a and \a b are not equal,
124 otherwise returns \c false.
125*/
126
127#ifndef QT_NO_DEBUG_STREAM
128QDebug QBluetoothAddress::streamingOperator(QDebug debug, const QBluetoothAddress &address)
129{
130 debug << address.toString();
131 return debug;
132}
133#endif
134
135QT_END_NAMESPACE
136

source code of qtconnectivity/src/bluetooth/qbluetoothaddress.cpp