espressif / esp-idf-provisioning-ios

Apache License 2.0
130 stars 62 forks source link

Network connection was lost when trying to reconnect to the ESP device after disconnecting #55

Open shushant43 opened 1 year ago

shushant43 commented 1 year ago

Problem

In our app, before we actually provision the device, we have to make an API call hence we have to disconnect from the ESPDevice and after making the api call, we have to connect to it again. But when we try to connect again with delegate while providing the correct POP, it doesn't connect. It fails to initialise a session because the network connection was lost.

To Reproduce

  1. Disconnect the device
  2. Connect to the device again

Expected behavior The device should be connected again without any issues

iOS information:

Provisioning information:

shushant43 commented 1 year ago

Can anyone help us out with this?

vikas-chandra-mnnit commented 1 year ago

Hi @shushant43, can you please explain what kind of API call you need to make during provisioning?

shushant43 commented 1 year ago

@vikas-chandra-mnnit It’s an API call to update a few things related to our workflow.

vikas-chandra-mnnit commented 1 year ago

@shushant43 Is it over BLE or simply a network call?

shushant43 commented 1 year ago

@vikas-chandra-mnnit A network call

vikas-chandra-mnnit commented 1 year ago

@shushant43 Can you make the network call without disconnecting from the BLE device?

shushant43 commented 1 year ago

@vikas-chandra-mnnit No. Because at that stage the iOS device is connected to the BLE device’s access point.

vikas-chandra-mnnit commented 1 year ago

@shushant43 Is it possible for you to share the code snippet here?

shushant43 commented 1 year ago

@vikas-chandra-mnnit

Disconnecting from the ESP device and checking for network

espDevice.disconnect()
checkForNetwork()

This works fine, the ESP device is disconnected and after the network reachability is restored, the network call is made.

After getting back the response from API, try to reconnect to the ESP device inside the success delegate

.observe(on: MainScheduler.instance)
            .subscribe(onNext: { response in
                self.espDevice.security = Utility.shared.espAppSettings.securityMode //secure
                self.espDevice.connect(delegate: self) { status in
                    DispatchQueue.main.async {
                        switch status {
                        case .connected:
                            self.startProvisioning()
                        case let .failedToConnect(error):
                            self.step2FailedWithMessage(message: error.description)
                        default:
                            ()
                        }
                    }
                }
            }, onError: { error in
                self.step2FailedWithMessage(message: error.localizedDescription)
            })

Provided POP via the delegate

extension StatusViewController: ESPDeviceConnectionDelegate {
    func getProofOfPossesion(forDevice: ESPDevice, completionHandler: @escaping (String) -> Void) {
        completionHandler("XXXXXX")
    }
}
vikas-chandra-mnnit commented 1 year ago

@shushant43 Please share the code for checkForNetwork() method.

shushant43 commented 1 year ago

@vikas-chandra-mnnit

let monitor = NWPathMonitor()

override func viewDidLoad() {
        super.viewDidLoad()

        monitor.pathUpdateHandler = { path in
            let ssid = self.getWiFiName() ?? ""
            if path.status == .satisfied && ssid != "" && !ssid.contains("XXXXX") {
                self.monitor.cancel()
                //API Calls based on some conditions
            }
        }
    }
    private func checkForNetwork() {
        let queue = DispatchQueue(label: "Monitor")
        monitor.start(queue: queue)
    }
shushant43 commented 1 year ago

@vikas-chandra-mnnit What do you think?

vikas-chandra-mnnit commented 1 year ago

@shushant43 Have you tried not disconnecting the BLE device and calling the checkNetwork() method? What happens in that case? I don't see any dependency between the two.