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
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
8
9#include <QtCore/qdebug.h>
10
11QT_USE_NAMESPACE
12
13@implementation DarwinBTConnectionMonitor
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];
100 discoveryNotification = nil;
101
102 for (IOBluetoothUserNotification *n in foundConnections)
103 [n unregister];
104
105 [foundConnections release];
106 foundConnections = nil;
107}
108
109@end
#define QT_BT_MAC_AUTORELEASEPOOL
Definition btutility_p.h:78