Skip to content

Commit

Permalink
fix(#6): searching for softap devices on android
Browse files Browse the repository at this point in the history
  • Loading branch information
mateogianolio committed Sep 25, 2024
1 parent ccdecc3 commit ac63141
Showing 1 changed file with 110 additions and 53 deletions.
163 changes: 110 additions & 53 deletions android/src/main/java/com/espidfprovisioning/EspIdfProvisioningModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,6 @@ class EspIdfProvisioningModule internal constructor(context: ReactApplicationCon
@SuppressLint("MissingPermission")
@ReactMethod
override fun searchESPDevices(devicePrefix: String, transport: String, security: Double, promise: Promise?) {
// Permission checks
if (!hasBluetoothPermissions() || !hasFineLocationPermission()) {
promise?.reject(Error("Missing one of the following permissions: BLUETOOTH, BLUETOOTH_ADMIN, BLUETOOTH_CONNECT, BLUETOOTH_SCAN, ACCESS_FINE_LOCATION"))
return
}

val transportEnum = when (transport) {
"softap" -> ESPConstants.TransportType.TRANSPORT_SOFTAP
"ble" -> ESPConstants.TransportType.TRANSPORT_BLE
Expand All @@ -100,58 +94,103 @@ class EspIdfProvisioningModule internal constructor(context: ReactApplicationCon
}

espDevices.clear()
espProvisionManager.searchBleEspDevices(devicePrefix, object : BleScanListener {
override fun scanStartFailed() {
promise?.reject(Error("Scan could not be started."))
if (transportEnum == ESPConstants.TransportType.TRANSPORT_BLE) {
// Permission checks
if (!hasBluetoothPermissions() || !hasFineLocationPermission()) {
promise?.reject(Error("Missing one of the following permissions: BLUETOOTH, BLUETOOTH_ADMIN, BLUETOOTH_CONNECT, BLUETOOTH_SCAN, ACCESS_FINE_LOCATION"))
return
}

override fun onPeripheralFound(device: BluetoothDevice?, scanResult: ScanResult?) {
// Can this happen?
if (device == null) {
return
espProvisionManager.searchBleEspDevices(devicePrefix, object : BleScanListener {
override fun scanStartFailed() {
promise?.reject(Error("Scan could not be started."))
}

val deviceName = scanResult?.scanRecord?.deviceName
override fun onPeripheralFound(device: BluetoothDevice?, scanResult: ScanResult?) {
// Can this happen?
if (device == null) {
return
}

// No device name
if (deviceName.isNullOrEmpty()) {
return
}
val deviceName = scanResult?.scanRecord?.deviceName

val serviceUuid = scanResult.scanRecord?.serviceUuids?.getOrNull(0)?.toString()
if (serviceUuid != null && !espDevices.containsKey(deviceName)) {
val espDevice = ESPDevice(reactApplicationContext, transportEnum, securityEnum)
espDevice.bluetoothDevice = device
espDevice.deviceName = deviceName
espDevice.primaryServiceUuid = serviceUuid
espDevices[deviceName] = espDevice
}
}
// No device name
if (deviceName.isNullOrEmpty()) {
return
}

override fun scanCompleted() {
if (espDevices.size == 0) {
promise?.reject(Error("No bluetooth device found with given prefix"))
return
val serviceUuid = scanResult.scanRecord?.serviceUuids?.getOrNull(0)?.toString()
if (serviceUuid != null && !espDevices.containsKey(deviceName)) {
val espDevice = ESPDevice(reactApplicationContext, transportEnum, securityEnum)
espDevice.bluetoothDevice = device
espDevice.deviceName = deviceName
espDevice.primaryServiceUuid = serviceUuid
espDevices[deviceName] = espDevice
}
}

val resultArray = Arguments.createArray()
override fun scanCompleted() {
if (espDevices.size == 0) {
promise?.reject(Error("No bluetooth device found with given prefix"))
return
}

espDevices.values.forEach { espDevice ->
val resultMap = Arguments.createMap()
resultMap.putString("name", espDevice.deviceName)
resultMap.putString("transport", transport)
resultMap.putInt("security", security.toInt())
val resultArray = Arguments.createArray()

resultArray.pushMap(resultMap)
espDevices.values.forEach { espDevice ->
val resultMap = Arguments.createMap()
resultMap.putString("name", espDevice.deviceName)
resultMap.putString("transport", transport)
resultMap.putInt("security", security.toInt())

resultArray.pushMap(resultMap)
}

promise?.resolve(resultArray)
}

promise?.resolve(resultArray)
override fun onFailure(e: Exception?) {
promise?.reject(e)
}
})
} else {
if (!hasWifiPermission() || !hasFineLocationPermission()) {
promise?.reject(Error("Missing one of the following permissions: CHANGE_WIFI_STATE, ACCESS_WIFI_STATE, ACCESS_NETWORK_STATE, ACCESS_FINE_LOCATION"))
}

override fun onFailure(e: Exception?) {
promise?.reject(e)
}
})
espProvisionManager.searchWiFiEspDevices(devicePrefix, object : WiFiScanListener {
override fun onWifiListReceived(wifiList: ArrayList<WiFiAccessPoint>?) {
if (wifiList?.size == 0) {
promise?.reject(Error("No wifi device found with given prefix"))
return
}

val resultArray = Arguments.createArray()

wifiList?.forEach { wiFiAccessPoint ->
val espDevice = ESPDevice(reactApplicationContext, transportEnum, securityEnum)
espDevice.wifiDevice = wiFiAccessPoint
espDevice.deviceName = wiFiAccessPoint.wifiName
espDevices[wiFiAccessPoint.wifiName] = espDevice

val resultMap = Arguments.createMap()

resultMap.putString("name", wiFiAccessPoint.wifiName)
resultMap.putString("transport", transport)
resultMap.putInt("security", wiFiAccessPoint.security)
resultMap.putString("password", wiFiAccessPoint.password)

resultArray.pushMap(resultMap)
}

promise?.resolve(resultArray)
}

override fun onWiFiScanFailed(e: Exception?) {
promise?.reject(e)
}
})
}
}

@SuppressLint("MissingPermission")
Expand Down Expand Up @@ -215,18 +254,36 @@ class EspIdfProvisioningModule internal constructor(context: ReactApplicationCon
}
}

// If the bluetooth device does not exist, try using the bonded one (if it exists)
if (espDevice?.bluetoothDevice == null) {
espDevice?.bluetoothDevice = bluetoothAdapter.bondedDevices.find {
bondedDevice -> bondedDevice.name == deviceName
if (transportEnum == ESPConstants.TransportType.TRANSPORT_BLE) {
// If the bluetooth device does not exist, try using the bonded one (if it exists)
if (espDevice?.bluetoothDevice == null) {
espDevice?.bluetoothDevice = bluetoothAdapter.bondedDevices.find { bondedDevice ->
bondedDevice.name == deviceName
}
}
}

// If the bluetooth device exists and we have a primary service uuid, we will be able to connect to it
if (espDevice?.bluetoothDevice != null && espDevice.primaryServiceUuid != null) {
espDevice.proofOfPossession = proofOfPossession
if (username != null) {
espDevice.userName = username
// If the bluetooth device exists and we have a primary service uuid, we will be able to connect to it
if (espDevice?.bluetoothDevice != null && espDevice.primaryServiceUuid != null) {
espDevice.proofOfPossession = proofOfPossession
if (username != null) {
espDevice.userName = username
}

val result = Arguments.createMap()
result.putString("name", espDevice.deviceName)
result.putString("transport", transport)
result.putInt("security", security.toInt())

promise?.resolve(result)
return
}
} else {
if (espDevice?.wifiDevice == null) {
val wifiDevice = WiFiAccessPoint()
wifiDevice.wifiName = deviceName
wifiDevice.password = softAPPassword

espDevice?.wifiDevice = wifiDevice
}

val result = Arguments.createMap()
Expand Down

0 comments on commit ac63141

Please sign in to comment.