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

fix(#6): searching for softap devices on android #79

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 110 additions & 54 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,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<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)

resultArray.pushMap(resultMap)
}

promise?.resolve(resultArray)
}

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

@SuppressLint("MissingPermission")
Expand Down Expand Up @@ -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())

Expand Down
Loading