Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
shushant43 opened this issue Jul 12, 2022 · 13 comments

Comments

@shushant43
Copy link

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:

  • Device type: iPhone
  • Device model: 13 Pro Max
  • iOS version: 15.5

Provisioning information:

  • ESPProvision iOS SDK version: 2.0.16
  • Device type: BLE
  • Security: Secure
@shushant43
Copy link
Author

Can anyone help us out with this?

@vikas-chandra-mnnit
Copy link
Contributor

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

@shushant43
Copy link
Author

shushant43 commented Jul 18, 2022

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

@vikas-chandra-mnnit
Copy link
Contributor

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

@shushant43
Copy link
Author

@vikas-chandra-mnnit A network call

@vikas-chandra-mnnit
Copy link
Contributor

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

@shushant43
Copy link
Author

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

@vikas-chandra-mnnit
Copy link
Contributor

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

@shushant43
Copy link
Author

@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
Copy link
Contributor

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

@shushant43
Copy link
Author

@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
Copy link
Author

@vikas-chandra-mnnit What do you think?

@vikas-chandra-mnnit
Copy link
Contributor

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants