I attached a patch which should resolve the issues regarding
the connection.

I know that this is not the ideal solution but I wanted to cover all cases.
First I try to make a connection using the UUID of a service.
If the timer expires and the device is in the ServiceLookupState, then
I extend the waiting time.
If the connection ends with a ServiceNotFoundError then I try to force
the connection on channel 1. If this doesn't work, I try again on channel
number 5.

I took this decision because the QBluetoothServiceDiscoveryAgent
cannot find the SPP service from my HW OSTCs device. I don't know
why. I captured the HCI events during the Qt service discovery proces
and they are identical with the ones raised by sdptool.
The interesting thing is that when I use my HW OSTC 2 device, the SPP
service is discovered.

If you want I can remove the part where it tries to initiate a connection
using SPP's uuid. I can try to make a connection on channel 1 and if
it fails I try again on channel number 5. This would work if the SPP
service is always listening on channel 1 or on channel 5.

Cheers,
Claudiu
From 18edac581b3d3f3bb4221f627d4751f5db55d904 Mon Sep 17 00:00:00 2001
From: Claudiu Olteanu <[email protected]>
Date: Wed, 1 Jul 2015 01:50:12 +0300
Subject: [PATCH 4/4] Add different attemtps for Bluetooth connection

First try to make a connection using the UUID of a service.
If the timer expires and the device is in the ServiceLookupState,
then extend the waiting time.
If the connection ends with a ServiceNotFoundError then try to
force the connection on channel 1. If this doesn't work, try again
on channel number 5.

Signed-off-by: Claudiu Olteanu <[email protected]>
---
 qtserialbluetooth.cpp | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

diff --git a/qtserialbluetooth.cpp b/qtserialbluetooth.cpp
index 329907b..d56f5fd 100644
--- a/qtserialbluetooth.cpp
+++ b/qtserialbluetooth.cpp
@@ -4,6 +4,7 @@
 #include <QtBluetooth/QBluetoothSocket>
 #include <QEventLoop>
 #include <QTimer>
+#include <QDebug>
 
 #include <libdivecomputer/custom_serial.h>
 
@@ -45,14 +46,40 @@ static int qt_serial_open(serial_t **out, dc_context_t *context, const char* dev
 
 	// Create a timer. If the connection doesn't succeed after five seconds or no error occurs then stop the opening step
 	QTimer timer;
+	int msec = 5000;
 	timer.setSingleShot(true);
 	loop.connect(&timer, SIGNAL(timeout()), SLOT(quit()));
 
 	// Try to connect to the Serial Port Profile service
-	serial_port->socket->connectToService(QBluetoothAddress(devaddr), QBluetoothUuid::SerialPort);
-	timer.start(5000);
+	QBluetoothAddress remoteDeviceAddress(devaddr);
+	serial_port->socket->connectToService(remoteDeviceAddress, QBluetoothUuid(QBluetoothUuid::SerialPort));
+	timer.start(msec);
 	loop.exec();
 
+	if (serial_port->socket->state() == QBluetoothSocket::ServiceLookupState) {
+		// It seems that the lookup for Serial Port profile took more than expected. Take a beer and wait another 20 seconds :)
+		qDebug() << "The lookup service took more than expected. Wait another 20 seconds.";
+		timer.start(4 * msec);
+		loop.exec();
+	}
+
+	if (serial_port->socket->error() == QBluetoothSocket::ServiceNotFoundError) {
+		// If Serial Port Profile wasn't found by the discovery agent try to connect to channel number 1.
+		// Most devices uses this port for the Serial Port Profile service.
+		qDebug() << "Didn't find the Serial Port Profile. Trying to connect to channel number 1.";
+		serial_port->socket->connectToService(remoteDeviceAddress, 1);
+		timer.start(msec);
+		loop.exec();
+
+		if (serial_port->socket->state() != QBluetoothSocket::ConnectedState) {
+			// Try to connect on channel number 5. Maybe this is a Shearwater Petrel2 device.
+			qDebug() << "Connection on channel 1 failed. Trying on channel number 5.";
+			serial_port->socket->connectToService(remoteDeviceAddress, 5);
+			timer.start(msec);
+			loop.exec();
+		}
+	}
+
 	if (serial_port->socket->socketDescriptor() == -1 || serial_port->socket->state() != QBluetoothSocket::ConnectedState) {
 		free (serial_port);
 
-- 
2.1.4

_______________________________________________
subsurface mailing list
[email protected]
http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to