SteffenH SteffenH - 4 months ago 31
Bash Question

dbus-send not working in shell script

I want Bluetooth tethering between my laptop (Debian 8) and my smartphone (Android).

At the arch linux wiki (https://wiki.archlinux.org/index.php/android_tethering#Tethering_via_Bluetooth) i found this command:

bus-send --system --type=method_call --dest='org.bluez' '/org/bluez/hci0/dev_C0_EE_FB_20_D7_00' org.bluez.Network1.Connect string:'nap'


When i execute it in the normal terminal everything works fine. For my purpose i need to call this command in a QT application. Because of this i created a shell script. But when executing the script nothing happens. Same result when calling the command inside a new shell (
sh
).

Does anybody have an idea how to get this working or another way? My normal terminal is the default Debian terminal, '
Root Terminal
'.

Thank you

Answer

I solved it using the QDBus object. First i create the following two methods:

QString MainWindow::getDBusInterface(){
    QString interface = "/org/bluez/hci0/dev";
    QStringList macParts = ui->selectedMac->text().toUpper().split(":");
    for (int i = 0; i < macParts.length(); i++){
        interface.append("_").append(macParts[i]); //MAC address to connect to from GUI
    }
    return interface;
}

and

QDBusMessage MainWindow::sendDBus(QString destination, QString path, QString interface, QString method, QList<QVariant> arguments){
    QDBusMessage response;


    QDBusConnection system = QDBusConnection::systemBus();
    if (!system.isConnected())
    {
        qFatal("Cannot connect to the D-Bus session bus.");
        return response;
    }

    QDBusMessage message2 = QDBusMessage::createMethodCall(destination, path, interface, method);

    message2.setArguments(arguments);


    // synchronous call (not recommended, blocking)
    response = QDBusConnection::systemBus().call(message2);
    qDebug() << "response is: " << response;

    return response;
}

Finally i call:

QString interface = getDBusInterface();

QList<QVariant> arguments;
arguments.append("nap");
QDBusMessage response = sendDBus("org.bluez",
                                     interface,
                                     "org.bluez.Network1",
                                     "Connect",
                                     arguments);