gpt4 book ai didi

android - 将带有 Android Things 的 Raspberry PI 3 连接到 Arduino

转载 作者:行者123 更新时间:2023-11-29 15:37:07 26 4
gpt4 key购买 nike

<分区>

我的问题是关于如何将 Android of Things 应用程序与 Arduino 进行通信。我想在我的 Android of Things 项目中使用所有便宜的 Arduino 传感器。我找到了两个源链接并进行了尝试,但没有得到值。

enter image description here

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 获取值?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com