I'm new to Android and Kotlin and I get stuck at a point in listing the discovered BLE devices.
When a BLE Device was found the onScanResult
callback gets fired and the data of the found BLE device is available as class ScanResult
.
To list all available BLE devices I have an mLeDevices: ArrayList<ScanResult> = ArrayList()
where the found device gets added if it's not already in the list.
private val bleCallback: ScanCallback = object :ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
super.onScanResult(callbackType, result)
if (!mLeDevices.contains(result)) {
mLeDevices.add(result)
}
}
}
But with this code every device gets added in the List multiple times since ScanResult also contains the RSSI Value which is varying. I think I have to check if mLeDevices
contains result.device.address
. If yes just update the rssi value, if not add the new device to the list. But i don't know how to implement that approach in the best way. Is there a way in Kotlin to check if an ArrayList of a class contains an element which contains an attribute? Or is there another solution for my problem?
You can use none
function that takes a lambda. You can then the condition on the unique attribute.
if (mLeDevices.none { it.BSSID == result.BSSID }) {
mLeDevices.add(result)
}