I'm building an application that transfers images using BLE technology. The images need not be transferred right away, hence upon reading some documents, I concluded 1-3 Mbit/s is a reasonable choice.
(From Wikipedia)
Distance/Range <100 m (<330 ft)
Over the air data rate 125 kbit/s – 1 Mbit/s – 2 Mbit/s
Application throughput 0.27-1.37 Mbit/s [38]
However, actual testing with BLE showed the transmission rate is very slow. (almost 100 byte/s) I was testing with,
To enhance the rate of transfer, I changed the buffer size from default to 500 byte using below code.
gatt.requestMtu(CommonConstants.FILE_BUFFER_SIZE);
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
Log.e("ERROR", "stats=>" + String.valueOf(status));
Log.e("INFO", "CONNECTED");
Log.i("ERROR", "Connected to GATT server.");
Log.i("ERROR", "Attempting to start service discovery:" + gatt.discoverServices());
}
However, changing Maximum Transfer Unit did not change the rate of data transfer.
I faced the same problem when I hard coded the payload length to 20 bytes and it took too long to transfer 1MB of file. So, I read about MTU
MTU (Maximum Transfer Unit) : MTU specifies the maximum number of bytes that can be sent in a single write operation.
Just requesting for MTU will not speed-up the transfer time. requestMtu() will simply return the maximum number of bytes that can send to other device while writing on device.
To speed-up process you need to update the payload which you are sending to write on connected device.
Let's assume your current bytes were sent as 100 bytes /sec:
Total Bytes: 5000 bytes
Payload length: 100 bytes
1 write operation: 100 bytes / sec
Total write operations will be: 5000/100 = 50 sec
Now let's say after requesting MTU value from device, it comes out to be 250 bytes, so again:
Total Bytes: 5000 bytes
Payload length: 250 bytes
1 write operation: 250 bytes / sec
Total write operations will be: 5000/250 = 20 sec // see the time is reduced now.
So, how you can achieve this:
1: Connect to the device
2: Request for MTU value (with maximum 512 and it will return you the supported MTU value of device. In most cases it is 251, but just for the information 3 bytes are used for internal purposes, so the maximum size is MTU-3 )
3: Use this MTU value to create the payload
4: Do the write operation
A large MTU with a short connection interval usually gives the best performance. If both devices support LE Data Length Extension and 2 Mbit/s PHY then you will increase the performance to the max.
See https://www.novelbits.io/bluetooth-5-speed-maximum-throughput/ .
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.