Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
btconnectionmonitor.mm
Go to the documentation of this file.
1// Copyright (C) 2022 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
5#include "btutility_p.h"
6
7#include "qbluetoothaddress.h"
8
9#include <QtCore/qdebug.h>
10
12
14{
15 QT_PREPEND_NAMESPACE(DarwinBluetooth::ConnectionMonitor) *monitor;
16 IOBluetoothUserNotification *discoveryNotification;
17 NSMutableArray *foundConnections;
18}
19
20- (id)initWithMonitor:(DarwinBluetooth::ConnectionMonitor *)aMonitor
21{
22 Q_ASSERT_X(aMonitor, "-initWithMonitor:", "invalid monitor (null)");
23
24 if (self = [super init]) {
25 monitor = aMonitor;
26 discoveryNotification = [[IOBluetoothDevice registerForConnectNotifications:self
27 selector:@selector(connectionNotification:withDevice:)] retain];
28 foundConnections = [[NSMutableArray alloc] init];
29 }
30
31 return self;
32}
33
34- (void)dealloc
35{
36 Q_ASSERT_X(!monitor, "-dealloc",
37 "Connection monitor was not stopped, calling -stopMonitoring is required");
38 [super dealloc];
39}
40
41- (void)connectionNotification:(IOBluetoothUserNotification *)aNotification
42 withDevice:(IOBluetoothDevice *)device
43{
44 Q_UNUSED(aNotification);
45
46 typedef IOBluetoothUserNotification Notification;
47
48 if (!device)
49 return;
50
51 if (!monitor) {
52 // Rather surprising: monitor == nullptr means we stopped monitoring.
53 // So apparently this thingie is still alive and keeps receiving
54 // notifications.
55 qCWarning(QT_BT_DARWIN,
56 "Connection notification received in a monitor that was cancelled");
57 return;
58 }
59
61
62 // All Obj-C objects are autoreleased.
63
64 const QBluetoothAddress deviceAddress(DarwinBluetooth::qt_address([device getAddress]));
65 if (deviceAddress.isNull())
66 return;
67
68 if (foundConnections) {
69 Notification *const notification = [device registerForDisconnectNotification:self
70 selector: @selector(connectionClosedNotification:withDevice:)];
71 if (notification)
72 [foundConnections addObject:notification];
73 }
74
75 Q_ASSERT_X(monitor, "-connectionNotification:withDevice:", "invalid monitor (null)");
76 monitor->deviceConnected(deviceAddress);
77}
78
79- (void)connectionClosedNotification:(IOBluetoothUserNotification *)notification
80 withDevice:(IOBluetoothDevice *)device
81{
83
84 [notification unregister];//?
85 [foundConnections removeObject:notification];
86
87 const QBluetoothAddress deviceAddress(DarwinBluetooth::qt_address([device getAddress]));
88 if (deviceAddress.isNull())
89 return;
90
91 Q_ASSERT_X(monitor, "-connectionClosedNotification:withDevice:", "invalid monitor (null)");
92 monitor->deviceDisconnected(deviceAddress);
93}
94
95-(void)stopMonitoring
96{
97 monitor = nullptr;
98 [discoveryNotification unregister];
99 [discoveryNotification release];
101
102 for (IOBluetoothUserNotification *n in foundConnections)
103 [n unregister];
104
105 [foundConnections release];
106 foundConnections = nil;
107}
108
109@end
NSMutableArray * foundConnections
IOBluetoothUserNotification * discoveryNotification
IOBluetoothDevice * device
#define QT_BT_MAC_AUTORELEASEPOOL
Definition btutility_p.h:78
\inmodule QtBluetooth
QString qt_address(NSString *address)
Definition btutility.mm:36
QString self
Definition language.cpp:58
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
#define qCWarning(category,...)
GLenum GLuint id
[7]
GLfloat n
GLuint in
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:48
static QT_BEGIN_NAMESPACE void init(QTextBoundaryFinder::BoundaryType type, QStringView str, QCharAttributes *attributes)
#define Q_UNUSED(x)