Qt: How to use an accesspoint throughout the application? - qt

I'm developing an application for Symbian S60 phones using the Qt Nokia SDK, which sends requests and receives responses from a webservice in every view i have.
The problem with this, is that it always asks the user to choose a accesspoint.
So what i want is to choose an accesspoint when the application starts, and use that throughout the application.
So i found this example: http://wiki.forum.nokia.com/index.php/How_to_set_default_access_point_using_Qt_Mobility_APIs
but i got following error:
undefined reference to 'QtMobility::QNetworkConfigurationManager::QNetworkConfigurationManager(QObject*)
i'm also getting more of these errors from other classes from QMobillity, like:
undefined reference to 'QtMobility::QNetworkSession::open()
.pro file:
CONFIG += mobility
MOBILITY += bearer
header:
#include <qmobilityglobal.h>
#include <QtNetwork>
#include <QNetworkSession>
#include <QNetworkConfigurationManager>
QTM_USE_NAMESPACE;
cpp file:
QNetworkConfigurationManager manager;
const bool selectIap = (manager.capabilities()& QNetworkConfigurationManager::CanStartAndStopInterfaces);
QNetworkConfiguration defaultIap = manager.defaultConfiguration();
if(!defaultIap.isValid() && (!selectIap && defaultIap.state() != QNetworkConfiguration::Active))
{
qDebug() << "Network access point NOT found";
// let the user know that there is no access point available
msgBox->setText(tr("Error"));
msgBox->setInformativeText(tr("No default access point available"));
msgBox->setStandardButtons(QMessageBox::Ok);
msgBox->setDefaultButton(QMessageBox::Ok);
msgBox->topLevelWidget();
msgBox->exec();
}
else
{
qDebug() << "Network access point found and chosen";
}
session = new QNetworkSession(defaultIap,this);
session->open();
Anyone got an idea of what could be wrong?

Have you tried adding this to the .PRO file?
CONFIG += network

Related

Qt How to properly connect to a phone programmatically (Bluetooth A2DP, AVRCP, HSP, HFP) in Linux

I am trying to develop an application that uses bluez stack along with pulseaudio and ofono in order to connect to a phone and achieve tasks such as media playback (A2DP), media control (AVRCP), and handsfree-based telephony (HFP). When I connect to my phone through bluetoothctl, it automatically connects to all the available profiles, so using all profiles A2DP, AVRCP, HFP through my program is achievable. If I don't connect to my phone using bluetoothctl, handsfree /HFP modem is not enabled/powered in ofono.
However, when I use QBluetoothSocket in Qt and connect using a profile, there is always a profile that is not connected. For example connecting to Handsfree profile, telephony works, but the media control does not work. In short, I want to be able to connect to bluetooth as bluetoothctl does. What I have in Qt is as follows (in short):
static const QList<QBluetoothUuid> audioUuids = QList<QBluetoothUuid>()
<< QBluetoothUuid::HeadsetAG
<< QBluetoothUuid::AV_RemoteControlTarget;
..
void BtConnection::setConnection(int index)
{
if(m_bluetoothSocket == nullptr) {
m_bluetoothSocket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
qDebug() << "Created Bluetooth Socket";
}
if(m_bluetoothSocket != nullptr) {
connect(m_bluetoothSocket, SIGNAL(connected()), this, SLOT(connected()));
connect(m_bluetoothSocket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(m_bluetoothSocket, SIGNAL(error(QBluetoothSocket::SocketError)),
this, SLOT(connectionError(QBluetoothSocket::SocketError)));
}
m_device = get(index);
// Check if an element in m_device.serviceUuids() match with an element in audioUuids
QList<QBluetoothUuid>::const_iterator uuid;
for (uuid = audioUuids.begin(); uuid != audioUuids.end(); ++uuid) {
if(m_device.serviceUuids().indexOf(*uuid) > 0) {
// This device supports one of the uuids we have scanned for
if(m_bluetoothSocket != nullptr) {
qDebug() << "*****Connecting... " << *uuid;
m_bluetoothSocket->connectToService(m_device.address(), *uuid);
return;
}
}
}
qDebug() << "*****Cannot connect to service...";
}
I would be willing to post more of the code if this is not clear to you. Any help is greately appreciated on how to connect to bluetooth with Qt as bluetoothctl does.
Not a direct answer but you might want to check KDE's KDEConnect project. It already does what you are looking for and might either be a source of inspiration or you could contribute to the project.

How to get name of WiFi using QT quick application on Android

I am trying to develop a code to get name of wifi connected to my android phone. My code sample is
QStringList WiFisList;
QNetworkConfiguration cfg;
QNetworkConfigurationManager ncm;
auto nc = ncm.allConfigurations();
for (auto &x : nc)
{
qDebug()<< "CHECK1 " << x.bearerType();
if (x.bearerType() == QNetworkConfiguration::BearerWLAN)
{ qDebug ()<<"CHECK2";
qDebug() <<"WIFI is"<<x.name();
}
}
Output of this code is just returning me:
(int main(int, char)): WIFI is "WiFi" but my expected output is ASUS_XOOTD
How can I get this as output? Is something missing in my code?
check that your qt version is supporting Bearer extension for android.

undefined reference to `_imp___ZN15QSerialPortInfo14availablePortsEv'

I am working on simple app on windows to scan the serial port.
Error:
undefined reference to `_imp___ZN15QSerialPortInfo14availablePortsEv'
My Code:
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
// do something
}
What does the error mean and how to fix it.
i only needed to add QT += serialport in my project (appname.pro) file.

