diff --git a/android/src/main/java/com/espidfprovisioning/EspIdfProvisioningModule.kt b/android/src/main/java/com/espidfprovisioning/EspIdfProvisioningModule.kt index c29c6e8..5b39236 100644 --- a/android/src/main/java/com/espidfprovisioning/EspIdfProvisioningModule.kt +++ b/android/src/main/java/com/espidfprovisioning/EspIdfProvisioningModule.kt @@ -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 @@ -100,58 +94,102 @@ 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?) { + 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) + + resultArray.pushMap(resultMap) + } + + promise?.resolve(resultArray) + } + + override fun onWiFiScanFailed(e: Exception?) { + promise?.reject(e) + } + }) + } } @SuppressLint("MissingPermission") @@ -215,22 +253,40 @@ 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() - result.putString("name", espDevice.deviceName) + result.putString("name", espDevice?.deviceName) result.putString("transport", transport) result.putInt("security", security.toInt())