简体   繁体   中英

Bluetooth LE data JSON in 20 bytes

I have a Bluetooth LE module on Arduino which sends a JSON string to an Android application.

The JSON string look like this:

{'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.

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. 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. In C first:

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

Then in 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.

Phew...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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