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
qdarwinpermissionplugin_bluetooth.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
6#include <deque>
7
8#include <CoreBluetooth/CoreBluetooth.h>
9
10@interface QDarwinBluetoothPermissionHandler () <CBCentralManagerDelegate>
11@property (nonatomic, retain) CBCentralManager *manager;
12@end
13
14@implementation QDarwinBluetoothPermissionHandler {
15 std::deque<PermissionCallback> m_callbacks;
16}
17
18- (instancetype)init
19{
20 if ((self = [super init]))
21 self.manager = nil;
22
23 return self;
24}
25
26- (Qt::PermissionStatus)checkPermission:(QPermission)permission
27{
28 Q_UNUSED(permission);
29 return [self currentStatus];
30}
31
32- (Qt::PermissionStatus)currentStatus
33{
34 auto status = CBCentralManager.authorization;
35 switch (status) {
36 case CBManagerAuthorizationNotDetermined:
37 return Qt::PermissionStatus::Undetermined;
38 case CBManagerAuthorizationRestricted:
39 case CBManagerAuthorizationDenied:
40 return Qt::PermissionStatus::Denied;
41 case CBManagerAuthorizationAllowedAlways:
42 return Qt::PermissionStatus::Granted;
43 }
44
45 qCWarning(lcPermissions) << "Unknown permission status" << status << "detected in" << self;
46 return Qt::PermissionStatus::Denied;
47}
48
49- (void)requestPermission:(QPermission)permission withCallback:(PermissionCallback)callback
50{
51 m_callbacks.push_back(callback);
52 if (!self.manager) {
53 self.manager = [[[CBCentralManager alloc]
54 initWithDelegate:self queue:dispatch_get_main_queue()] autorelease];
55 }
56}
57
58- (void)centralManagerDidUpdateState:(CBCentralManager *)manager
59{
60 Q_ASSERT(manager == self.manager);
61 Q_ASSERT(!m_callbacks.empty());
62
63 auto status = [self currentStatus];
64
65 for (auto callback : m_callbacks)
66 callback(status);
67
68 m_callbacks = {};
69 self.manager.delegate = nil;
70 self.manager = nil;
71}
72
73- (QStringList)usageDescriptionsFor:(QPermission)permission
74{
75 Q_UNUSED(permission);
76#ifdef Q_OS_MACOS
77 if (QOperatingSystemVersion::current() > QOperatingSystemVersion::MacOSBigSur)
78#endif
79 {
80 return { "NSBluetoothAlwaysUsageDescription" };
81 }
82
83 return {};
84}
85@end
86
87#include "moc_qdarwinpermissionplugin_p_p.cpp"
QList< QString > QStringList
Constructs a string list that contains the given string, str.