How does smart configuration of Wifi IP Camera work, using an App?

I have seen various wifi enabled devices, mostly IP cameras and Home automation systems, be configured using a mobile app. I would like to know how that is achieved. I have read and understood most points except the below two. I would appreciate some help or a general pointer as to where I should dig. :)

Before that, Let me set the context for the question. There are various Wifi enabled devices in the market which can be configured to connect to a wifi of our choice, using an Android or iPhone App. The phone with the app is first connected to the Wifi of choice, and the Serial No. of the device to be configured is entered into the app, or a QR Code is scanned. The configuration process takes about a couple of seconds and the device is configured to connect to the same wifi that the phone is connected to, at the time of configuration.

My questions:

1) How does the app communicate with the device? If it was Bluetooth, then it requires manual pairing, right? IR can be used, but not all phones have that functionality also. The device can setup an open hidden wifi, that the app can connect to and share the configuration details. If so, can an App disconnect from a wifi and connect to the another?

2) How does the App read the wifi SSID, Passphrase etc from the connected wifi network, so that it can configure the device?

Any help is greatly appreciated. I will list a couple of devices that I have come across that employs similar configuration mechanism.