Connect to different WiFi network at Runtime

I am trying to change WiFi networks at runtime inside a container that runs in a Raspberry Pi. The wifi settings already exist in the /etc/NetworkManager/system-connections/ folder. I am having difficulties understanding how to use DBUS to point to the new connection settings and activate the connection. I have successfully managed to list the interfaces, but I am not sure how to proceed.
Should I use the org.freedesktop.NetworkManager or the /fi/w1/wpa_supplicant1 to connect?
Any suggestions are appreciated.
PS: I am using Node and the ‘dbus-native’ package.

Hello, is this a one off thing, or something that you would be looking at doing semi regularly? Does the changing of wifi networks have to be controlled from within the container? Thanks

It will most likely be a one-off thing; container boots and then connects to a known network. It needs to start within the container. Thx!

I see, just for a little more context as it may help us out and depending on the scenario there may be a simpler way to achieve the desired result - what is the use case if you don’t mind me asking? The device is booted, and connects to a known network - but then it has to connect to a different network post boot, and this must be triggered from within a container? Why is this? Thanks!

Thank you for your reply. The container checks if there is an active WiFi connection and if not, is tasked to boot an external LTE module that has its own WiFi. In most of the cases, that I am testing there will not be an active WiFi connection, so the container will have to somehow boot the LTE, wait until it is initialized, and then connect to its WiFi.

Hi, just reading up on this. As you are using NodeJS then you may check out the dbus-native library. Here is an example project of ours that uses it: https://github.com/balena-io-modules/nm-api, it might be useful for your use case.

Thx Alex; I have tried to use nm-api library but in my installation, it doesn’t seem to work.

Hi, can you elaborate on that. What sort of problems are you running into using the dbus-api ?

I am starting nm-api as a node module from nm-api and I am getting TypeError: nm.init is not a function

Just looked into the modukle and I am not quite sure what the status is. Apparently it has not been touched for two years and I could not find it in NPM. Also the reason you can not find init is probably due to the documentation not being quite correct for the module use case.
From what I see the module exports:

export {
	createHttpServer,
	NetworkManager
};

on the top level and the NetworkManager class has a function init…
Given that you can either try to figure out the right use of the module or just use it to figure out how it uses dbus-native to activate a connection and adapt that for your own purpose…

Thank @samothx and @alexgg ! I didn’t pay attention to that. Thanks to your suggestion I have written the following snippet that allows me to change from my Node.js container:

const nmApi = require('nm-api');
async function connectNetwork(network) {
     const nm = new nmApi.NetworkManager;
     let service = await nm.init();
     let accessPoints = await service.listNearbyNetworks();
     logger.info(accessPoints);

     service.connectNetwork(network)
         .then((result) => console.log(result))
         .catch((err) => {
             logger.error('Connection failed with error', err);
         });
 } 
 // And to call it: 

let network = {'mode': 'infrastructure', 'passphrase': 'pass', 'ssid':'wifi-ssid'};
let _ = this.connectNetwork(network);

Great, glad it worked!