简体   繁体   English

20 字节的蓝牙 LE 数据 JSON

[英]Bluetooth LE data JSON in 20 bytes

I have a Bluetooth LE module on Arduino which sends a JSON string to an Android application.我在 Arduino 上有一个蓝牙 LE 模块,它向 Android 应用程序发送一个 JSON 字符串。

The JSON string look like this: JSON 字符串如下所示:

{'d_stats':[{'t':'26.62','h':'59.64','p':'755.23','a':'109.02','hrm':'0.00'}]}

The Android app receives packets of 20 bytes (20 characters limit) and I can't find a method to put all packets together when the last packet was received. Android 应用程序接收 20 字节(20 个字符限制)的数据包,我找不到在收到最后一个数据包时将所有数据包放在一起的方法。

Is there a way to know when the last packet is received?有没有办法知道什么时候收到最后一个数据包?

Edit: the bluetooth sends data at a constant interval of time.编辑:蓝牙以恒定的时间间隔发送数据。 There is a button connected to the Arduino board which, when pushed, will send other data via Bluetooth. Arduino 板上有一个按钮,按下该按钮时,将通过蓝牙发送其他数据。 The problem is that it overlaps with the timed transmission.问题在于它与定时传输重叠。

I found the solution, although not very elegant.我找到了解决方案,虽然不是很优雅。 Instead of sending the whole JSON string, BLE will send a key/value pair in a single packet. BLE 将在单个数据包中发送键/值对,而不是发送整个 JSON 字符串。 In C first:首先在 C 中:

char passMsg(String akey, char* origMsg){
  // akey = object key must be 4 characters long
  // origMsg + akey must be shorter than 20 characters
  char* newmsg = origMsg;
  size_t prevlen = strlen(newmsg);  
  memset(newmsg + prevlen, ' ', 15 - prevlen);
  *(newmsg + 15) = '\0'; 
  String bleMsg = akey + ":"+newmsg;
  ble.print("AT+BLEUARTTX=");            
  ble.println(bleMsg);  
}

This way I pass a string like this: temp:20.45这样我传递了一个这样的字符串: temp:20.45

Then in Android/Java:然后在 Android/Java 中:

String[] rawString = data.replace(" ", "").split(":");
if(rawString.length>1){
  String apiCallKey = rawString[0];
  String apiCallVal = rawString[1];
  callAPI(apiCallKey,apiCallVal);
}

Where data is raw data from Bluetooth.其中data是来自蓝牙的原始数据。

Phew...呼...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM