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
qbluetoothserviceinfo_android.cpp
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
5#include "android/androidutils_p.h"
12
13#include <QtCore/QLoggingCategory>
14
16
17Q_DECLARE_LOGGING_CATEGORY(QT_BT_ANDROID)
18
19extern QHash<QBluetoothServerPrivate*, int> __fakeServerPorts;
20
21QBluetoothServiceInfoPrivate::QBluetoothServiceInfoPrivate()
22: registered(false)
23{
24}
25
29
31{
32 return registered;
33}
34
36{
37 if (!registered)
38 return false;
39
40 QBluetoothServerPrivate *sPriv = __fakeServerPorts.key(serverChannel());
41 if (!sPriv) {
42 //QBluetoothServer::close() was called without prior call to unregisterService().
43 //Now it is unregistered anyway.
44 registered = false;
45 return true;
46 }
47
48 bool result = sPriv->deactivateActiveListening();
49 if (!result)
50 return false;
51
52 registered = false;
53 return true;
54}
55
56bool QBluetoothServiceInfoPrivate::registerService(const QBluetoothAddress& localAdapter)
57{
58 if (!ensureAndroidPermission(QBluetoothPermission::Access)) {
59 qCWarning(QT_BT_ANDROID) << "Serviceinfo registerService() failed due to"
60 "missing permissions";
61 return false;
62 }
63
64 const QList<QBluetoothHostInfo> localDevices = QBluetoothLocalDevice::allDevices();
65 if (localDevices.isEmpty())
66 return false; //no Bluetooth device
67
68 if (!localAdapter.isNull()) {
69 bool found = false;
70 for (const QBluetoothHostInfo &hostInfo : localDevices) {
71 if (hostInfo.address() == localAdapter) {
72 found = true;
73 break;
74 }
75 }
76
77 if (!found) {
78 qCWarning(QT_BT_ANDROID) << localAdapter.toString() << "is not a valid local Bt adapter";
79 return false;
80 }
81 }
82
83 //already registered on local adapter => nothing to do
84 if (registered)
85 return false;
86
87 if (protocolDescriptor(QBluetoothUuid::ProtocolUuid::Rfcomm).isEmpty()) {
88 qCWarning(QT_BT_ANDROID) << Q_FUNC_INFO << "Only RFCOMM services can be registered on Android";
89 return false;
90 }
91
92 QBluetoothServerPrivate *sPriv = __fakeServerPorts.key(serverChannel());
93 if (!sPriv)
94 return false;
95
96 //tell the server what service name and uuid our listener should have
97 //and start the real listener
98 bool result = sPriv->initiateActiveListening(
99 attributes.value(QBluetoothServiceInfo::ServiceId).value<QBluetoothUuid>(),
100 attributes.value(QBluetoothServiceInfo::ServiceName).toString());
101 if (!result) {
102 return false;
103 }
104
105
106 registered = true;
107 return true;
108}
109
110QT_END_NAMESPACE
bool registerService(const QBluetoothAddress &localAdapter=QBluetoothAddress())