【发布时间】:2017-11-23 11:32:19
【问题描述】:
我的问题是关于如何与 Arduino 通信 Android of Things 应用程序。我想在我的 Android of Things 项目中使用所有甜美廉价的 Arduino 传感器库。我找到了两个源链接并尝试过,但没有得到值。
class Arduino(uartDevice: String = "UART0"): AutoCloseable {
private val TAG = "Arduino"
private val uart: UartDevice by lazy {
PeripheralManagerService().openUartDevice(uartDevice).apply {
setBaudrate(115200)
setDataSize(8)
setParity(UartDevice.PARITY_NONE)
setStopBits(1)
}
}
fun read(): String {
val maxCount = 8
val buffer = ByteArray(maxCount)
var output = ""
do {
val count = uart.read(buffer, buffer.size)
output += buffer.toReadableString()
if(count == 0) break
Log.d(TAG, "Read ${buffer.toReadableString()} $count bytes from peripheral")
} while (true)
return output
}
private fun ByteArray.toReadableString() = filter { it > 0.toByte() }
.joinToString(separator = "") { it.toChar().toString() }
fun write(value: String) {
val count = uart.write(value.toByteArray(), value.length)
Log.d(TAG, "Wrote $value $count bytes to peripheral")
}
override fun close() {
uart.close()
}
}
我在堆栈上找到了这个Raspberry Pi to Arduino Communication 源,但讨论与我的问题无关。我想要从 arduino 到 android things RPI 3 的所有值。
我们如何使用 java 通过 android 框架从 arduino 获取值?
【问题讨论】:
-
看看this 项目。它有一个重要的部分:双向逻辑电平转换器。
标签: android arduino communication android-things