In Kotlin I created a function to check if keyboard is shown.
fun hideKeyboard() {
val shownKeyboard = driver.executeScript("mobile: isKeyboardShown")
if (shownKeyboard.equals(true)) {
driver.executeScript("mobile: hideKeyboard")
}
}