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
QtBluetoothBroadcastReceiver.java
Go to the documentation of this file.
1// Copyright (C) 2016 Lauri Laanmets (Proekspert AS) <lauri.laanmets@eesti.ee>
2// Copyright (C) 2016 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5package org.qtproject.qt.android.bluetooth;
6
7import android.app.Activity;
8import android.bluetooth.BluetoothAdapter;
9import android.bluetooth.BluetoothDevice;
10import android.bluetooth.BluetoothProfile;
11import android.bluetooth.BluetoothManager;
12import android.content.BroadcastReceiver;
13import android.content.Context;
14import android.content.Intent;
15import android.util.Log;
16
17import java.lang.reflect.Method;
18import java.util.HashSet;
19import java.util.List;
20
21class QtBluetoothBroadcastReceiver extends BroadcastReceiver
22{
23 /* Pointer to the Qt object that "owns" the Java object */
24 @SuppressWarnings("WeakerAccess")
25 long qtObject = 0;
26 @SuppressWarnings("WeakerAccess")
27 static Context qtContext = null;
28
29 // These are opaque tokens that could be used to match the completed action
30 private static final int TURN_BT_ENABLED = 3330;
31 private static final int TURN_BT_DISCOVERABLE = 3331;
32 private static final int TURN_BT_DISABLED = 3332;
33
34 // The 'Disable' action identifier is hidden in the public APIs so we define it here
35 static final String ACTION_REQUEST_DISABLE =
36 "android.bluetooth.adapter.action.REQUEST_DISABLE";
37
38 private static final String TAG = "QtBluetoothBroadcastReceiver";
39
40 @Override
41 public void onReceive(Context context, Intent intent)
42 {
43 synchronized (qtContext) {
44 if (qtObject == 0)
45 return;
46
47 jniOnReceive(qtObject, context, intent);
48 }
49 }
50
51 void unregisterReceiver()
52 {
53 synchronized (qtContext) {
54 qtObject = 0;
55 try {
56 qtContext.unregisterReceiver(this);
57 } catch (Exception ex) {
58 Log.d(TAG, "Trying to unregister a BroadcastReceiver which is not yet registered");
59 }
60 }
61 }
62
63 native void jniOnReceive(long qtObject, Context context, Intent intent);
64
65 static void setContext(Context context)
66 {
67 qtContext = context;
68 }
69
70 static boolean setDisabled()
71 {
72 if (!(qtContext instanceof android.app.Activity)) {
73 Log.w(TAG, "Bluetooth cannot be disabled from a service.");
74 return false;
75 }
76 // The 'disable' is hidden in the public API and as such
77 // there are no availability guarantees; may throw an "ActivityNotFoundException"
78 Intent intent = new Intent(ACTION_REQUEST_DISABLE);
79
80 try {
81 ((Activity)qtContext).startActivityForResult(intent, TURN_BT_DISABLED);
82 } catch (Exception ex) {
83 Log.w(TAG, "setDisabled() failed to initiate Bluetooth disablement");
84 ex.printStackTrace();
85 return false;
86 }
87 return true;
88 }
89
90 static boolean setDiscoverable()
91 {
92 if (!(qtContext instanceof android.app.Activity)) {
93 Log.w(TAG, "Discovery mode cannot be enabled from a service.");
94 return false;
95 }
96
97 Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
98 intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
99 try {
100 ((Activity)qtContext).startActivityForResult(intent, TURN_BT_DISCOVERABLE);
101 } catch (Exception ex) {
102 Log.w(TAG, "setDiscoverable() failed to initiate Bluetooth discoverability change");
103 ex.printStackTrace();
104 return false;
105 }
106 return true;
107 }
108
109 static boolean setEnabled()
110 {
111 if (!(qtContext instanceof android.app.Activity)) {
112 Log.w(TAG, "Bluetooth cannot be enabled from a service.");
113 return false;
114 }
115
116 Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
117 try {
118 ((Activity)qtContext).startActivityForResult(intent, TURN_BT_ENABLED);
119 } catch (Exception ex) {
120 Log.w(TAG, "setEnabled() failed to initiate Bluetooth enablement");
121 ex.printStackTrace();
122 return false;
123 }
124 return true;
125 }
126
127 static boolean setPairingMode(String address, boolean isPairing)
128 {
129 BluetoothManager manager =
130 (BluetoothManager)qtContext.getSystemService(Context.BLUETOOTH_SERVICE);
131 if (manager == null)
132 return false;
133
134 BluetoothAdapter adapter = manager.getAdapter();
135 if (adapter == null)
136 return false;
137
138 // Uses reflection as the removeBond() is not part of public API
139 try {
140 BluetoothDevice device = adapter.getRemoteDevice(address);
141 String methodName = "createBond";
142 if (!isPairing)
143 methodName = "removeBond";
144
145 Method m = device.getClass()
146 .getMethod(methodName, (Class[]) null);
147 m.invoke(device, (Object[]) null);
148 } catch (Exception ex) {
149 ex.printStackTrace();
150 return false;
151 }
152
153 return true;
154 }
155
156 /*
157 * Returns a list of remote devices confirmed to be connected.
158 *
159 * This list is not complete as it only detects GATT/BtLE related connections.
160 * Unfortunately there is no API that provides the complete list.
161 *
162 */
163 static String[] getConnectedDevices()
164 {
165 BluetoothManager bluetoothManager =
166 (BluetoothManager) qtContext.getSystemService(Context.BLUETOOTH_SERVICE);
167
168 if (bluetoothManager == null) {
169 Log.w(TAG, "Failed to retrieve connected devices");
170 return new String[0];
171 }
172
173 List<BluetoothDevice> gattConnections =
174 bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
175 List<BluetoothDevice> gattServerConnections =
176 bluetoothManager.getConnectedDevices(BluetoothProfile.GATT_SERVER);
177
178 // Process found remote connections but avoid duplications
179 HashSet<String> set = new HashSet<String>();
180 for (Object gattConnection : gattConnections)
181 set.add(gattConnection.toString());
182
183 for (Object gattServerConnection : gattServerConnections)
184 set.add(gattServerConnection.toString());
185
186 return set.toArray(new String[set.size()]);
187 }
188}
IOBluetoothDevice * device
QPainter Context
static const QString context()
Definition java.cpp:398
@ BluetoothAdapter
@ BluetoothDevice
[vector_of_multirole_objects_0]
Definition main.cpp:188
static QString methodName(const QDBusIntrospection::Method &method)
#define TAG(x)
const GLfloat * m
[1]
GLenum GLuint GLintptr GLsizeiptr size
GLuint GLuint64EXT address
QNetworkAccessManager manager
[0]