Skip to content

Instantly share code, notes, and snippets.

@josmithua
Created September 10, 2018 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josmithua/c3ff322d060a5cd23d1c4a9afa5b8ef5 to your computer and use it in GitHub Desktop.
Save josmithua/c3ff322d060a5cd23d1c4a9afa5b8ef5 to your computer and use it in GitHub Desktop.
InputStream for Android Things UartDevice
package com.mycompany.uartproj
import com.google.android.things.pio.UartDevice
import java.io.InputStream
class UartInputStream(private val uartDevice: UartDevice) : InputStream() {
override fun read(): Int {
val byteArray = ByteArray(1)
while (true) {
val count = uartDevice.read(byteArray, 1)
if (count == 0) {
Thread.sleep(20)
continue
}
return byteArray[0].toInt()
}
}
override fun read(b: ByteArray?, off: Int, len: Int): Int {
if (b == null) {
throw NullPointerException()
} else if (off < 0 || len < 0 || b.size - off < len) {
throw IndexOutOfBoundsException()
} else if (len == 0) {
return 0
}
val byteArray = ByteArray(len)
val count = uartDevice.read(byteArray, len)
System.arraycopy(byteArray, 0, b, off, len)
return count
}
override fun available(): Int {
// What to do?
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment