Changeset ed39269 in subsurface


Ignore:
Timestamp:
Jun 1, 2017, 4:53:29 PM (6 months ago)
Author:
Dirk Hohndel <dirk@…>
Branches:
master
Children:
3b4091a
Parents:
ec34ebf
git-author:
Jan Mulder <jlmulder@…> (06/01/17 11:07:19)
git-committer:
Dirk Hohndel <dirk@…> (06/01/17 16:53:29)
Message:

Android BT: pull the pairing data from a device

As Qt is not able to pull the pairing data from a device, a lengthy
discovery process is needed to see what devices are paired. On
https://forum.qt.io/topic/46075/solved-bluetooth-list-paired-devices
user s.frings74 does, however, present a solution to this using JNI.
Currently, this code is taken "as is".

Currently, only for Android (so not mobile-on-desktop, or even desktop).
And only generating logging data in the logcat.

Signed-off-by: Jan Mulder <jlmulder@…>

Location:
mobile-widgets
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • mobile-widgets/qmlmanager.cpp

    rf5adbe5 red39269  
    1212#include <QElapsedTimer>
    1313#include <QTimer>
     14#if defined(Q_OS_ANDROID)
     15#include <QAndroidJniObject>
     16#include <QAndroidJniEnvironment>
     17#endif
    1418
    1519#include "qt-models/divelistmodel.h"
     
    9397        if (localBtDevice.isValid()) {
    9498                localBtDevice.powerOn();
     99                QStringList pairedBT = getBluetoothDevices();
     100
     101                for (int i = 0; i < pairedBT.length(); i++) {
     102                        qDebug() << "Paired = " << pairedBT[i];
     103                }
     104
    95105                QString localDeviceName = "localDevice " + localBtDevice.name() + " is valid, starting discovery";
    96106                appendTextToLog(localDeviceName.toUtf8().data());
     
    15981608        return productList[vendor];
    15991609}
     1610
     1611//
     1612// As Qt is not able to pull the pairing data from a device, a lengthy
     1613// discovery process is needed to see what devices are paired. On
     1614// https://forum.qt.io/topic/46075/solved-bluetooth-list-paired-devices
     1615// user s.frings74 does, however, present a solution to this using JNI.
     1616// Currently, this code is taken "as is".
     1617//
     1618QStringList QMLManager::getBluetoothDevices()
     1619{
     1620        QStringList result;
     1621#if defined(Q_OS_ANDROID)
     1622        QString fmt("%1 %2");
     1623        // Query via Android Java API.
     1624
     1625        // returns a BluetoothAdapter
     1626        QAndroidJniObject adapter=QAndroidJniObject::callStaticObjectMethod("android/bluetooth/BluetoothAdapter","getDefaultAdapter","()Landroid/bluetooth/BluetoothAdapter;");
     1627        if (checkException("BluetoothAdapter.getDefaultAdapter()", &adapter)) {
     1628                return result;
     1629        }
     1630        // returns a Set<BluetoothDevice>
     1631        QAndroidJniObject pairedDevicesSet=adapter.callObjectMethod("getBondedDevices","()Ljava/util/Set;");
     1632        if (checkException("BluetoothAdapter.getBondedDevices()", &pairedDevicesSet)) {
     1633                return result;
     1634        }
     1635        jint size=pairedDevicesSet.callMethod<jint>("size");
     1636        checkException("Set<BluetoothDevice>.size()", &pairedDevicesSet);
     1637        if (size > 0) {
     1638                // returns an Iterator<BluetoothDevice>
     1639                QAndroidJniObject iterator=pairedDevicesSet.callObjectMethod("iterator","()Ljava/util/Iterator;");
     1640                if (checkException("Set<BluetoothDevice>.iterator()", &iterator)) {
     1641                        return result;
     1642                }
     1643                for (int i = 0; i < size; i++) {
     1644                        // returns a BluetoothDevice
     1645                        QAndroidJniObject dev=iterator.callObjectMethod("next","()Ljava/lang/Object;");
     1646                        if (checkException("Iterator<BluetoothDevice>.next()", &dev)) {
     1647                                continue;
     1648                }
     1649                QString address=dev.callObjectMethod("getAddress","()Ljava/lang/String;").toString(); // returns a String
     1650                QString name=dev.callObjectMethod("getName", "()Ljava/lang/String;").toString(); // returns a String
     1651                result.append(fmt.arg(address).arg(name));
     1652        }
     1653}
     1654#endif
     1655        return result;
     1656}
     1657
     1658#if defined(Q_OS_ANDROID)
     1659bool QMLManager::checkException(const char* method, const QAndroidJniObject *obj)
     1660{
     1661        static QAndroidJniEnvironment env;
     1662        bool result = false;
     1663
     1664        if (env->ExceptionCheck()) {
     1665                qCritical("Exception in %s", method);
     1666                env->ExceptionDescribe();
     1667                env->ExceptionClear();
     1668                result=true;
     1669        }
     1670        if (!(obj == NULL || obj->isValid())) {
     1671                qCritical("Invalid object returned by %s", method);
     1672        result=true;
     1673        }
     1674        return result;
     1675}
     1676#endif
  • mobile-widgets/qmlmanager.h

    rf5adbe5 red39269  
    1212#include <QBluetoothDeviceDiscoveryAgent>
    1313#include <QBluetoothUuid>
     14#endif
     15#if defined(Q_OS_ANDROID)
     16#include <QAndroidJniObject>
    1417#endif
    1518
     
    126129        void btDeviceDiscovered(const QBluetoothDeviceInfo &device);
    127130#endif
     131        QStringList getBluetoothDevices();
    128132
    129133public slots:
     
    208212        bool currentGitLocalOnly;
    209213        bool m_showPin;
     214
     215#if defined(Q_OS_ANDROID)
     216        bool checkException(const char* method, const QAndroidJniObject* obj);
     217#endif
     218
    210219#if defined(BT_SUPPORT)
    211220        QBluetoothLocalDevice localBtDevice;
Note: See TracChangeset for help on using the changeset viewer.