sometimes fails to switch wifi from ap mode to sta

I am using a TX1 along with NetworkManager to setup a access point, all goes ok.but when I want to switch from ap mode to sta mode, It goes wrong sometimes and I have no idea to fix it expect reboot the TX1.

here is the core part of shell code:
echo 0 > /sys/module/bcmdhd/parameters/op_mode
sed -i ‘/options bcmdhd op_mode=2/d’ /etc/modprobe.d/bcmdhd.conf

ifconfig wlan0 down
ifconfig wlan0 up

sleep 3
nmcli dev wifi con “$2” password “$3”
nmcli connection up $2

What I notice is that it fails to switch from ap to sta and therefore networkmanager throws an error likes: Error: No network with SSID ‘somessid’ found.

sometimes reboot can solve this, but It’s not a good way

fengz181x,

Would you hit this problem when using Ubuntu GUI? It looks like a known issue for NetworkManager. Could you also try to use latest NM?

sudo apt-get --only-upgrade install network-manager