Detect if wifi is connected


#1

I want to detect if the host os connman has connected to a wifi AP.

I am using dbus, as reverse engineered from the resin-wifi-connect:

DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host_run/dbus/system_bus_socket
dbus-send --system --dest=net.connman --print-reply /net/connman/technology/wifi net.connman.Technology.GetProperties

but i’m getting the reply:

Error org.freedesktop.DBus.Error.ServiceUnknown: The name net.connman was not provided by any .service files

Any ideas?


#2

Which device and which host OS version are you using? Connman is on the resinOS 1.x series, while resinOS 2.x has NetworkManager. I’m guessing you are using the latter, that’s why the issue you experience.

Just tried your version of the request on a Raspberry Pi 3 with host OS 1.24.1 (which uses Connman), and the result I get is:

method return sender=:1.3 -> dest=:1.10 reply_serial=2
   array [
      dict entry(
         string "Name"
         variant             string "WiFi"
      )
      dict entry(
         string "Type"
         variant             string "wifi"
      )
      dict entry(
         string "Powered"
         variant             boolean true
      )
      dict entry(
         string "Connected"
         variant             boolean true
      )
      dict entry(
         string "Tethering"
         variant             boolean false
      )
   ]

Also tried on the 2.x line, which needs some NetworkManager magic:

Put together from the NetworkManager DBus reference and this blogpost: Dbus Tutorial - Fun with Network Manager, it should work with something like:

DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host/run/dbus/system_bus_socket \
dbus-send \
 --system \
 --print-reply \
 --dest=org.freedesktop.NetworkManager \
 /org/freedesktop/NetworkManager \
 org.freedesktop.DBus.Properties.GetAll \
 string:"org.freedesktop.NetworkManager"

and look for the State variable, or call it directly with

DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host/run/dbus/system_bus_socket \
dbus-send \
 --system \
 --print-reply \
 --dest=org.freedesktop.NetworkManager \
 /org/freedesktop/NetworkManager \
 org.freedesktop.DBus.Properties.Get \
 string:"org.freedesktop.NetworkManager" \
 string:"State"

State can take these values, and value 70 meaning global connectivity.

NetworkManager should have a lot more tricks up its sleeve, so would love to hear what you make in the end!

There’s also some notes in this other forum post how to use Node.js with DBus for networking, and there’s of course binding in a bunch of other languages to make it easier to tie it in.


#3

I’m using Resin OS 1.19.0 and beaglebone-debian:jessie-20161228

weird.

although I wasn’t aware you were changing to networkmanager on 2.xx so I guess i’ll upgrade and try again using that…


#4

For clarity, which BeagleBone are you using? Judging by the base image, you use BeagleBone Black, which only has Ethernet by default. If that one, how do you add a wifi interface to it?


#5

Beaglebone Black

Using this cape:

http://www.each-cs.com/ENG/internet_hub.html

Works great - I currently drop .service files into /host/var/lib/connman. I guess Networkmanager works similarly?


#6

I’ve just tried the currently downloadable resinOS 1.24 for the BeagleBone Black with a Raspberry Pi wifi dongle and the command you mentioned work just fine, with this result:

root@beaglebone-1b16c8c:/# DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host/run/dbus/system_bus_socket dbus-send --system --dest=net.connman --print-reply /net/connman/technology/wifi net.connman.Technology.GetProperties
method return sender=:1.4 -> dest=:1.11 reply_serial=2
   array [
      dict entry(
         string "Name"
         variant             string "WiFi"
      )
      dict entry(
         string "Type"
         variant             string "wifi"
      )
      dict entry(
         string "Powered"
         variant             boolean true
      )
      dict entry(
         string "Connected"
         variant             boolean true
      )
      dict entry(
         string "Tethering"
         variant             boolean false
      )
   ]

Is it possible for you to reflash the device with the latest resinOS and try again? It’s recommended to upgrade from 1.19 anyways.

x x x

On your later question, NetworkManager is somewhat different, so might need to modify how you are doing things. In resinOS 2.x you can set up connections by adding config files to the resin-boot partition’s system-connections directory (an example is included there all the time), and the DBus calls are different as mentioned above.

If you want to test 2.x, it’s available from resinstaging, our staging service which runs the latest beta services and images. You have to sign up there separately, and things are not expected to always work on staging (as opposted to the main production service)


#7

For anyone that’s interested - I resolved the problem by issuing the commands all on one line:

DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host_run/dbus/system_bus_socket dbus-send --system --dest=net.connman --print-reply /net/connman/technology/wifi net.connman.Technology.GetProperties


#8

Yes, indeed that’s a single command for dbus-send with an environment variable (DBUS_SYSTEM_BUS_ADDRESS). Maybe have to clarify somehow, that it is how it works in the Linux shell, there’s no line break after the env var (these docs and forums break the lines, that’s why I prefer putting code in with the tripple-tick as a code block, rather than single tick as inline code (see the Markdown Cheat Sheet. Probably that’s what I’ve missed in your original comment too?

You can also do, by the way setting that env var with export:

export DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host/run/dbus/system_bus_socket

and then all the following commands in that shell will be passed that parameters, so you can just run dbus-send ...