diff options
Diffstat (limited to 'src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothBroadcastReceiver.java')
-rw-r--r-- | src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothBroadcastReceiver.java | 68 |
1 files changed, 43 insertions, 25 deletions
diff --git a/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothBroadcastReceiver.java b/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothBroadcastReceiver.java index 57d92c03..6b46ec0a 100644 --- a/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothBroadcastReceiver.java +++ b/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothBroadcastReceiver.java @@ -1,32 +1,38 @@ /**************************************************************************** ** -** Copyright (C) 2013 Lauri Laanmets (Proekspert AS) <lauri.laanmets@eesti.ee> -** Copyright (C) 2015 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ +** Copyright (C) 2016 Lauri Laanmets (Proekspert AS) <lauri.laanmets@eesti.ee> +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtBluetooth module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -40,6 +46,7 @@ import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.util.Log; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -52,14 +59,15 @@ public class QtBluetoothBroadcastReceiver extends BroadcastReceiver @SuppressWarnings("WeakerAccess") long qtObject = 0; @SuppressWarnings("WeakerAccess") - static Activity qtactivity = null; + static Context qtContext = null; private static final int TURN_BT_ON = 3330; private static final int TURN_BT_DISCOVERABLE = 3331; + private static final String TAG = "QtBluetoothBroadcastReceiver"; public void onReceive(Context context, Intent intent) { - synchronized (qtactivity) { + synchronized (qtContext) { if (qtObject == 0) return; @@ -69,25 +77,30 @@ public class QtBluetoothBroadcastReceiver extends BroadcastReceiver public void unregisterReceiver() { - synchronized (qtactivity) { + synchronized (qtContext) { qtObject = 0; - qtactivity.unregisterReceiver(this); + qtContext.unregisterReceiver(this); } } public native void jniOnReceive(long qtObject, Context context, Intent intent); - static public void setActivity(Activity activity, Object activityDelegate) + static public void setContext(Context context) { - qtactivity = activity; + qtContext = context; } static public void setDiscoverable() { + if (!(qtContext instanceof android.app.Activity)) { + Log.w(TAG, "Discovery mode cannot be enabled from a service."); + return; + } + Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); try { - qtactivity.startActivityForResult(intent, TURN_BT_ON); + ((Activity)qtContext).startActivityForResult(intent, TURN_BT_ON); } catch (Exception ex) { ex.printStackTrace(); } @@ -95,9 +108,14 @@ public class QtBluetoothBroadcastReceiver extends BroadcastReceiver static public void setConnectable() { + if (!(qtContext instanceof android.app.Activity)) { + Log.w(TAG, "Connectable mode cannot be enabled from a service."); + return; + } + Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); try { - qtactivity.startActivityForResult(intent, TURN_BT_DISCOVERABLE); + ((Activity)qtContext).startActivityForResult(intent, TURN_BT_DISCOVERABLE); } catch (Exception ex) { ex.printStackTrace(); } @@ -136,7 +154,7 @@ public class QtBluetoothBroadcastReceiver extends BroadcastReceiver try { //Bluetooth service name Field f = Context.class.getField("BLUETOOTH_SERVICE"); - String serviceValueString = (String)f.get(qtactivity); + String serviceValueString = (String)f.get(qtContext); Class btProfileClz = Class.forName("android.bluetooth.BluetoothProfile"); @@ -149,7 +167,7 @@ public class QtBluetoothBroadcastReceiver extends BroadcastReceiver int gattServer = f.getInt(null); //get BluetoothManager instance - Object bluetoothManager = qtactivity.getSystemService(serviceValueString); + Object bluetoothManager = qtContext.getSystemService(serviceValueString); Class[] cArg = new Class[1]; cArg[0] = int.class; |