EspressifApp/EspBlufiForiOS

Security key negotation failed

Closed this issue · 2 comments

Hi there,

I'm trying to perform a connection to an ESP32 card using your library BlufiLibrary inside a React-native application.

My problem

Once I'm connected to the card (delegate method centralManager(_ central: didConnect peripheral:) is called), I launch the key negotation. During the negotation process, I have an error code 4 using the following delegate method :

func blufi(_ client: BlufiClient, didReceiveError errCode: Int) {
   print("[BLUFI] Received error. Code = ", errCode)
}

My understanding

Following this documentation, it looks like I got a 0x04: init security error.

My question

The application of this repository works just well when I compile it from my computer and launch it on my device.
Do you have any idea or insights about why I got this error when I use it inside an other app.

My code

@objc(RNEsp32)
class RNEsp32: NSObject, BlufiDelegate, CBCentralManagerDelegate, CBPeripheralDelegate {

    var bluFiClient: BlufiClient?

    @objc(connectESP32:withB:withResolver:withRejecter:)
    func connectESP32(a: Float, b: Float, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
      self.connect()
      resolve()
    }

  private func connect() -> Void {
    print("Connection launched !")
    bluFiClient = BlufiClient.init()
    bluFiClient?.blufiDelegate = self
    bluFiClient?.centralManagerDelete = self
    bluFiClient?.postPackageLengthLimit = 128;
  }

  func blufi(_ client: BlufiClient, gattPrepared status: BlufiStatusCode, service: CBService?, writeChar: CBCharacteristic?, notifyChar: CBCharacteristic?) {
    print("[BLUFI] Blufi ready ! Status = ", status)
    if status == StatusSuccess {
      print("[BLUFI] launch security negotation")
      bluFiClient?.negotiateSecurity()
    }
  }

  func blufi(_ client: BlufiClient, didNegotiateSecurity status: BlufiStatusCode) {
    print("[BLUFI] Security has been negotatited ! Status =", status)
    let params = BlufiConfigureParams()
    params.opMode = OpModeSta
    params.staSsid = "MY_SSID"
    params.staPassword = "MY_PASSWORD"
    print("[BLUFI] Let's connect !!!!!")
    bluFiClient?.configure(params)
  }

  func blufi(_ client: BlufiClient, didReceiveError errCode: Int) {
    print("[BLUFI] Received error. Code = ", errCode)
  }

  func blufi(_ client: BlufiClient, didPostConfigureParams status: BlufiStatusCode) {
    print("[BLUFI] Did Post configuration params")
  }

  func blufi(_ client: BlufiClient, didPostCustomData data: Data, status: BlufiStatusCode) {
    print("[BLUFI] Did post custom data")
  }

  func blufi(_ client: BlufiClient, didReceiveCustomData data: Data, status: BlufiStatusCode) {
    let customString = String(decoding: data, as: UTF8.self)
    print("[BLUFI] did receive custom data : ", customString)
  }

  func centralManagerDidUpdateState(_ central: CBCentralManager) {
    if central.state == .poweredOn {
      bluFiClient?.connect("DEVICE_UUID")
    }
  }

  func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
    print("[Central] Device connected")
  }

  func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
    print("[Centrail] Device connection failed")
  }

  func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
    print("[Central] Device has been disconnected")
  }
}

Thanks for your help.

I'd suggest looking through the Blufi library code to see if you can find a place where it returns error code 0x04.

@brunck Thank you for your answer but I have finally found my mistake.

In our implementation, the ESP32 card considers that the connexion is done when we have press a button to which it is connected. Once we've pressed this button we receive a custom success message.

So I don't have to call negotiateSecurity in blufi(_ :gattPrepared:service:writeChar:notifyChar:) but in func blufi(_:didReceiveCustomData:status:)