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

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.

Related

Dubs Connman wifi connect Qt

I'am working on an imx6, and i'am trying to connect to a wifi network through Dbus with a Qt application.
The application connect correctly to connman via Dbus and i recieve correctly the wifi services.
The problem is that when i try to connect to a wiif network i catch this error :
"Method "Connect" with signature "ss" on interface "net.connman.Service" doesn't exist
The code that i'am using in Qt application to coonect to a wifi network is:
QDBusInterface *iface =
new QDBusInterface("net.connman","/net/connman/technology/wifi","net.connman.Service",QDBusConnection::systemBus());
if (!iface->isValid())
{
qDebug() << Q_FUNC_INFO << "Fail to connect to the Connman Technology interface: " << QDBusConnection::systemBus().lastError().message();
}
QDBusReply<void> reply = iface->call("Connect","/net/connman/service/wifi_88da1a4db14c_41684179_managed_psk","password");
if (!reply.isValid())
{
qDebug() << "Call connect result: " << reply.error().message();
}
When i try to connect to the wifi network with shell commands using connmanctl it works like a charm.
I had the same problem on imx6. The solution which works for me is creating a configuration file for network before invoking Connect method.
The file should be in /var/lib/connman and name [SSID].config .
File content:
[service_wifi_PUT_SERVICE_NAME]
Name = PUT_SSID
Type = wifi
Passphrase = PUT_PASSWORD
And try connecting in this way:
QDBusInterface *iface = new QDBusInterface("net.connman", QString{"/net/connman/service/%1"}.arg(SERVICE_NAME), "net.connman.Service", QDBusConnection::systemBus());
QDBusReply<void> reply = iface->call("Connect");
if(!reply.isValid() {
...

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.

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
}

Create a RFCOMM Server using Qt for symbian

I´m new on Qt, Symbian devices and Bluetooth.
I have to set up a RFCOMM server to receive connection from a bluetooth device (its a pinpad) that only support SPP profile.
I searched on google and found some examples, like this: http://doc.qt.nokia.com/qtmobility/btchat.html
Tried everything but I can´t connect them. Both devices was paired, but when I try to connect it fails.
When I asked to the manufacturer, they said that it was happening because the SPP Server on my celphone was not avaiable to listen for incoming connections.
I´m creating the RFCOMM server and registering the service just like the example, but it still not working.
Someone can help me?
I'm using Qt with QtMobility 1.2.0 and my cellphone is a Nokia 500 (Symbian^3).
Here is my code:
void bluetooth::startServer()
{
QString deviceName;
QBluetoothLocalDevice localDevice;
if (rfcommServer)
return;
localDevice.powerOn();
localDevice.setHostMode(QBluetoothLocalDevice::HostDiscoverable);
deviceName = localDevice.name();
rfcommServer = new QRfcommServer(this);
connect(rfcommServer, SIGNAL(newConnection()), this, SLOT(vConectou())) ;
if( rfcommServer->listen(localDevice.address(),
quint16(rfcommServer->serverPort()) ) )
emit vExibeMsg("Listening");
else
emit vExibeMsg("Error");
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceRecordHandle,
(uint)0x00010010);
QBluetoothServiceInfo::Sequence classId;
classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort));
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId);
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, tr("Test Server"));
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceDescription,
tr("Test Bluetooth"));
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider, deviceName );
serviceInfo.setServiceUuid(QBluetoothUuid(QBluetoothUuid::Rfcomm));
serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList,
QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup));
QBluetoothServiceInfo::Sequence protocolDescriptorList;
QBluetoothServiceInfo::Sequence protocol;
/*
protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap));
protocolDescriptorList.append(QVariant::fromValue(protocol));
protocol.clear();
protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm))
<< QVariant::fromValue(quint8(rfcommServer->serverPort()));
protocolDescriptorList.append(QVariant::fromValue(protocol));
protocol.clear();
protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort));
protocolDescriptorList.append(QVariant::fromValue(protocol));
*/
protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm))
<< QVariant::fromValue(quint8(rfcommServer->serverPort()));
protocolDescriptorList.append(QVariant::fromValue(protocol));
serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
protocolDescriptorList);
if( serviceInfo.registerService() )
{
emit vExibeMsg("Waiting for connections...");
}
else
{
emit vExibeMsg("Error to create the service");
}
}

Qt: How to use an accesspoint throughout the application?

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

Resources