<分区>
我的问题是关于如何将 Android of Things 应用程序与 Arduino 进行通信。我想在我的 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 获取值?