1/***************************************************************************
2**
3** Copyright (C) 2013 BlackBerry Limited. All rights reserved.
4** Copyright (C) 2017 The Qt Company Ltd.
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtBluetooth module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** BSD License Usage
19** Alternatively, you may use this file under the terms of the BSD license
20** as follows:
21**
22** "Redistribution and use in source and binary forms, with or without
23** modification, are permitted provided that the following conditions are
24** met:
25** * Redistributions of source code must retain the above copyright
26** notice, this list of conditions and the following disclaimer.
27** * Redistributions in binary form must reproduce the above copyright
28** notice, this list of conditions and the following disclaimer in
29** the documentation and/or other materials provided with the
30** distribution.
31** * Neither the name of The Qt Company Ltd nor the names of its
32** contributors may be used to endorse or promote products derived
33** from this software without specific prior written permission.
34**
35**
36** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
39** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
40** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
43** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
44** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
45** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
46** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
47**
48** $QT_END_LICENSE$
49**
50****************************************************************************/
51
52#include "characteristicinfo.h"
53#include "qbluetoothuuid.h"
54#include <QByteArray>
55
56CharacteristicInfo::CharacteristicInfo(const QLowEnergyCharacteristic &characteristic):
57 m_characteristic(characteristic)
58{
59}
60
61void CharacteristicInfo::setCharacteristic(const QLowEnergyCharacteristic &characteristic)
62{
63 m_characteristic = characteristic;
64 emit characteristicChanged();
65}
66
67QString CharacteristicInfo::getName() const
68{
69 //! [les-get-descriptors]
70 QString name = m_characteristic.name();
71 if (!name.isEmpty())
72 return name;
73
74 // find descriptor with CharacteristicUserDescription
75 const QList<QLowEnergyDescriptor> descriptors = m_characteristic.descriptors();
76 for (const QLowEnergyDescriptor &descriptor : descriptors) {
77 if (descriptor.type() == QBluetoothUuid::CharacteristicUserDescription) {
78 name = descriptor.value();
79 break;
80 }
81 }
82 //! [les-get-descriptors]
83
84 if (name.isEmpty())
85 name = "Unknown";
86
87 return name;
88}
89
90QString CharacteristicInfo::getUuid() const
91{
92 const QBluetoothUuid uuid = m_characteristic.uuid();
93 bool success = false;
94 quint16 result16 = uuid.toUInt16(ok: &success);
95 if (success)
96 return QStringLiteral("0x") + QString::number(result16, base: 16);
97
98 quint32 result32 = uuid.toUInt32(ok: &success);
99 if (success)
100 return QStringLiteral("0x") + QString::number(result32, base: 16);
101
102 return uuid.toString().remove(c: QLatin1Char('{')).remove(c: QLatin1Char('}'));
103}
104
105QString CharacteristicInfo::getValue() const
106{
107 // Show raw string first and hex value below
108 QByteArray a = m_characteristic.value();
109 QString result;
110 if (a.isEmpty()) {
111 result = QStringLiteral("<none>");
112 return result;
113 }
114
115 result = a;
116 result += QLatin1Char('\n');
117 result += a.toHex();
118
119 return result;
120}
121
122QString CharacteristicInfo::getHandle() const
123{
124 return QStringLiteral("0x") + QString::number(m_characteristic.handle(), base: 16);
125}
126
127QString CharacteristicInfo::getPermission() const
128{
129 QString properties = "( ";
130 uint permission = m_characteristic.properties();
131 if (permission & QLowEnergyCharacteristic::Read)
132 properties += QStringLiteral(" Read");
133 if (permission & QLowEnergyCharacteristic::Write)
134 properties += QStringLiteral(" Write");
135 if (permission & QLowEnergyCharacteristic::Notify)
136 properties += QStringLiteral(" Notify");
137 if (permission & QLowEnergyCharacteristic::Indicate)
138 properties += QStringLiteral(" Indicate");
139 if (permission & QLowEnergyCharacteristic::ExtendedProperty)
140 properties += QStringLiteral(" ExtendedProperty");
141 if (permission & QLowEnergyCharacteristic::Broadcasting)
142 properties += QStringLiteral(" Broadcast");
143 if (permission & QLowEnergyCharacteristic::WriteNoResponse)
144 properties += QStringLiteral(" WriteNoResp");
145 if (permission & QLowEnergyCharacteristic::WriteSigned)
146 properties += QStringLiteral(" WriteSigned");
147 properties += " )";
148 return properties;
149}
150
151QLowEnergyCharacteristic CharacteristicInfo::getCharacteristic() const
152{
153 return m_characteristic;
154}
155

source code of qtconnectivity/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp