I want to search bluetooth devices available and list them but the code I am following from documenation doesn't work.
BluetoothDevices::BluetoothDevices(QObject *parent) : QObject(parent)
{
}
// search Bluetooth devices
void BluetoothDevices::startDeviceDiscovery()
{
qDebug() << "Bluetooth discovery started";
// Create a discovery agent and connect to its signals
//QBluetoothDeviceDiscoveryAgent *discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
this, SLOT(deviceDiscovered(QBluetoothDeviceInfo)));
// Start a discovery
discoveryAgent->start();
}
void BluetoothDevices::deviceDiscovered(const QBluetoothDeviceInfo &device)
{
qDebug() << "Found new device:" << device.name() << '(' << device.address().toString() << ')';
}
In main window I start the search:
void MainWindow::on_pushButtonSearchBluetooth_clicked()
{
bluetoothDevices.startDeviceDiscovery();
}
I expect the deviceDiscovered() slot to get called with the device names as it gets found but this slot never gets called. I never hear anything back after initiating discovery. When I do search bluetooth devices manually from control panel, it does find couple of laptops. What else do I need to do to find them through my app?
I am using windows 7 and Qt 5.5.
I have the same problem. You can connect signal
connect(m_discoveryAgent, SIGNAL(error(QBluetoothServiceDiscoveryAgent::Error)), this, SLOT(error(QBluetoothServiceDiscoveryAgent::Error))); and you can see that slot emitted with error InvalidBluetoothAdapterError.
I try it on WIN7 and WIN10 and it not worked. In Ubuntu the same code work properly.
It seems that bluetooth not supported in windows platforms.
Currently, the API is supported on the following platforms: Android, iOS, Linux (BlueZ 4.x/5.x) and OS X.
from http://doc.qt.io/qt-5/qtbluetooth-index.html
Related
I'm trying to use Qt Bluetooth on Qt 5.8 and WinRT. I understand it's something that MUST go wrong one way or another but I'd like to avoid mixing Qt and native Windows APIs. I'm pretty sure I'm missing something super obvious right now.
const QLowEnergyCharacteristic characteristic = m_service->characteristic(
QBluetoothUuid(QBluetoothUuid::GlucoseMeasurement));
if (!characteristic.isValid()) {
break;
}
m_notificationDesc = characteristic.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
if (m_notificationDesc.isValid()) {
m_service->writeDescriptor(m_notificationDesc, enable_notification);
}
Where enable_notification is QByteArray::fromHex("0100");.
After calling writeDescriptor, I get QLowEnergyService::DescriptorWriteError and an instant disconnect. Any ideas?
I have two programs doing IPC using QLocalSocket & QLocalServer, I have managed to implement the connection and send the data successfully.
However, for the QLocalServer side, how can I know a connection is ended by client (like the client program quit.)?
So that I could release the resource of corresponding socket on server side?
You can connect the void QLocalSocket::disconnected() signal from QLocalHost to a slot in your class:
connect(mySocket, &QLocalSocket::disconnected, this, &MyClass::socketDisconnected, Qt::QueuedConnection);
Or you can connect to the stateChange (for more detailed version), something like:
// Connect like this
connect(mySocket, &QLocalSocket::stateChanged, this, &MyClass::socketNewState, Qt::QueuedConnection);
// Implement a slot that handles the various states...
MyClass::socketNewState(QLocalSocket::LocalSocketState socketState)
{
qDebug() << "New state is " << socketState << endl;
}
I'm trying to perform a network connectivity check whenever user opens the app or whenever app comes in foreground. Below is the sample code
void ApplicationUI::onFullscreen()
{
qDebug()<<"Application has entered foreground";
QNetworkConfigurationManager mgr;
QList<QNetworkConfiguration> activeConfigs = mgr.allConfigurations(QNetworkConfiguration::Active);
if (activeConfigs.count() > 0)
{
qDebug()<<"Has Internet connection";
}
else
{
qDebug()<<"No Internet connection";
}
}
This always prints Has Internet connection even when the network connection is off. Any ideas?
You can use QNetworkConfigurationManager.isOnline().
QNetworkConfigurationManager mgr;
mgr.isOnline();
If you want to get notified about changes of the online state then you also can connect to the QNetworkConfigurationManager::onlineStateChanged(bool isOnline) signal.
connect(mgr, SIGNAL(onlineStateChanged(bool)), this, SLOT(onOnlineStateChanged(bool)));
I'm tracking a log file that is changed by another application. In linux I receive the fileChanged signal correctly as soon as the other application changes the file. In windows QFileSystemWatcher doesn't emit any fileChanged signal until the other application is closed.
I have tried to open the log with notepad to make sure is actually been changed and as soon as the notepad open the log ,QFileSystemWatcher sends the fileChanged signal.
My code:
void LogLoader::createFileWatcher()
{
if(fileWatcher != NULL) delete fileWatcher;
fileWatcher = new QFileSystemWatcher(this);
connect(fileWatcher, SIGNAL(fileChanged(QString)),
this, SLOT(prepareLogWorker(QString)));
if(fileWatcher->addPath(logPath))
{
qDebug() << "LogLoader: "<< "FileWatcher linked.";
}
}
void LogLoader::prepareLogWorker(QString path)
{
//Added this just in case because I read it as solution
//in other question. But in my case the file is not removed.
if (!fileWatcher->files().contains(path))
{
fileWatcher->addPath(path);
}
QTimer::singleShot(1000, this, SLOT(sendLogWorker()));
}
Am I doing something wrong? Is there any other solution than checking
the file manually from time to time?
I've installed gtkmm3 via homebrew. My project links and builds without errors but never opens a window. xQuartz/X11 fires up upon successful build as well. It just seems to hang during the Gtk::Application::create() call. I've included my code below. Building on Xcode 5.1. Any help is much appreciated.
Thanks
#include <iostream>
#include <gtkmm-3.0/gtkmm.h>
int main(int argc, char * argv[])
{
std::cout << "Creating Application" << std::endl;
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "some.thing.here");
std::cout << "Creating Window" << std::endl;
Gtk::Window window;
std::cout << "Setting window title" << std::endl;
window.set_title("Window One");
std::cout << "Running App" << std::endl;
return app->run(window);
}
Gtk::Application::create() seems to hang because X11 isn't responding to it's request for a window. In it's current (default I assume) state only root can request a window.
Ideal solution (what worked best for me):
Go to Product > Scheme > Edit Scheme in the main menu of XCode. Make sure your current scheme (or whatever your dev scheme is) is selected in the fly out menu. On the modal that opens there are a few radio buttons. Select the 'run as root' option. Now X11 should respond to the request for the window.
Another solution:
Compile program and run with sudo.
An even more complex solution but if you intend to eventually let someone use this program via ssh...
Use xhost to add a user and enable ssh forwarding so you can run the compiled version via ssh without sudo. There are many docs explaining how to do this so I won't put the particulars here.
One other Note
XCode generates a main function with const char argv. Gtk::Application::create() won't take a const char argv. Remove const from main's argv and everything works.