QtDBus Simply Example With PowerManager

I'm trying to use QtDbus to communicate with interface provided by PowerManager in my system. My goal is very simple. I will be writing code which causes my system to hibernate using DBus interface.
So, I installed d-feet application to see what interfaces DBus is available on my system, and what I saw:
As we see, I have a few interfaces and methods from which I can choose something. My choice is Hibernate(), from interface org.freedesktop.PowerManagment
In this goal I prepared some extremely simple code to only understand mechanism. I of course used Qt library:
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QStringList>
#include <QtDBus/QtDBus>
#include <QDBusInterface>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
if (!QDBusConnection::sessionBus().isConnected()) {
fprintf(stderr, "Cannot connect to the D-Bus session bus.\n"
"To start it, run:\n"
"\teval `dbus-launch --auto-syntax`\n");
return 1;
}
QDBusInterface iface("org.freedesktop.PowerManagement" ,"/" , "" , QDBusConnection::sessionBus());
if(iface.isValid())
{
qDebug() << "Is good";
QDBusReply<QString> reply = iface.call("Methods" , "Hibernate");
if(reply.isValid())
{
qDebug() << "Hibernate by by " << qPrintable(reply.value());
}
qDebug() << "some error " << qPrintable(reply.error().message());
}
return 0;
}
Unfortunately I get error in my terminal:
Is good
some error Method "Methods" with signature "s" on interface "(null)" doesn't exist
So please tell me what's wrong with this code? I am sure that I forgot some arguments in function QDBusInterface::call() but what ?
When creating interface you have to specify correct interface, path, service. So that's why your iface object should be created like this:
QDBusInterface iface("org.freedesktop.PowerManagement", // from list on left
"/org/freedesktop/PowerManagement", // from first line of screenshot
"org.freedesktop.PowerManagement", // from above Methods
QDBusConnection::sessionBus());
Moreover, when calling a method you need to use it's name and arguments (if any):
iface.call("Hibernate");
And Hibernate() doesn't have an output argument, so you have to use QDBusReply<void> and you can't check for .value()
QDBusReply<void> reply = iface.call("Hibernate");
if(reply.isValid())
{
// reply.value() is not valid here
}

How to start video-suite in MeeGo / Nokia N9 from Qt code?

I am having problems with launching Nokia's own video player from my application that I just don't seem to be able to solve.
My first attempt included calling
Qt.openUrlExternally(url)
from QML and that seemed to do the trick just fine, except that it opened the browser every time and used it instead of the video-suite (native player).
Next I tried cuteTube -approach where I start new process like this:
QStringList args;
args << url;
QProcess *player = new QProcess();
connect(player, SIGNAL(finished(int, QProcess::ExitStatus)), player, SLOT(deleteLater()));
player->start("/usr/bin/video-suite", args);
That worked, except that it required video-suite to be closed upon calling player->start, otherwise it did nothing.
My third attempt involved starting the video-suite via QDBus, but that didn't work any better:
QList<QVariant> args;
QStringList urls;
urls << url;
args.append(urls);
QDBusMessage message = QDBusMessage::createMethodCall(
"com.nokia.VideoSuite",
"/",
"com.nokia.maemo.meegotouch.VideoSuiteInterface",
"play");
message.setArguments(args);
message.setAutoStartService(true);
QDBusConnection bus = QDBusConnection::sessionBus();
if (bus.isConnected()) {
bus.send(message);
} else {
qDebug() << "Error, QDBus is not connected";
}
The problem with this is that it requires video-suite to be up and running - autoStartService parameter didn't help either. If video-suite isn't running already, the call opens it just fine but, alas, no video starts to play.
Eventually I tried using also VideoSuiteInterface, but even having the program compile with it seemed to be difficult. When I eventually managed to compile and link all relevant libraries, the results didn't differ from option 3 above.
So, is there a way to use either VideoSuiteInterface directly or via DBus so that it would start video playback regardless of the current state of the application?
The solution was actually simpler than I really thought initially; the VideoSuiteInterface -approach worked after all. All it took was to use it properly. Here are the full sources should anyone want to try it themselves.
player.h:
#ifndef PLAYER_H
#define PLAYER_H
#include <QObject>
#include <maemo-meegotouch-interfaces/videosuiteinterface.h>
class Player : public QObject {
Q_OBJECT
private:
VideoSuiteInterface* videosuite;
public:
Player(QObject *parent = 0);
Q_INVOKABLE void play(QString url);
};
#endif // PLAYER_H
player.cpp:
#include "player.h"
#include <QObject>
#include <QStringList>
#include <QtDeclarative>
Player::Player(QObject *parent) : QObject(parent) {}
void Player::play(QString url) {
QList<QVariant> args;
QStringList urls;
urls << url;
args.append(urls);
videosuite = new VideoSuiteInterface();
videosuite->play(urls);
}
In addition you may want to connect some signals to make the UI more responsive, but basically that should do the trick.
Finally, you need to remember to add following to your .pro file and you are good to go:
CONFIG += videosuiteinterface-maemo-meegotouch

Resources