1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
// SPDX-License-Identifier: GPL-2.0
#include "btdiscovery.h"
#include "downloadfromdcthread.h"
#include "core/libdivecomputer.h"
#include <QTimer>
#include <QDebug>
extern QMap<QString, dc_descriptor_t *> descriptorLookup;
BTDiscovery *BTDiscovery::m_instance = NULL;
ConnectionListModel::ConnectionListModel(QObject *parent) :
QAbstractListModel(parent)
{
}
QHash <int, QByteArray> ConnectionListModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[AddressRole] = "address";
return roles;
}
QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= m_addresses.count())
return QVariant();
if (role != AddressRole)
return QVariant();
return m_addresses[index.row()];
}
QString ConnectionListModel::address(int idx) const
{
if (idx < 0 || idx >> m_addresses.count())
return QString();
return m_addresses[idx];
}
int ConnectionListModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_addresses.count();
}
void ConnectionListModel::addAddress(const QString address)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_addresses.append(address);
endInsertRows();
}
static dc_descriptor_t *getDeviceType(QString btName)
// central function to convert a BT name to a Subsurface known vendor/model pair
{
QString vendor, product;
if (btName.startsWith("OSTC")) {
vendor = "Heinrichs Weikamp";
if (btName.mid(4,2) == "3#") product = "OSTC 3";
else if (btName.mid(4,2) == "3+") product = "OSTC 3+";
else if (btName.mid(4,2) == "s#") product = "OSTC Sport";
else if (btName.mid(4,2) == "s ") product = "OSTC Sport";
else if (btName.mid(4,2) == "4-") product = "OSTC 4";
else if (btName.mid(4,2) == "2-") product = "OSTC 2N";
// all OSTCs are HW_FAMILY_OSTC_3, so when we do not know,
// just try this
else product = "OSTC 3"; // all OSTCs are HW_FAMILY_OSTC_3
}
if (btName.startsWith("Petrel") || btName.startsWith("Perdix") || btName.startsWith("Predator")) {
vendor = "Shearwater";
if (btName.startsWith("Petrel")) product = "Petrel"; // or petrel 2?
if (btName.startsWith("Perdix")) product = "Perdix";
if (btName.startsWith("Predator")) product = "Predator";
}
if (btName.startsWith("EON Steel")) {
vendor = "Suunto";
product = "EON Steel";
}
if (btName.startsWith("G2")) {
vendor = "Scubapro";
product = "G2";
}
if (!vendor.isEmpty() && !product.isEmpty())
return(descriptorLookup[vendor + product]);
return(NULL);
}
BTDiscovery::BTDiscovery(QObject *parent)
{
Q_UNUSED(parent)
if (m_instance) {
qDebug() << "trying to create an additional BTDiscovery object";
return;
}
m_instance = this;
#if defined(BT_SUPPORT)
#if !defined(Q_OS_IOS)
if (localBtDevice.isValid() &&
localBtDevice.hostMode() == QBluetoothLocalDevice::HostConnectable) {
btPairedDevices.clear();
qDebug() << "localDevice " + localBtDevice.name() + " is valid, starting discovery";
m_btValid = true;
#else
m_btValid = false;
#endif
#if defined(Q_OS_IOS) || (defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID))
discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BTDiscovery::btDeviceDiscovered);
qDebug() << "starting BLE discovery";
discoveryAgent->start();
#endif
#if defined(Q_OS_ANDROID) && defined(BT_SUPPORT)
getBluetoothDevices();
// and add the paired devices to the internal data
// So behaviour is same on Linux/Bluez stack and
// Android/Java stack with respect to discovery
for (int i = 0; i < btPairedDevices.length(); i++) {
btDeviceDiscoveredMain(btPairedDevices[i]);
}
#endif
for (int i = 0; i < btPairedDevices.length(); i++) {
qDebug() << "Paired =" << btPairedDevices[i].name << btPairedDevices[i].address;
}
#if defined(Q_OS_IOS) || (defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID))
QTimer timer;
timer.setSingleShot(true);
connect(&timer, &QTimer::timeout, discoveryAgent, &QBluetoothDeviceDiscoveryAgent::stop);
timer.start(3000);
#endif
#if !defined(Q_OS_IOS)
} else {
qDebug() << "localBtDevice isn't valid";
m_btValid = false;
}
#endif
#endif
}
BTDiscovery::~BTDiscovery()
{
m_instance = NULL;
#if defined(BT_SUPPORT)
free(discoveryAgent);
#endif
}
BTDiscovery *BTDiscovery::instance()
{
if (!m_instance)
m_instance = new BTDiscovery();
return m_instance;
}
#if defined(BT_SUPPORT)
#if defined(SSRF_CUSTOM_IO)
extern void addBtUuid(QBluetoothUuid uuid);
#endif
extern QHash<QString, QStringList> productList;
extern QStringList vendorList;
QString markBLEAddress(const QBluetoothDeviceInfo *device)
{
QBluetoothDeviceInfo::CoreConfigurations flags;
QString prefix = "";
flags = device->coreConfigurations();
if (flags == QBluetoothDeviceInfo::LowEnergyCoreConfiguration)
prefix = "LE:";
if (device->address().isNull())
return prefix + device->deviceUuid().toString();
else
return prefix + device->address().toString();
}
void BTDiscovery::btDeviceDiscovered(const QBluetoothDeviceInfo &device)
{
#if defined(SSRF_CUSTOM_IO)
btPairedDevice this_d;
this_d.address = markBLEAddress(&device);
this_d.name = device.name();
btPairedDevices.append(this_d);
QList<QBluetoothUuid> serviceUuids = device.serviceUuids();
foreach (QBluetoothUuid id, serviceUuids) {
addBtUuid(id);
qDebug() << id.toByteArray();
}
btDeviceDiscoveredMain(this_d);
#endif
}
void BTDiscovery::btDeviceDiscoveredMain(const btPairedDevice &device)
{
btVendorProduct btVP;
QString newDevice;
dc_descriptor_t *newDC = getDeviceType(device.name);
if (newDC)
newDevice = dc_descriptor_get_product(newDC);
else
newDevice = device.name;
qDebug() << "Found new device:" << newDevice << device.address;
if (newDC) {
QString vendor = dc_descriptor_get_vendor(newDC);
qDebug() << "this could be a " + vendor + " " + newDevice;
btVP.btpdi = device;
btVP.dcDescriptor = newDC;
btVP.vendorIdx = vendorList.indexOf(vendor);
btVP.productIdx = productList[vendor].indexOf(newDevice);
btDCs << btVP;
connectionListModel.addAddress(device.address + " (" + newDevice + ")");
return;
}
connectionListModel.addAddress(device.address);
qDebug() << "Not recognized as dive computer";
}
QList<BTDiscovery::btVendorProduct> BTDiscovery::getBtDcs()
{
return btDCs;
}
bool BTDiscovery::btAvailable() const
{
return m_btValid;
}
// Android: As Qt is not able to pull the pairing data from a device, i
// 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".
#if defined(Q_OS_ANDROID)
void BTDiscovery::getBluetoothDevices()
{
struct BTDiscovery::btPairedDevice result;
// Query via Android Java API.
// returns a BluetoothAdapter
QAndroidJniObject adapter=QAndroidJniObject::callStaticObjectMethod("android/bluetooth/BluetoothAdapter","getDefaultAdapter","()Landroid/bluetooth/BluetoothAdapter;");
if (checkException("BluetoothAdapter.getDefaultAdapter()", &adapter)) {
return;
}
// returns a Set<BluetoothDevice>
QAndroidJniObject pairedDevicesSet=adapter.callObjectMethod("getBondedDevices","()Ljava/util/Set;");
if (checkException("BluetoothAdapter.getBondedDevices()", &pairedDevicesSet)) {
return;
}
jint size=pairedDevicesSet.callMethod<jint>("size");
checkException("Set<BluetoothDevice>.size()", &pairedDevicesSet);
if (size > 0) {
// returns an Iterator<BluetoothDevice>
QAndroidJniObject iterator=pairedDevicesSet.callObjectMethod("iterator","()Ljava/util/Iterator;");
if (checkException("Set<BluetoothDevice>.iterator()", &iterator)) {
return;
}
for (int i = 0; i < size; i++) {
// returns a BluetoothDevice
QAndroidJniObject dev=iterator.callObjectMethod("next","()Ljava/lang/Object;");
if (checkException("Iterator<BluetoothDevice>.next()", &dev)) {
continue;
}
jint btType = dev.callMethod<jint>("getType", "()I");
result.address = dev.callObjectMethod("getAddress","()Ljava/lang/String;").toString();
if (btType == 2) // DEVICE_TYPE_LE
result.address = QString("LE:%1").arg(result.address);
result.name = dev.callObjectMethod("getName", "()Ljava/lang/String;").toString();
qDebug() << "paired Device type" << btType << "with address" << result.address;
btPairedDevices.append(result);
if (btType == 3) { // DEVICE_TYPE_DUAL
result.address = QString("LE:%1").arg(result.address);
qDebug() << "paired Device type" << btType << "with address" << result.address;
btPairedDevices.append(result);
}
}
}
}
bool BTDiscovery::checkException(const char* method, const QAndroidJniObject *obj)
{
static QAndroidJniEnvironment env;
bool result = false;
if (env->ExceptionCheck()) {
qCritical("Exception in %s", method);
env->ExceptionDescribe();
env->ExceptionClear();
result=true;
}
if (!(obj == NULL || obj->isValid())) {
qCritical("Invalid object returned by %s", method);
result=true;
}
return result;
}
#endif // Q_OS_ANDROID
#endif // BT_SUPPORT
|