We have moved to a new Sailfish OS Forum. Please start new discussions there.
1

Connect to specific WiFi from command line

asked 2020-04-22 12:40:34 +0200

nas gravatar image

Hello,

I have two wifi access points A and B. When I am connected to B and go close to A, B has very weak signal and doesn't work fast / reliable. Since the SailfishX doesn't handle it's own to jump from B to A (it could be an option for that maybe?), I created an "App" in the desktop so it can run a shell script to connect to specific Access Point (one for A one for B). I tried as root:

# wpa_supplicant -B -i wlan0 -c <(wpa_passphrase "A" A_WIFI_PASS )

the result was I could not connect at any wifi, network restart from Utilities didn't work and I had to reboot my device.

Then I tried the following:

# iw wlan0 connect A and returns: command failed: Operation already in progress (-114)

The access points I am trying to connect to (A, B) are configured on my phone with WPA password / static IP.

Is there any way I can select from command line specific wifi to connect to? Does anyone have any idea about that?

edit retag flag offensive close delete

Comments

sailfishos uses connman. you can use it from dbus

coderus ( 2020-04-22 13:44:45 +0200 )edit
1

also for a reliable signals: often its enough to turn off and on wifi and sfos will reconnect to most strength point again.

coderus ( 2020-04-22 13:45:28 +0200 )edit

@coderus thank you for the info and the idea coderus! I found in tjc a solution about turning off and on the wifi using dbus, but I suppose it will be faster if you connect directly to the new wifi.

nas ( 2020-04-22 14:35:28 +0200 )edit

@coderus I did made some tries, I used the dbus commands from this post https://together.jolla.com/question/56437/i-would-like-to-toggle-internet-connectivity-via-terminal-with-dbus/ and I ended up with that:

/usr/bin/dbus-send --print-reply --type=method_call --system --dest=net.connman /net/connman/technology/wifi net.connman.Technology.SetProperty string:Powered variant:boolean:false

/usr/bin/dbus-send --print-reply --type=method_call --system --dest=net.connman /net/connman/technology/wifi net.connman.Technology.SetProperty string:Powered variant:boolean:true

It turns off and on the wifi but it doesn't seem to scan / connect to any wifi.. As far as I tried to see this dbus couldn't find anything to set specific WiFi

nas ( 2020-04-22 16:10:51 +0200 )edit

Just tested and it worked fine (as root).
Disconnected and connected to my hone network on its own (ANY, no specific, nerwork).

peterleinchen ( 2020-04-23 00:13:51 +0200 )edit

1 Answer

Sort by » oldest newest most voted
2

answered 2020-04-22 17:07:49 +0200

nas gravatar image

updated 2020-04-23 10:55:46 +0200

Well, after some hours of research on tjc and duckduckgo I kind of made it:

  1. if you don't have it installed connmanctl you can do it that way as root from command line: pkcon install connman-tools
  2. Then as root type connmanctl services
  3. In the second column you get the name of the WiFi (and cellular) (let's say A) and in third column you get something like wifi_1234567890a2_6087600f7e89c67_managed_psk
  4. As root use the following command: connmanctl connect wifi_1234567890a2_6087600f7e89c67_managed_psk and after 2 - 3 seconds connects to the WiFi access point A

This was the best way so far and worked for me, use it at your own risk. Unfortunately the "App" part it didn't work (yet).

UPDATE: 2020_04_23

The App part also worked! This is the howto:

  • I created directory "bin" in nemo home folder

  • I created a shell-script with the name: wifi-A.sh in the above "bin" directory and I wrote:

sh -c "echo ROOT_PASS | devel-su -c connmanctl connect wifi_1234567890a2_6087600f7e89c67_managed_psk  >  /dev/null 2>&1  "
# date >> ~/bin/date_log  # for debuging purposes, to see if the script runs or not. Uncomment to use it
  • I created an icon (64x64pixel png) and I saved it at: /home/nemo/bin/appicons/wifi-A.png

  • In the directory ~nemo/.local/share/applications/ I created a file: select-wifi-A.desktop

  • In the file I wrote:

[Desktop Entry]
Type=Application
Exec=sh -c '/home/nemo/bin/wifi-A.sh'
Name=WiFi-A
Icon=/home/nemo/bin/appicons/wifi-A.png
Comment="Connect to specific WiFi A"`

That's it ! You should be able to see an Icon with your icon (or maybe empty but with the test WiFi-A. Click it and watch your phone to disconnect to the B WiFi and connect to A in seconds!

Notes: in step 2 as you can see you have to use your root password, otherwise it does not work. This is not very wise for security reasons, but I know the risks and I am okey with them. the 'wifi_1234..._psk ' is from the first part of the answer. Use all these at your own risk!

edit flag offensive delete publish link more

Comments

Nice find. :)

peterleinchen ( 2020-04-23 00:15:14 +0200 )edit

Thank you peterleinchen ! Unfortunately the off/on wifi trick didn't work for me (XperiaX), and also it seems it needs quite many seconds for the whole procedure. My next try will be a shell script which find which access point of a specific list has the strongest signal and connect to that! I am thinking also to make kind of daemon to run always, but that might be affect battery consumption.

nas ( 2020-04-23 10:59:30 +0200 )edit

@nas How does your shell script "disconnect from A" and "connect to B"? It has only one connmanctl connect command.

addydon ( 2020-04-24 14:40:47 +0200 )edit
1

@addydon my script says to the connman to connect to the specific Access Point. I suppose connman after it gets my request it disconnects (if it is even connected) from the current wifi before it connects to the new one. It worked for me as I wrote it, it didn't need any extra commands for the disconnect part. 11 days now that I use my "App" I am very satisfied and I happily smile every time I use it for my (small) achievement ;)

nas ( 2020-05-04 15:56:38 +0200 )edit
Login/Signup to Answer

Question tools

Follow
4 followers

Stats

Asked: 2020-04-22 12:40:34 +0200

Seen: 520 times

Last updated: Apr 23 '20