| 
									
										
										
										
											2017-04-27 20:26:05 +02:00
										 |  |  | // SPDX-License-Identifier: GPL-2.0
 | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | #include <QShortcut>
 | 
					
						
							|  |  |  | #include <QDebug>
 | 
					
						
							|  |  |  | #include <QMessageBox>
 | 
					
						
							|  |  |  | #include <QMenu>
 | 
					
						
							| 
									
										
										
										
											2017-09-16 20:18:12 -07:00
										 |  |  | #include "core/btdiscovery.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QBluetoothUuid>
 | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "ui_btdeviceselectiondialog.h"
 | 
					
						
							|  |  |  | #include "btdeviceselectiondialog.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | BtDeviceSelectionDialog::BtDeviceSelectionDialog(QWidget *parent) : | 
					
						
							|  |  |  | 	QDialog(parent), | 
					
						
							| 
									
										
										
										
											2015-08-27 09:22:17 -07:00
										 |  |  | 	ui(new Ui::BtDeviceSelectionDialog), | 
					
						
							|  |  |  | 	remoteDeviceDiscoveryAgent(0) | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | { | 
					
						
							|  |  |  | 	ui->setupUi(this); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Quit button callbacks
 | 
					
						
							|  |  |  | 	QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this); | 
					
						
							|  |  |  | 	connect(quit, SIGNAL(activated()), this, SLOT(reject())); | 
					
						
							|  |  |  | 	connect(ui->quit, SIGNAL(clicked()), this, SLOT(reject())); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 	// Translate the UI labels
 | 
					
						
							|  |  |  | 	ui->localDeviceDetails->setTitle(tr("Local Bluetooth device details")); | 
					
						
							|  |  |  | 	ui->selectDeviceLabel->setText(tr("Select device:")); | 
					
						
							|  |  |  | 	ui->deviceAddressLabel->setText(tr("Address:")); | 
					
						
							|  |  |  | 	ui->deviceNameLabel->setText(tr("Name:")); | 
					
						
							|  |  |  | 	ui->deviceState->setText(tr("Bluetooth powered on")); | 
					
						
							| 
									
										
										
										
											2015-09-04 23:25:33 +01:00
										 |  |  | 	ui->changeDeviceState->setText(tr("Turn on/off")); | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 	ui->discoveredDevicesLabel->setText(tr("Discovered devices")); | 
					
						
							|  |  |  | 	ui->scan->setText(tr("Scan")); | 
					
						
							|  |  |  | 	ui->clear->setText(tr("Clear")); | 
					
						
							|  |  |  | 	ui->save->setText(tr("Save")); | 
					
						
							| 
									
										
										
										
											2017-11-05 18:16:46 +01:00
										 |  |  | 	ui->save->setDefault(true); | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 	ui->quit->setText(tr("Quit")); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 	// Disable the save button because there is no device selected
 | 
					
						
							|  |  |  | 	ui->save->setEnabled(false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-18 20:51:10 +03:00
										 |  |  | 	// Add event for item selection
 | 
					
						
							| 
									
										
										
										
											2017-11-04 15:09:07 +01:00
										 |  |  | 	connect(ui->discoveredDevicesList, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), | 
					
						
							|  |  |  | 		this, SLOT(currentItemChanged(QListWidgetItem*,QListWidgetItem*))); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-18 20:51:10 +03:00
										 |  |  | 	// Initialize the local Bluetooth device
 | 
					
						
							|  |  |  | 	localDevice = new QBluetoothLocalDevice(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-18 17:50:18 +03:00
										 |  |  | 	// Populate the list with local bluetooth devices
 | 
					
						
							|  |  |  | 	QList<QBluetoothHostInfo> localAvailableDevices = localDevice->allDevices(); | 
					
						
							|  |  |  | 	int availableDevicesSize = localAvailableDevices.size(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-19 21:50:06 +03:00
										 |  |  | 	if (availableDevicesSize > 1) { | 
					
						
							|  |  |  | 		int defaultDeviceIndex = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (int it = 0; it < availableDevicesSize; it++) { | 
					
						
							|  |  |  | 			QBluetoothHostInfo localAvailableDevice = localAvailableDevices.at(it); | 
					
						
							|  |  |  | 			ui->localSelectedDevice->addItem(localAvailableDevice.name(), | 
					
						
							|  |  |  | 							 QVariant::fromValue(localAvailableDevice.address())); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (localDevice->address() == localAvailableDevice.address()) | 
					
						
							|  |  |  | 				defaultDeviceIndex = it; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Positionate the current index to the default device and register to index changes events
 | 
					
						
							|  |  |  | 		ui->localSelectedDevice->setCurrentIndex(defaultDeviceIndex); | 
					
						
							|  |  |  | 		connect(ui->localSelectedDevice, SIGNAL(currentIndexChanged(int)), | 
					
						
							|  |  |  | 			this, SLOT(localDeviceChanged(int))); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		// If there is only one local Bluetooth adapter hide the combobox and the label
 | 
					
						
							| 
									
										
										
										
											2015-07-19 22:05:00 +03:00
										 |  |  | 		ui->selectDeviceLabel->hide(); | 
					
						
							| 
									
										
										
										
											2015-07-19 21:50:06 +03:00
										 |  |  | 		ui->localSelectedDevice->hide(); | 
					
						
							| 
									
										
										
										
											2015-07-18 17:50:18 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-18 17:50:18 +03:00
										 |  |  | 	// Update the UI information about the local device
 | 
					
						
							|  |  |  | 	updateLocalDeviceInformation(); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-18 20:51:45 +03:00
										 |  |  | 	// Initialize the device discovery agent
 | 
					
						
							|  |  |  | 	if (localDevice->isValid()) | 
					
						
							|  |  |  | 		initializeDeviceDiscoveryAgent(); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | BtDeviceSelectionDialog::~BtDeviceSelectionDialog() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	delete ui; | 
					
						
							| 
									
										
										
										
											2015-08-18 20:11:25 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Clean the local device
 | 
					
						
							|  |  |  | 	delete localDevice; | 
					
						
							| 
									
										
										
										
											2018-09-29 22:39:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-27 09:22:17 -07:00
										 |  |  | 	if (remoteDeviceDiscoveryAgent) { | 
					
						
							|  |  |  | 		// Clean the device discovery agent
 | 
					
						
							| 
									
										
										
										
											2018-09-29 22:39:07 -07:00
										 |  |  | 		if (remoteDeviceDiscoveryAgent->isActive()) | 
					
						
							| 
									
										
										
										
											2015-08-27 09:22:17 -07:00
										 |  |  | 			remoteDeviceDiscoveryAgent->stop(); | 
					
						
							|  |  |  | 		delete remoteDeviceDiscoveryAgent; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void BtDeviceSelectionDialog::on_changeDeviceState_clicked() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (localDevice->hostMode() == QBluetoothLocalDevice::HostPoweredOff) { | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 		ui->dialogStatus->setText(tr("Trying to turn on the local Bluetooth device...")); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 		localDevice->powerOn(); | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 		ui->dialogStatus->setText(tr("Trying to turn off the local Bluetooth device...")); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 		localDevice->setHostMode(QBluetoothLocalDevice::HostPoweredOff); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void BtDeviceSelectionDialog::on_save_clicked() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// Get the selected device. There will be always a selected device if the save button is enabled.
 | 
					
						
							|  |  |  | 	QListWidgetItem *currentItem = ui->discoveredDevicesList->currentItem(); | 
					
						
							|  |  |  | 	QBluetoothDeviceInfo remoteDeviceInfo = currentItem->data(Qt::UserRole).value<QBluetoothDeviceInfo>(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Save the selected device
 | 
					
						
							| 
									
										
										
										
											2017-11-11 22:12:21 +01:00
										 |  |  | 	selectedRemoteDeviceInfo.reset(new QBluetoothDeviceInfo(remoteDeviceInfo)); | 
					
						
							| 
									
										
										
										
											2017-09-16 20:21:46 -07:00
										 |  |  | 	QString address = remoteDeviceInfo.address().isNull() ? remoteDeviceInfo.deviceUuid().toString() : | 
					
						
							|  |  |  | 								remoteDeviceInfo.address().toString(); | 
					
						
							| 
									
										
										
										
											2018-02-25 00:17:33 +01:00
										 |  |  | 	saveBtDeviceInfo(address, remoteDeviceInfo); | 
					
						
							| 
									
										
										
										
											2015-07-16 23:06:22 +03:00
										 |  |  | 	if (remoteDeviceDiscoveryAgent->isActive()) { | 
					
						
							|  |  |  | 		// Stop the SDP agent if the clear button is pressed and enable the Scan button
 | 
					
						
							|  |  |  | 		remoteDeviceDiscoveryAgent->stop(); | 
					
						
							|  |  |  | 		ui->scan->setEnabled(true); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 	// Close the device selection dialog and set the result code to Accepted
 | 
					
						
							|  |  |  | 	accept(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void BtDeviceSelectionDialog::on_clear_clicked() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-04 23:25:33 +01:00
										 |  |  | 	ui->dialogStatus->setText(tr("Remote devices list was cleared.")); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 	ui->discoveredDevicesList->clear(); | 
					
						
							| 
									
										
										
										
											2015-07-16 23:06:22 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (remoteDeviceDiscoveryAgent->isActive()) { | 
					
						
							|  |  |  | 		// Stop the SDP agent if the clear button is pressed and enable the Scan button
 | 
					
						
							|  |  |  | 		remoteDeviceDiscoveryAgent->stop(); | 
					
						
							|  |  |  | 		ui->scan->setEnabled(true); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void BtDeviceSelectionDialog::on_scan_clicked() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 	ui->dialogStatus->setText(tr("Scanning for remote devices...")); | 
					
						
							| 
									
										
										
										
											2015-08-18 20:15:49 +03:00
										 |  |  | 	ui->discoveredDevicesList->clear(); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 	remoteDeviceDiscoveryAgent->start(); | 
					
						
							|  |  |  | 	ui->scan->setEnabled(false); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void BtDeviceSelectionDialog::remoteDeviceScanFinished() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-06-28 08:04:53 +02:00
										 |  |  | 	// This check is not necessary for Qt's QBluetoothDeviceDiscoveryAgent,
 | 
					
						
							|  |  |  | 	// but with the home-brew WinBluetoothDeviceDiscoveryAgent, on error we
 | 
					
						
							|  |  |  | 	// get an error() and an finished() signal. Thus, don't overwrite the
 | 
					
						
							|  |  |  | 	// error message with a success message.
 | 
					
						
							|  |  |  | 	if (remoteDeviceDiscoveryAgent->error() == QBluetoothDeviceDiscoveryAgent::NoError) | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 		ui->dialogStatus->setText(tr("Scanning finished successfully.")); | 
					
						
							| 
									
										
										
										
											2015-08-18 20:12:15 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 	ui->scan->setEnabled(true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void BtDeviceSelectionDialog::hostModeStateChanged(QBluetoothLocalDevice::HostMode mode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	bool on = !(mode == QBluetoothLocalDevice::HostPoweredOff); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-05 11:01:17 +02:00
										 |  |  | 	//: %1 will be replaced with "turned on" or "turned off"
 | 
					
						
							|  |  |  | 	ui->dialogStatus->setText(tr("The local Bluetooth device was %1.") | 
					
						
							|  |  |  | 				  .arg(on? tr("turned on") : tr("turned off"))); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 	ui->deviceState->setChecked(on); | 
					
						
							|  |  |  | 	ui->scan->setEnabled(on); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void BtDeviceSelectionDialog::addRemoteDevice(const QBluetoothDeviceInfo &remoteDeviceInfo) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-02-12 07:35:35 -08:00
										 |  |  | 	// are we supposed to show all devices or just dive computers?
 | 
					
						
							|  |  |  | 	if (!ui->showNonDivecomputers->isChecked() && !matchesKnownDiveComputerNames(remoteDeviceInfo.name())) | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2015-08-18 20:51:10 +03:00
										 |  |  | #if defined(Q_OS_WIN)
 | 
					
						
							| 
									
										
										
										
											2015-08-18 22:27:16 +03:00
										 |  |  | 	// On Windows we cannot obtain the pairing status so we set only the name and the address of the device
 | 
					
						
							|  |  |  | 	QString deviceLabel = QString("%1 (%2)").arg(remoteDeviceInfo.name(), | 
					
						
							|  |  |  | 						     remoteDeviceInfo.address().toString()); | 
					
						
							|  |  |  | 	QColor pairingColor = QColor(Qt::white); | 
					
						
							| 
									
										
										
										
											2015-08-18 20:51:10 +03:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2015-08-18 20:15:49 +03:00
										 |  |  | 	// By default we use the status label and the color for the UNPAIRED state
 | 
					
						
							| 
									
										
										
										
											2017-06-29 15:19:33 -07:00
										 |  |  | 	QColor pairingColor = QColor("#F1A9A0"); | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 	QString pairingStatusLabel = tr("UNPAIRED"); | 
					
						
							| 
									
										
										
										
											2015-08-18 20:15:49 +03:00
										 |  |  | 	QBluetoothLocalDevice::Pairing pairingStatus = localDevice->pairingStatus(remoteDeviceInfo.address()); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-18 20:15:49 +03:00
										 |  |  | 	if (pairingStatus == QBluetoothLocalDevice::Paired) { | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 		pairingStatusLabel = tr("PAIRED"); | 
					
						
							| 
									
										
										
										
											2015-08-18 20:15:49 +03:00
										 |  |  | 		pairingColor = QColor(Qt::gray); | 
					
						
							|  |  |  | 	} else if (pairingStatus == QBluetoothLocalDevice::AuthorizedPaired) { | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 		pairingStatusLabel = tr("AUTHORIZED_PAIRED"); | 
					
						
							| 
									
										
										
										
											2017-06-29 15:19:33 -07:00
										 |  |  | 		pairingColor = QColor("#89C4F4"); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-06-29 23:37:30 +02:00
										 |  |  | 	if (remoteDeviceInfo.address().isNull()) | 
					
						
							|  |  |  | 		pairingColor = QColor(Qt::gray); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-16 20:18:12 -07:00
										 |  |  | 	QString deviceLabel; | 
					
						
							| 
									
										
										
										
											2015-08-18 20:15:49 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-16 20:18:12 -07:00
										 |  |  | #if defined(Q_OS_MACOS) || defined(Q_OS_IOS)
 | 
					
						
							|  |  |  | 	if (!remoteDeviceInfo.deviceUuid().isNull()) { | 
					
						
							|  |  |  | 		// we have only a Uuid, no address, so show that and reset the pairing color
 | 
					
						
							|  |  |  | 		deviceLabel = QString("%1 (%2)").arg(remoteDeviceInfo.name(),remoteDeviceInfo.deviceUuid().toString()); | 
					
						
							|  |  |  | 		pairingColor = QColor(Qt::white); | 
					
						
							|  |  |  | 	} else | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 	deviceLabel = tr("%1 (%2)   [State: %3]").arg(remoteDeviceInfo.name(), | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 							      remoteDeviceInfo.address().toString(), | 
					
						
							|  |  |  | 							      pairingStatusLabel); | 
					
						
							| 
									
										
										
										
											2015-08-18 22:27:16 +03:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 	// Create the new item, set its information and add it to the list
 | 
					
						
							| 
									
										
										
										
											2015-08-18 20:15:49 +03:00
										 |  |  | 	QListWidgetItem *item = new QListWidgetItem(deviceLabel); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	item->setData(Qt::UserRole, QVariant::fromValue(remoteDeviceInfo)); | 
					
						
							|  |  |  | 	item->setBackgroundColor(pairingColor); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ui->discoveredDevicesList->addItem(item); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 15:09:07 +01:00
										 |  |  | void BtDeviceSelectionDialog::currentItemChanged(QListWidgetItem *item, QListWidgetItem *) | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-11-04 15:09:07 +01:00
										 |  |  | 	// If the list is cleared, we get a signal with a null item pointer
 | 
					
						
							|  |  |  | 	if (!item) { | 
					
						
							|  |  |  | 		ui->save->setEnabled(false); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-18 22:38:14 +03:00
										 |  |  | 	// By default we assume that the devices are paired
 | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 	QBluetoothDeviceInfo remoteDeviceInfo = item->data(Qt::UserRole).value<QBluetoothDeviceInfo>(); | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 	QString statusMessage = tr("The device %1 can be used for connection. You can press the Save button.") | 
					
						
							| 
									
										
										
										
											2017-09-16 20:18:12 -07:00
										 |  |  | 				  .arg(remoteDeviceInfo.address().isNull() ? | 
					
						
							|  |  |  | 					       remoteDeviceInfo.deviceUuid().toString() : | 
					
						
							|  |  |  | 					       remoteDeviceInfo.address().toString()); | 
					
						
							| 
									
										
										
										
											2015-08-18 22:38:14 +03:00
										 |  |  | 	bool enableSaveButton = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if !defined(Q_OS_WIN)
 | 
					
						
							|  |  |  | 	// On other platforms than Windows we can obtain the pairing status so if the devices are not paired we disable the button
 | 
					
						
							| 
									
										
										
										
											2017-09-16 20:18:12 -07:00
										 |  |  | 	// except on MacOS for those devices that only give us a Uuid and not and address (as we have no pairing status for those, either)
 | 
					
						
							|  |  |  | 	if (!remoteDeviceInfo.address().isNull()) { | 
					
						
							|  |  |  | 		QBluetoothLocalDevice::Pairing pairingStatus = localDevice->pairingStatus(remoteDeviceInfo.address()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (pairingStatus == QBluetoothLocalDevice::Unpaired) { | 
					
						
							|  |  |  | 			statusMessage = tr("The device %1 must be paired in order to be used. Please use the context menu for pairing options.") | 
					
						
							|  |  |  | 					.arg(remoteDeviceInfo.address().toString()); | 
					
						
							|  |  |  | 			enableSaveButton = false; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-16 20:18:12 -07:00
										 |  |  | 	if (remoteDeviceInfo.address().isNull() && remoteDeviceInfo.deviceUuid().isNull()) { | 
					
						
							| 
									
										
										
										
											2017-06-29 23:37:30 +02:00
										 |  |  | 		statusMessage = tr("A device needs a non-zero address for a connection."); | 
					
						
							|  |  |  | 		enableSaveButton = false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-18 20:51:10 +03:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-08-18 22:38:14 +03:00
										 |  |  | 	// Update the status message and the save button
 | 
					
						
							|  |  |  | 	ui->dialogStatus->setText(statusMessage); | 
					
						
							|  |  |  | 	ui->save->setEnabled(enableSaveButton); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-18 17:50:18 +03:00
										 |  |  | void BtDeviceSelectionDialog::localDeviceChanged(int index) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QBluetoothAddress localDeviceSelectedAddress = ui->localSelectedDevice->itemData(index, Qt::UserRole).value<QBluetoothAddress>(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Delete the old localDevice
 | 
					
						
							|  |  |  | 	if (localDevice) | 
					
						
							|  |  |  | 		delete localDevice; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Create a new local device using the selected address
 | 
					
						
							|  |  |  | 	localDevice = new QBluetoothLocalDevice(localDeviceSelectedAddress); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 	ui->dialogStatus->setText(tr("The local device was changed.")); | 
					
						
							| 
									
										
										
										
											2015-07-18 21:01:40 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-18 17:50:18 +03:00
										 |  |  | 	// Clear the discovered devices list
 | 
					
						
							|  |  |  | 	on_clear_clicked(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Update the UI information about the local device
 | 
					
						
							|  |  |  | 	updateLocalDeviceInformation(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-18 20:51:45 +03:00
										 |  |  | 	// Initialize the device discovery agent
 | 
					
						
							|  |  |  | 	if (localDevice->isValid()) | 
					
						
							|  |  |  | 		initializeDeviceDiscoveryAgent(); | 
					
						
							| 
									
										
										
										
											2015-07-18 17:50:18 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | void BtDeviceSelectionDialog::displayPairingMenu(const QPoint &pos) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QMenu menu(this); | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 	QAction *pairAction = menu.addAction(tr("Pair")); | 
					
						
							| 
									
										
										
										
											2015-09-04 23:25:33 +01:00
										 |  |  | 	QAction *removePairAction = menu.addAction(tr("Remove pairing")); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 	QAction *chosenAction = menu.exec(ui->discoveredDevicesList->viewport()->mapToGlobal(pos)); | 
					
						
							|  |  |  | 	QListWidgetItem *currentItem = ui->discoveredDevicesList->currentItem(); | 
					
						
							|  |  |  | 	QBluetoothDeviceInfo currentRemoteDeviceInfo = currentItem->data(Qt::UserRole).value<QBluetoothDeviceInfo>(); | 
					
						
							|  |  |  | 	QBluetoothLocalDevice::Pairing pairingStatus = localDevice->pairingStatus(currentRemoteDeviceInfo.address()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//TODO: disable the actions
 | 
					
						
							|  |  |  | 	if (pairingStatus == QBluetoothLocalDevice::Unpaired) { | 
					
						
							|  |  |  | 		pairAction->setEnabled(true); | 
					
						
							|  |  |  | 		removePairAction->setEnabled(false); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		pairAction->setEnabled(false); | 
					
						
							|  |  |  | 		removePairAction->setEnabled(true); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (chosenAction == pairAction) { | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 		ui->dialogStatus->setText(tr("Trying to pair device %1") | 
					
						
							|  |  |  | 					    .arg(currentRemoteDeviceInfo.address().toString())); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 		localDevice->requestPairing(currentRemoteDeviceInfo.address(), QBluetoothLocalDevice::Paired); | 
					
						
							|  |  |  | 	} else if (chosenAction == removePairAction) { | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 		ui->dialogStatus->setText(tr("Trying to unpair device %1") | 
					
						
							|  |  |  | 					    .arg(currentRemoteDeviceInfo.address().toString())); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 		localDevice->requestPairing(currentRemoteDeviceInfo.address(), QBluetoothLocalDevice::Unpaired); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void BtDeviceSelectionDialog::pairingFinished(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-18 20:17:11 +03:00
										 |  |  | 	// Determine the color, the new pairing status and the log message. By default we assume that the devices are UNPAIRED.
 | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 	QString remoteDeviceStringAddress = address.toString(); | 
					
						
							| 
									
										
										
										
											2015-08-18 20:17:11 +03:00
										 |  |  | 	QColor pairingColor = QColor(Qt::red); | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 	QString pairingStatusLabel = tr("UNPAIRED"); | 
					
						
							|  |  |  | 	QString dialogStatusMessage = tr("Device %1 was unpaired.").arg(remoteDeviceStringAddress); | 
					
						
							| 
									
										
										
										
											2015-08-18 20:17:11 +03:00
										 |  |  | 	bool enableSaveButton = false; | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-18 20:17:11 +03:00
										 |  |  | 	if (pairing == QBluetoothLocalDevice::Paired) { | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 		pairingStatusLabel = tr("PAIRED"); | 
					
						
							| 
									
										
										
										
											2015-08-18 20:17:11 +03:00
										 |  |  | 		pairingColor = QColor(Qt::gray); | 
					
						
							|  |  |  | 		enableSaveButton = true; | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 		dialogStatusMessage = tr("Device %1 was paired.").arg(remoteDeviceStringAddress); | 
					
						
							| 
									
										
										
										
											2015-08-18 20:17:11 +03:00
										 |  |  | 	} else if (pairing == QBluetoothLocalDevice::AuthorizedPaired) { | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 		pairingStatusLabel = tr("AUTHORIZED_PAIRED"); | 
					
						
							| 
									
										
										
										
											2015-08-18 20:17:11 +03:00
										 |  |  | 		pairingColor = QColor(Qt::blue); | 
					
						
							|  |  |  | 		enableSaveButton = true; | 
					
						
							| 
									
										
										
										
											2015-09-18 16:28:01 +02:00
										 |  |  | 		dialogStatusMessage = tr("Device %1 was paired and is authorized.").arg(remoteDeviceStringAddress); | 
					
						
							| 
									
										
										
										
											2015-08-18 20:17:11 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-18 20:17:11 +03:00
										 |  |  | 	// Find the items which represent the BTH device and update their state
 | 
					
						
							|  |  |  | 	QList<QListWidgetItem *> items = ui->discoveredDevicesList->findItems(remoteDeviceStringAddress, Qt::MatchContains); | 
					
						
							| 
									
										
										
										
											2019-06-06 11:45:02 +02:00
										 |  |  | 	QRegularExpression pairingExpression(QString("%1|%2|%3").arg(tr("PAIRED"), | 
					
						
							|  |  |  | 								     tr("AUTHORIZED_PAIRED"), | 
					
						
							|  |  |  | 								     tr("UNPAIRED"))); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-18 20:17:11 +03:00
										 |  |  | 	for (int i = 0; i < items.count(); ++i) { | 
					
						
							|  |  |  | 		QListWidgetItem *item = items.at(i); | 
					
						
							| 
									
										
										
										
											2019-06-06 11:45:02 +02:00
										 |  |  | 		QString updatedDeviceLabel = item->text().replace(pairingExpression, pairingStatusLabel); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-18 20:17:11 +03:00
										 |  |  | 		item->setText(updatedDeviceLabel); | 
					
						
							|  |  |  | 		item->setBackgroundColor(pairingColor); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-18 20:17:11 +03:00
										 |  |  | 	// Check if the updated device is the selected one from the list and inform the user that it can/cannot start the download mode
 | 
					
						
							|  |  |  | 	QListWidgetItem *currentItem = ui->discoveredDevicesList->currentItem(); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-18 20:17:11 +03:00
										 |  |  | 	if (currentItem != NULL && currentItem->text().contains(remoteDeviceStringAddress, Qt::CaseInsensitive)) { | 
					
						
							|  |  |  | 		if (pairing == QBluetoothLocalDevice::Unpaired) { | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 			dialogStatusMessage = tr("The device %1 must be paired in order to be used. Please use the context menu for pairing options.") | 
					
						
							|  |  |  | 						.arg(remoteDeviceStringAddress); | 
					
						
							| 
									
										
										
										
											2015-08-18 20:17:11 +03:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 			dialogStatusMessage = tr("The device %1 can now be used for connection. You can press the Save button.") | 
					
						
							|  |  |  | 						.arg(remoteDeviceStringAddress); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-18 20:17:11 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Update the save button and the dialog status message
 | 
					
						
							|  |  |  | 	ui->save->setEnabled(enableSaveButton); | 
					
						
							|  |  |  | 	ui->dialogStatus->setText(dialogStatusMessage); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void BtDeviceSelectionDialog::error(QBluetoothLocalDevice::Error error) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 	ui->dialogStatus->setText(tr("Local device error: %1.") | 
					
						
							|  |  |  | 				    .arg((error == QBluetoothLocalDevice::PairingError)? tr("Pairing error. If the remote device requires a custom PIN code, " | 
					
						
							|  |  |  | 											    "please try to pair the devices using your operating system. ") | 
					
						
							|  |  |  | 										       : tr("Unknown error"))); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-18 20:54:31 +03:00
										 |  |  | void BtDeviceSelectionDialog::deviceDiscoveryError(QBluetoothDeviceDiscoveryAgent::Error error) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QString errorDescription; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (error) { | 
					
						
							|  |  |  | 	case QBluetoothDeviceDiscoveryAgent::PoweredOffError: | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 		errorDescription = tr("The Bluetooth adaptor is powered off, power it on before doing discovery."); | 
					
						
							| 
									
										
										
										
											2015-07-18 20:54:31 +03:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 	case QBluetoothDeviceDiscoveryAgent::InputOutputError: | 
					
						
							| 
									
										
										
										
											2015-09-04 23:25:33 +01:00
										 |  |  | 		errorDescription = tr("Writing to or reading from the device resulted in an error."); | 
					
						
							| 
									
										
										
										
											2015-07-18 20:54:31 +03:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 		errorDescription = tr("An unknown error has occurred."); | 
					
						
							| 
									
										
										
										
											2015-07-18 20:54:31 +03:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 	ui->dialogStatus->setText(tr("Device discovery error: %1.").arg(errorDescription)); | 
					
						
							| 
									
										
										
										
											2015-07-18 20:54:31 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-29 19:43:00 -07:00
										 |  |  | extern QString markBLEAddress(const QBluetoothDeviceInfo *device); | 
					
						
							| 
									
										
										
										
											2017-11-13 22:46:42 +01:00
										 |  |  | extern QString btDeviceAddress(const QBluetoothDeviceInfo *device, bool isBle); | 
					
						
							| 
									
										
										
										
											2017-06-29 19:43:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | QString BtDeviceSelectionDialog::getSelectedDeviceAddress() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-11-13 22:46:42 +01:00
										 |  |  | 	if (!selectedRemoteDeviceInfo) | 
					
						
							|  |  |  | 		return QString(); | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-13 22:46:42 +01:00
										 |  |  | 	int btMode = ui->btMode->currentIndex(); | 
					
						
							|  |  |  | 	QBluetoothDeviceInfo *device = selectedRemoteDeviceInfo.data(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (btMode) { | 
					
						
							|  |  |  | 	case 0:		// Auto
 | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		return markBLEAddress(device); | 
					
						
							|  |  |  | 	case 1:		// Force LE
 | 
					
						
							|  |  |  | 		return btDeviceAddress(device, true); | 
					
						
							|  |  |  | 	case 2:		// Force classical
 | 
					
						
							|  |  |  | 		return btDeviceAddress(device, false); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QString BtDeviceSelectionDialog::getSelectedDeviceName() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-11-11 22:12:21 +01:00
										 |  |  | 	if (selectedRemoteDeviceInfo) | 
					
						
							| 
									
										
										
										
											2015-07-06 16:35:13 +03:00
										 |  |  | 		return selectedRemoteDeviceInfo.data()->name(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return QString(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-18 17:50:18 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-12 12:33:20 +01:00
										 |  |  | QString BtDeviceSelectionDialog::getSelectedDeviceText() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return formatDeviceText(getSelectedDeviceAddress(), getSelectedDeviceName()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QString BtDeviceSelectionDialog::formatDeviceText(const QString &address, const QString &name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (address.isEmpty()) | 
					
						
							|  |  |  | 		return name; | 
					
						
							|  |  |  | 	if (name.isEmpty()) | 
					
						
							|  |  |  | 		return address; | 
					
						
							|  |  |  | 	return QString("%1 (%2)").arg(name, address); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-18 17:50:18 +03:00
										 |  |  | void BtDeviceSelectionDialog::updateLocalDeviceInformation() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-07-18 21:01:40 +03:00
										 |  |  | 	// Check if the selected Bluetooth device can be accessed
 | 
					
						
							|  |  |  | 	if (!localDevice->isValid()) { | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 		QString na = tr("Not available"); | 
					
						
							| 
									
										
										
										
											2015-07-18 21:01:40 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Update the UI information
 | 
					
						
							|  |  |  | 		ui->deviceAddress->setText(na); | 
					
						
							|  |  |  | 		ui->deviceName->setText(na); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Announce the user that there is a problem with the selected local Bluetooth adapter
 | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 		ui->dialogStatus->setText(tr("The local Bluetooth adapter cannot be accessed.")); | 
					
						
							| 
									
										
										
										
											2015-07-18 21:01:40 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Disable the buttons
 | 
					
						
							|  |  |  | 		ui->save->setEnabled(false); | 
					
						
							|  |  |  | 		ui->scan->setEnabled(false); | 
					
						
							|  |  |  | 		ui->clear->setEnabled(false); | 
					
						
							|  |  |  | 		ui->changeDeviceState->setEnabled(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-18 17:50:18 +03:00
										 |  |  | 	// Set UI information about the local device
 | 
					
						
							|  |  |  | 	ui->deviceAddress->setText(localDevice->address().toString()); | 
					
						
							|  |  |  | 	ui->deviceName->setText(localDevice->name()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	connect(localDevice, SIGNAL(hostModeStateChanged(QBluetoothLocalDevice::HostMode)), | 
					
						
							|  |  |  | 		this, SLOT(hostModeStateChanged(QBluetoothLocalDevice::HostMode))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Initialize the state of the local device and activate/deactive the scan button
 | 
					
						
							|  |  |  | 	hostModeStateChanged(localDevice->hostMode()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Add context menu for devices to be able to pair them
 | 
					
						
							|  |  |  | 	ui->discoveredDevicesList->setContextMenuPolicy(Qt::CustomContextMenu); | 
					
						
							|  |  |  | 	connect(ui->discoveredDevicesList, SIGNAL(customContextMenuRequested(QPoint)), | 
					
						
							|  |  |  | 		this, SLOT(displayPairingMenu(QPoint))); | 
					
						
							|  |  |  | 	connect(localDevice, SIGNAL(pairingFinished(QBluetoothAddress, QBluetoothLocalDevice::Pairing)), | 
					
						
							|  |  |  | 		this, SLOT(pairingFinished(QBluetoothAddress, QBluetoothLocalDevice::Pairing))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	connect(localDevice, SIGNAL(error(QBluetoothLocalDevice::Error)), | 
					
						
							|  |  |  | 		this, SLOT(error(QBluetoothLocalDevice::Error))); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-18 20:51:45 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | void BtDeviceSelectionDialog::initializeDeviceDiscoveryAgent() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// Intialize the discovery agent
 | 
					
						
							|  |  |  | 	remoteDeviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(localDevice->address()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Test if the discovery agent was successfully created
 | 
					
						
							|  |  |  | 	if (remoteDeviceDiscoveryAgent->error() == QBluetoothDeviceDiscoveryAgent::InvalidBluetoothAdapterError) { | 
					
						
							| 
									
										
										
										
											2015-09-02 22:52:54 +03:00
										 |  |  | 		ui->dialogStatus->setText(tr("The device discovery agent was not created because the %1 address does not " | 
					
						
							|  |  |  | 					     "match the physical adapter address of any local Bluetooth device.") | 
					
						
							|  |  |  | 					    .arg(localDevice->address().toString())); | 
					
						
							| 
									
										
										
										
											2015-07-18 20:51:45 +03:00
										 |  |  | 		ui->scan->setEnabled(false); | 
					
						
							|  |  |  | 		ui->clear->setEnabled(false); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	connect(remoteDeviceDiscoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), | 
					
						
							|  |  |  | 		this, SLOT(addRemoteDevice(QBluetoothDeviceInfo))); | 
					
						
							|  |  |  | 	connect(remoteDeviceDiscoveryAgent, SIGNAL(finished()), | 
					
						
							|  |  |  | 		this, SLOT(remoteDeviceScanFinished())); | 
					
						
							| 
									
										
										
										
											2015-07-18 20:54:31 +03:00
										 |  |  | 	connect(remoteDeviceDiscoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)), | 
					
						
							|  |  |  | 		this, SLOT(deviceDiscoveryError(QBluetoothDeviceDiscoveryAgent::Error))); | 
					
						
							| 
									
										
										
										
											2015-08-18 20:51:10 +03:00
										 |  |  | } |