Android Kotlin BLE Scan call back - compare device name
Budget: £36 – £0 GBP
I have some really simple BLE Kotin code, in the call back I want to find a device with a particular name and then connect to the device. The scan and call back is working, but I cannot work out how to compare the device.name to my target BLE device name.
This looks really simple, but I just cannot work it out and just need the code to complete the compare of strings, See the code below:
private val scanCallback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult)
{
val indexQuery = scanResults.indexOfFirst { it.device.address == result.device.address }
if (indexQuery != -1) { // A scan result already exists with the same address
scanResults[indexQuery] = result
scanResultAdapter.notifyItemChanged(indexQuery)
} else {
with(result.device)
{
Log.i("TAG", "Found BLE device! Name: {$result.device.name}")
// !!!!!!!!!!!!!
// Need to convet this to a string so I can compare it and then connect to the device
// !!!!!!!!!!!! this does not work
val BleName = result.device.name.toString()
// Compare incoming BLE names and find our selected BLE device
if ("mybledevice" == BleName)
{
Log.i("TAG", "!!!!Found mybledevice !!!")
}
else
{
Log.i("TAG", "!!!!Not found mybledevice !!!")
}
}
Log.i("TAG", "Found BLE device! Name: {$result.device.name}")
scanResults.add(result)
scanResultAdapter.notifyItemInserted(scanResults.size - 1)
}
}
This looks really simple, but I just cannot work it out and just need the code to complete the compare of strings, See the code below:
private val scanCallback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult)
{
val indexQuery = scanResults.indexOfFirst { it.device.address == result.device.address }
if (indexQuery != -1) { // A scan result already exists with the same address
scanResults[indexQuery] = result
scanResultAdapter.notifyItemChanged(indexQuery)
} else {
with(result.device)
{
Log.i("TAG", "Found BLE device! Name: {$result.device.name}")
// !!!!!!!!!!!!!
// Need to convet this to a string so I can compare it and then connect to the device
// !!!!!!!!!!!! this does not work
val BleName = result.device.name.toString()
// Compare incoming BLE names and find our selected BLE device
if ("mybledevice" == BleName)
{
Log.i("TAG", "!!!!Found mybledevice !!!")
}
else
{
Log.i("TAG", "!!!!Not found mybledevice !!!")
}
}
Log.i("TAG", "Found BLE device! Name: {$result.device.name}")
scanResults.add(result)
scanResultAdapter.notifyItemInserted(scanResults.size - 1)
}
}