Skip to content

Commit

Permalink
fix(#25): fix incorrect if-block inside createESPDevice method (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateogianolio authored Dec 19, 2023
1 parent ff7f88f commit 4d153ee
Showing 1 changed file with 40 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,41 +212,52 @@ class EspIdfProvisioningModule internal constructor(context: ReactApplicationCon
else -> ESPConstants.SecurityType.SECURITY_2
}

if (espDevices[deviceName] != null) {
val result = Arguments.createMap()
result.putString("name", espDevices[deviceName]?.deviceName)
result.putArray("capabilities", Arguments.fromList(espDevices[deviceName]?.deviceCapabilities))
result.putInt("security", security)
result.putString("transport", transport)
result.putString("username", espDevices[deviceName]?.userName)
result.putString("versionInfo", espDevices[deviceName]?.versionInfo)

promise?.resolve(result)
return
}

// If no ESP device found in list (no scan has been performed), create a new one
if (espDevices[deviceName] == null) {
val espDevice = espProvisionManager.createESPDevice(transportEnum, securityEnum)
var bleDevice = espDevice?.bluetoothDevice

// If the bluetooth device does not contain service uuids, try using the bonded
// one (if it exists)
if (bleDevice?.uuids == null) {
bleDevice = bluetoothAdapter.bondedDevices.find {
bondedDevice -> bondedDevice.name == deviceName
}
val espDevice = espProvisionManager.createESPDevice(transportEnum, securityEnum)
var bleDevice = espDevice?.bluetoothDevice

// If the bluetooth device does not contain service uuids, try using the bonded
// one (if it exists)
if (bleDevice?.uuids == null) {
bleDevice = bluetoothAdapter.bondedDevices.find {
bondedDevice -> bondedDevice.name == deviceName
}
}

// If the bluetooth device exists and contains service uuids, we will be able to connect to it
if (bleDevice?.uuids != null) {
espDevice.bluetoothDevice = bleDevice
espDevice.deviceName = deviceName
espDevice.proofOfPossession = proofOfPossession
if (username != null) {
espDevice.userName = username
}
// If the bluetooth device exists and contains service uuids, we will be able to connect to it
if (bleDevice?.uuids != null) {
espDevice.bluetoothDevice = bleDevice
espDevice.deviceName = deviceName
espDevice.proofOfPossession = proofOfPossession
if (username != null) {
espDevice.userName = username
}

espDevices[deviceName] = espDevice
espDevices[deviceName] = espDevice

val result = Arguments.createMap()
result.putString("name", espDevice.deviceName)
result.putArray("capabilities", Arguments.fromList(espDevice.deviceCapabilities))
result.putInt("security", security)
result.putString("transport", transport)
result.putString("username", espDevice.userName)
result.putString("versionInfo", espDevice.versionInfo)
val result = Arguments.createMap()
result.putString("name", espDevice.deviceName)
result.putArray("capabilities", Arguments.fromList(espDevice.deviceCapabilities))
result.putInt("security", security)
result.putString("transport", transport)
result.putString("username", espDevice.userName)
result.putString("versionInfo", espDevice.versionInfo)

promise?.resolve(result)
return
}
promise?.resolve(result)
return
}

// Exhausted our other options, perform search in hope of finding the device
Expand Down

0 comments on commit 4d153ee

Please sign in to comment.