This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Receiving large amount of data in android via BLE

Hello everyone, recently i was trying to figure out the best way how to send a large amount of data from ble peripheral to the central (Android device in this case) and thanks to great posts here i found the answer. Now im also doing the part for the android device and since i couldn't find any example here, maybe someone who understands Android could share what is the best way to receive the large amount of data? How to manage the data stream that is coming from the peripheral device if its total size will be someone near 100kB?

Edit: 2016-11-21

So i ran some tests today and this is what i get. When i connect with my phone i see that connection parameters are as follows: initphoneparams.png. When i use MIN_CONN_INTERVAL MSEC_TO_UNITS(7.5, UNIT_1_25_MS) and MAX_CONN_INTERVAL MSEC_TO_UNITS(40, UNIT_1_25_MS) i can see connection params update request after connection has been actually established: paramsupdate2.png. If i force both MIN_CONN_INTERVAL and MAX_CONN_INTERVAL to MSEC_TO_UNITS(7.5, UNIT_1_25_MS), i am also seeing conn params update request after establishing the connect: paramsupdate1.png.

Both ways you can see that i get connection parameters update response accepted (one packet below than the one i am looking to the data).So i can be sure that my device did update connection parameters, correct?

No for the question that i had to check? Is there a way to see in the software how much packets does actually my peripheral send in one connection interval? Or i just have to look at the times that packets are received and calculate how many packets was send in a certain time? Because looking at the times i would say that when both min and max connection interval is set to 7.5ms i am seeing 2 - 3 packets in one connection event, but that number is not consistent. I also see a packet loss. If i set min connection interval to 7.5ms and maximum connection interval to 40ms, i am seeing 2 packets per connection event. Could you watch through my wireshark logs and confirm that what i am seeing is correct?

So am i really seeing the limitations of my central device here? I also want to ask, if i set min connection interval to 7.5ms and maximum connection interval to 40ms, does that really guarantee maximum throughput for every central?

And also what will happen if i set both connection interval to 7.5ms and maximum connection interval to 7.5ms on my peripheral, but my central does not support this value? Will peripheral just drop the connection? And here are my wireshark logos. max40min75.pcapng and max75min75.pcapng.

Sorry for bad naming also, but the first one is with max conn interval 40ms and min conn interval 7.5ms. Second one is with both min and max conn interval equal to 7.5ms

  • What is your actual problem here?

    BLE is neither particularly high-speed nor high-volume; certainly not relative to other interfaces common to Android (eg, WiFi) - so there shouldn't be any issues specific to BLE here.

    What are you intending to do with the data?

  • I just want to save received data to the file, and as i said, it would be about a 100kB of data. It is just that i am still learning the android and simply i just don't know what is the correct way in android to receive such amount of data and how to deal with it. (What i have done is that i created a characteristic on my peripheral device with notification property. then from android device i write a descriptor value to enable notifications and trigger data send start by writing to the characteristic. From that point my peripheral just goes to the loop and uses sd_ble_gatts_hvx(); to send out the data. When my android device receives the data, onCharacteristicChanged(); method is called and i can see a received data. But since i am not really an android developer, i don't know how exactly to handle that data(should i create a big buffer and store data there, or write everything to the file instantly if that is possible or whatever). I intended to basically ask: What is the best way to receive about 100kB of data with an android device and save it to a file, but i cann't find any examples from Nordic how to do something like that. Hope i make it clear now :)

  • BLE packets are only 20 bytes; it shouldn't be a problem to write 20 bytes at a time to a file.

    Again, this has nothing specifically to do with Nordic or Bluetooth - a file neither knows nor cares where the data came from!

  • I'd start with Nordic's DFU library for Android:

    github.com/.../Android-DFU-Library

    Find the classes in there that send the application/sd/bootloader image over the air from central to peripheral. You have the reverse situation: you'll be reading from a characteristic repeatedly, not writing, but perhaps you can turn their code around a bit.

    100 kB / 20 bytes per characteristic read = 5,120 reads. It'll take a while.

  • I understand this awneil. What i actually wanted to know is how to write that data properly to a file. Should i i read all the data and store it in some sort of buffer and write it to a file with a single command, or write to the file by chunks of every 20 bytes, because i imagine that writing to a file takes a while, and my peripheral will be sending a large to stream, so i was wondering if android will have enough time to do all this and not to miss received packets? Thank you for your comments.

Related