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
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
21public class 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 public static final String ACTION_REQUEST_DISABLE =
36 "android.bluetooth.adapter.action.REQUEST_DISABLE";
37
38 private static final String TAG = "QtBluetoothBroadcastReceiver";
39
40 public void onReceive(Context context, Intent intent)
41 {
42 synchronized (qtContext) {
43 if (qtObject == 0)
44 return;
45
46 jniOnReceive(qtObject, context, intent);
47 }
48 }
49
50 public void unregisterReceiver()
51 {
52 synchronized (qtContext) {
53 qtObject = 0;
54 try {
55 qtContext.unregisterReceiver(this);
56 } catch (Exception ex) {
57 Log.d(TAG, "Trying to unregister a BroadcastReceiver which is not yet registered");
58 }
59 }
60 }
61
62 public native void jniOnReceive(long qtObject, Context context, Intent intent);
63
64 static public void setContext(Context context)
65 {
66 qtContext = context;
67 }
68
69 static public boolean setDisabled()
70 {
71 if (!(qtContext instanceof android.app.Activity)) {
72 Log.w(TAG, "Bluetooth cannot be disabled from a service.");
73 return false;
74 }
75 // The 'disable' is hidden in the public API and as such
76 // there are no availability guarantees; may throw an "ActivityNotFoundException"
77 Intent intent = new Intent(ACTION_REQUEST_DISABLE);
78
79 try {
80 ((Activity)qtContext).startActivityForResult(intent, TURN_BT_DISABLED);
81 } catch (Exception ex) {
82 Log.w(TAG, "setDisabled() failed to initiate Bluetooth disablement");
83 ex.printStackTrace();
84 return false;
85 }
86 return true;
87 }
88
89 static public boolean setDiscoverable()
90 {
91 if (!(qtContext instanceof android.app.Activity)) {
92 Log.w(TAG, "Discovery mode cannot be enabled from a service.");
93 return false;
94 }
95
96 Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
97 intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
98 try {
99 ((Activity)qtContext).startActivityForResult(intent, TURN_BT_DISCOVERABLE);
100 } catch (Exception ex) {
101 Log.w(TAG, "setDiscoverable() failed to initiate Bluetooth discoverability change");
102 ex.printStackTrace();
103 return false;
104 }
105 return true;
106 }
107
108 static public boolean setEnabled()
109 {
110 if (!(qtContext instanceof android.app.Activity)) {
111 Log.w(TAG, "Bluetooth cannot be enabled from a service.");
112 return false;
113 }
114
115 Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
116 try {
117 ((Activity)qtContext).startActivityForResult(intent, TURN_BT_ENABLED);
118 } catch (Exception ex) {
119 Log.w(TAG, "setEnabled() failed to initiate Bluetooth enablement");
120 ex.printStackTrace();
121 return false;
122 }
123 return true;
124 }
125
126 static public boolean setPairingMode(String address, boolean isPairing)
127 {
128 BluetoothManager manager =
129 (BluetoothManager)qtContext.getSystemService(Context.BLUETOOTH_SERVICE);
130 if (manager == null)
131 return false;
132
133 BluetoothAdapter adapter = manager.getAdapter();
134 if (adapter == null)
135 return false;
136
137 // Uses reflection as the removeBond() is not part of public API
138 try {
139 BluetoothDevice device = adapter.getRemoteDevice(address);
140 String methodName = "createBond";
141 if (!isPairing)
142 methodName = "removeBond";
143
144 Method m = device.getClass()
145 .getMethod(methodName, (Class[]) null);
146 m.invoke(device, (Object[]) null);
147 } catch (Exception ex) {
148 ex.printStackTrace();
149 return false;
150 }
151
152 return true;
153 }
154
155 /*
156 * Returns a list of remote devices confirmed to be connected.
157 *
158 * This list is not complete as it only detects GATT/BtLE related connections.
159 * Unfortunately there is no API that provides the complete list.
160 *
161 */
162 static public String[] getConnectedDevices()
163 {
164 BluetoothManager bluetoothManager =
165 (BluetoothManager) qtContext.getSystemService(Context.BLUETOOTH_SERVICE);
166
167 if (bluetoothManager == null) {
168 Log.w(TAG, "Failed to retrieve connected devices");
169 return new String[0];
170 }
171
172 List<BluetoothDevice> gattConnections =
173 bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
174 List<BluetoothDevice> gattServerConnections =
175 bluetoothManager.getConnectedDevices(BluetoothProfile.GATT_SERVER);
176
177 // Process found remote connections but avoid duplications
178 HashSet<String> set = new HashSet<String>();
179 for (Object gattConnection : gattConnections)
180 set.add(gattConnection.toString());
181
182 for (Object gattServerConnection : gattServerConnections)
183 set.add(gattServerConnection.toString());
184
185 return set.toArray(new String[set.size()]);
186 }
187}
IOBluetoothDevice * device
Definition main.cpp:8
native void jniOnReceive(long qtObject, Context context, Intent intent)
static void * context
static QString methodName(const QDBusIntrospection::Method &method)
#define TAG(x)
const GLfloat * m
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint GLuint64EXT address
static void add(QPainterPath &path, const QWingedEdge &list, int edge, QPathEdge::Traversal traversal)
QNetworkAccessManager manager