From c6c706c28a90c78617c911b8c9c1192e6800d8f7 Mon Sep 17 00:00:00 2001 From: Claudiu Olteanu Date: Mon, 13 Jul 2015 23:37:49 +0300 Subject: Use SPP's uuid to connect to a device on Android platform On Android, a Bluetooth connection to a service cannot be established using a port. Therefore we use the uuid of the Serial Port Profile service to connect to the remote BT device. Signed-off-by: Claudiu Olteanu Signed-off-by: Dirk Hohndel --- qtserialbluetooth.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/qtserialbluetooth.cpp b/qtserialbluetooth.cpp index f06f24e89..95f3611cf 100644 --- a/qtserialbluetooth.cpp +++ b/qtserialbluetooth.cpp @@ -54,6 +54,7 @@ static int qt_serial_open(serial_t **out, dc_context_t *context, const char* dev timer.setSingleShot(true); loop.connect(&timer, SIGNAL(timeout()), SLOT(quit())); +#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID) // First try to connect on RFCOMM channel 1. This is the default channel for most devices QBluetoothAddress remoteDeviceAddress(devaddr); serial_port->socket->connectToService(remoteDeviceAddress, 1); @@ -79,8 +80,22 @@ static int qt_serial_open(serial_t **out, dc_context_t *context, const char* dev loop.exec(); } } +#elif defined(Q_OS_ANDROID) + // Try to connect to the device using the uuid of the Serial Port Profile service + QBluetoothAddress remoteDeviceAddress(devaddr); + serial_port->socket->connectToService(remoteDeviceAddress, QBluetoothUuid(QBluetoothUuid::SerialPort)); + timer.start(msec); + loop.exec(); - if (serial_port->socket->socketDescriptor() == -1 || serial_port->socket->state() != QBluetoothSocket::ConnectedState) { + if (serial_port->socket->state() == QBluetoothSocket::ConnectingState || + serial_port->socket->state() == QBluetoothSocket::ServiceLookupState) { + // It seems that the connection step took more than expected. Wait another 20 seconds. + qDebug() << "The connection step took more than expected. Wait another 20 seconds"; + timer.start(4 * msec); + loop.exec(); + } +#endif + if (serial_port->socket->state() != QBluetoothSocket::ConnectedState) { free (serial_port); // Get the latest error and try to match it with one from libdivecomputer -- cgit v1.2.3-70-g09d2