【问题标题】:Websocket from Arduino to Android: send integer instead of text从 Arduino 到 Android 的 Websocket:发送整数而不是文本
【发布时间】:2018-08-22 13:50:00
【问题描述】:

我正在尝试使用 Link2004/Arduino WebSockets 库将一个整数从 Wemos D1(ESP8266) 作为服务器发送到使用 OkHttpClient 库的 Android 客户端,使用 sendBIN(num, buf, buf_len),如下所示:

int n=1234;
uint8_t * buf = (uint8_t *) &n;
size_t buf_len = sizeof(n);
webSocket.sendBIN(0,buf,buf_len); // num is set to 0, ( client # 0 )

但我在 Android 端得到的是 d2040000,而不是 1234。

Android 监听器如下所示:

public void onMessage(WebSocket webSocket, ByteString bytes) {
    output("Received bytes : " + bytes.hex());
}

正如我所说,TextView 输出的结果是d2040000,而不是发送时的 1234。

谁能澄清一下这个困惑?

【问题讨论】:

  • 您不应转换为十六进制表示。

标签: android websocket arduino esp8266


【解决方案1】:

字节0xd20x040x000x00little-endian format中的32位整数1234的字节。

所以d2040000 是这些字节的十六进制字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 2020-04-03
    • 2018-11-13
    相关资源
    最近更新 更多