Cosas de Android: NullPointerException al recibir datos a través de UART USB en Raspberry PI 3

En mi código, logging un UartDeviceCallBack en mi UartDevice y cuando mi periférico envía datos a través de una serie, mi aplicación falla con el siguiente error:

 E/AndroidRuntime: FATAL EXCEPTION: main Process: co.foodles.posapp, PID: 1655 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.android.things.pio.UartDeviceCallback.onUartDeviceDataAvailable(com.google.android.things.pio.UartDevice)' on a null object reference at com.google.android.things.pio.UartDeviceImpl$UartDeviceCallbackDispatch.dispatchInterruptEvent(UartDeviceImpl.java:245) at com.google.android.things.pio.CallbackDispatch.onFileDescriptorEvents(CallbackDispatch.java:149) at android.os.MessageQueue.dispatchEvents(MessageQueue.java:284) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:325) at android.os.Looper.loop(Looper.java:142) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

Aquí está la class de mi dispositivo de Uart :

 class RFideas80581AK0(peripheralManagerService : PeripheralManagerService): Closeable { companion object { private val LOG_TAG = RFideas80581AK0::class.java.simpleName private val UART_DEVICE_NAME = "USB1-1.2:1.0" private val BAUDRATE = 57600 private val DATA_SIZE = 8 private val PARITY = UartDevice.PARITY_NONE private val STOP_BIT = 1 } private var mDevice : UartDevice? = getDevice(peripheralManagerService, UART_DEVICE_NAME, BAUDRATE, DATA_SIZE, PARITY, STOP_BIT) private val uartDeviceCallBack = CustomUartCallBack() var readCallBack : ((String) -> (Unit))? = null private fun getDevice(peripheralManagerService: PeripheralManagerService, name : String, baudrate : Int, dataSize : Int, parity : Int, stopBit : Int) : UartDevice? = try { peripheralManagerService.openUartDevice(name).also { it.setBaudrate(baudrate) it.setDataSize(dataSize) it.setParity(parity) it.setStopBits(stopBit) it.registerUartDeviceCallback(uartDeviceCallBack) Log.d(LOG_TAG, "configunetworking device ${it.name}") } } catch (e : IOException){ Log.e(LOG_TAG, "error while opening UartDevice on address $UART_DEVICE_NAME",e) null } override fun close() { Log.d(LOG_TAG, "close") mDevice = mDevice?.let { try { it.unregisterUartDeviceCallback(uartDeviceCallBack) it.close() } catch (e : IOException){ Log.e(LOG_TAG, "error while closing UartDevice",e) throw e } null } } private fun convertAsciiNumbersToHexString(asciiNumbers : String) : String { var hexString = BigInteger(asciiNumbers).toString(16) if (hexString.length %2 == 1) hexString = "0"+hexString return hexString.windowed(2, 2).reversed().joinToString("").toUpperCase() } override fun readUartBuffer(uartDevice: UartDevice) : String { val maxCount = 128 val buffer = ByteArray(maxCount) uartDevice.read(buffer, maxCount) return convertAsciiNumbersToHexString(String(buffer).takeWhile { it.isDigit() }) } inner class CustomUartCallBack : UartDeviceCallback(){ override fun onUartDeviceDataAvailable(uart: UartDevice): Boolean { try { readCallBack?.invoke(readUartBuffer(uart)) } catch (e : IOException){ Log.e(LOG_TAG, "error while reading uart buffer",e) } return true } override fun onUartDeviceError(uart: UartDevice, error: Int) { Log.e(LOG_TAG, "error $error in CustomUartCallBack") } } } 

¿Cualquier pista?

Gracias a Onik, aquí está la implementación correcta:

 class RFideas80581AK0(peripheralManagerService : PeripheralManagerService): Closeable { companion object { private val LOG_TAG = RFideas80581AK0::class.java.simpleName private val UART_DEVICE_NAME = "USB1-1.2:1.0" private val BAUDRATE = 57600 private val DATA_SIZE = 8 private val PARITY = UartDevice.PARITY_NONE private val STOP_BIT = 1 } private val uartDeviceCallBack = CustomUartCallBack() var readCallBack : ((String) -> (Unit))? = null private var mDevice : UartDevice? = getDevice(peripheralManagerService, UART_DEVICE_NAME, BAUDRATE, DATA_SIZE, PARITY, STOP_BIT) private fun getDevice(peripheralManagerService: PeripheralManagerService, name : String, baudrate : Int, dataSize : Int, parity : Int, stopBit : Int) : UartDevice? = try { peripheralManagerService.openUartDevice(name).also { it.setBaudrate(baudrate) it.setDataSize(dataSize) it.setParity(parity) it.setStopBits(stopBit) it.registerUartDeviceCallback(uartDeviceCallBack) Log.d(LOG_TAG, "configunetworking device ${it.name}") } } catch (e : IOException){ Log.e(LOG_TAG, "error while opening UartDevice on address $UART_DEVICE_NAME",e) null } override fun close() { Log.d(LOG_TAG, "close") mDevice = mDevice?.let { try { it.unregisterUartDeviceCallback(uartDeviceCallBack) it.close() } catch (e : IOException){ Log.e(LOG_TAG, "error while closing UartDevice",e) throw e } null } } private fun convertAsciiNumbersToHexString(asciiNumbers : String) : String { var hexString = BigInteger(asciiNumbers).toString(16) if (hexString.length %2 == 1) hexString = "0"+hexString return hexString.windowed(2, 2).reversed().joinToString("").toUpperCase() } override fun readUartBuffer(uartDevice: UartDevice) : String { val maxCount = 128 val buffer = ByteArray(maxCount) uartDevice.read(buffer, maxCount) return convertAsciiNumbersToHexString(String(buffer).takeWhile { it.isDigit() }) } inner class CustomUartCallBack : UartDeviceCallback(){ override fun onUartDeviceDataAvailable(uart: UartDevice): Boolean { try { readCallBack?.invoke(readUartBuffer(uart)) } catch (e : IOException){ Log.e(LOG_TAG, "error while reading uart buffer",e) } return true } override fun onUartDeviceError(uart: UartDevice, error: Int) { Log.e(LOG_TAG, "error $error in CustomUartCallBack") } } } 

En el momento de la línea private var mDevice : UartDevice? = getDevice(..) private var mDevice : UartDevice? = getDevice(..) se invoca, uartDeviceCallBack se refiere a null porque su uartDeviceCallBack instancias ocurre una línea más. Cambiar el order de las líneas:

 private val uartDeviceCallBack = CustomUartCallBack() private var mDevice : UartDevice? = getDevice(peripheralManagerService, UART_DEVICE_NAME, BAUDRATE, DATA_SIZE, PARITY, STOP_BIT)