Changeset dbd1d1f4 in subsurface


Ignore:
Timestamp:
Jun 2, 2017, 11:31:00 AM (4 months ago)
Author:
Dirk Hohndel <dirk@…>
Branches:
master
Children:
78ee3f4
Parents:
3b4091a
git-author:
Jan Mulder <jlmulder@…> (06/02/17 05:06:11)
git-committer:
Dirk Hohndel <dirk@…> (06/02/17 11:31:00)
Message:

Android BT: generalize pairing data

This adds a list of paired BT devices to the QMLManager class. In addition,
a very simple implementation is made of getting the paired BT devices on
Linux, so that we can test further processing of selecting the proper
devices, in a mobile-on-desktop situation.

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

Location:
mobile-widgets
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • mobile-widgets/qmlmanager.cpp

    r3b4091a rdbd1d1f4  
    9797        if (localBtDevice.isValid() &&
    9898            localBtDevice.hostMode() == QBluetoothLocalDevice::HostConnectable) {
    99                 QStringList pairedBT = getBluetoothDevices();
    100 
    101                 for (int i = 0; i < pairedBT.length(); i++) {
    102                         qDebug() << "Paired = " << pairedBT[i];
    103                 }
    104 
     99                btPairedDevices.clear();
    105100                QString localDeviceName = "localDevice " + localBtDevice.name() + " is valid, starting discovery";
    106101                appendTextToLog(localDeviceName.toUtf8().data());
     102#if defined(Q_OS_LINUX)
    107103                discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
    108104                connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &QMLManager::btDeviceDiscovered);
    109105                discoveryAgent->start();
     106#endif
     107#if defined(Q_OS_ANDROID)
     108                getBluetoothDevices();
     109#endif
     110                for (int i = 0; i < btPairedDevices.length(); i++) {
     111                        qDebug() << "Paired =" << btPairedDevices[i].name << btPairedDevices[i].address.toString();
     112                }
     113#if defined(Q_OS_LINUX)
     114                discoveryAgent->stop();
     115#endif
    110116        } else {
    111117                appendTextToLog("localBtDevice isn't valid");
     
    220226void QMLManager::btDeviceDiscovered(const QBluetoothDeviceInfo &device)
    221227{
     228        btPairedDevice this_d;
     229        this_d.address = device.address();
     230        this_d.name = device.name();
     231        btPairedDevices.append(this_d);
     232
    222233        QString newDevice = device.name();
     234
    223235        // all the HW OSTC BT computers show up as "OSTC" + some other text, depending on model
    224236        if (newDevice.startsWith("OSTC"))
     
    16091621}
    16101622
    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
     1623// Android: As Qt is not able to pull the pairing data from a device, i
     1624// a lengthy discovery process is needed to see what devices are paired. On
    16141625// https://forum.qt.io/topic/46075/solved-bluetooth-list-paired-devices
    16151626// user s.frings74 does, however, present a solution to this using JNI.
    16161627// Currently, this code is taken "as is".
    1617 //
    1618 QStringList QMLManager::getBluetoothDevices()
    1619 {
    1620         QStringList result;
    1621 #if defined(Q_OS_ANDROID)
    1622         QString fmt("%1 %2");
     1628
     1629void QMLManager::getBluetoothDevices()
     1630{
     1631#if defined(Q_OS_ANDROID) && defined(BT_SUPPORT)
     1632        struct QMLManager::btPairedDevice result;
    16231633        // Query via Android Java API.
    16241634
     
    16261636        QAndroidJniObject adapter=QAndroidJniObject::callStaticObjectMethod("android/bluetooth/BluetoothAdapter","getDefaultAdapter","()Landroid/bluetooth/BluetoothAdapter;");
    16271637        if (checkException("BluetoothAdapter.getDefaultAdapter()", &adapter)) {
    1628                 return result;
     1638                return;
    16291639        }
    16301640        // returns a Set<BluetoothDevice>
    16311641        QAndroidJniObject pairedDevicesSet=adapter.callObjectMethod("getBondedDevices","()Ljava/util/Set;");
    16321642        if (checkException("BluetoothAdapter.getBondedDevices()", &pairedDevicesSet)) {
    1633                 return result;
     1643                return;
    16341644        }
    16351645        jint size=pairedDevicesSet.callMethod<jint>("size");
     
    16391649                QAndroidJniObject iterator=pairedDevicesSet.callObjectMethod("iterator","()Ljava/util/Iterator;");
    16401650                if (checkException("Set<BluetoothDevice>.iterator()", &iterator)) {
    1641                         return result;
     1651                        return;
    16421652                }
    16431653                for (int i = 0; i < size; i++) {
     
    16471657                                continue;
    16481658                }
    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));
     1659
     1660                result.address = QBluetoothAddress(dev.callObjectMethod("getAddress","()Ljava/lang/String;").toString());
     1661                result.name = dev.callObjectMethod("getName", "()Ljava/lang/String;").toString();
     1662
     1663                btPairedDevices.append(result);
    16521664        }
    16531665}
    16541666#endif
    1655         return result;
    16561667}
    16571668
  • mobile-widgets/qmlmanager.h

    red39269 rdbd1d1f4  
    127127        Q_INVOKABLE QString getBtAddress();
    128128#if defined(BT_SUPPORT)
     129        struct btPairedDevice {
     130                QBluetoothAddress address;
     131                QString name;
     132        };
    129133        void btDeviceDiscovered(const QBluetoothDeviceInfo &device);
    130 #endif
    131         QStringList getBluetoothDevices();
     134        void getBluetoothDevices();
     135#endif
    132136
    133137public slots:
     
    218222
    219223#if defined(BT_SUPPORT)
     224        QList<struct btPairedDevice> btPairedDevices;
    220225        QBluetoothLocalDevice localBtDevice;
    221226        QBluetoothDeviceDiscoveryAgent *discoveryAgent;
Note: See TracChangeset for help on using the changeset viewer.