简体   繁体   English

如何自定义 Android BLE(蓝牙低功耗)gatt 传输速度?

[英]How can I customize Android BLE (Bluetooth Low Energy) gatt transfer speed?

I'm building an application that transfers images using BLE technology.我正在构建一个使用 BLE 技术传输图像的应用程序。 The images need not be transferred right away, hence upon reading some documents, I concluded 1-3 Mbit/s is a reasonable choice.图像不需要立即传输,因此在阅读了一些文档后,我得出结论,1-3 Mbit/s 是一个合理的选择。

(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.然而,使用 BLE 进行的实际测试显示传输速率非常慢。 (almost 100 byte/s) I was testing with, (几乎 100 字节/秒)我正在测试,

  • two latest update Android phones: Samsung galaxy 8, Samsung galaxy note 10.两款最新更新的 Android 手机:三星 Galaxy 8、三星 Galaxy Note 10。
  • debugging with IntelliJ使用 IntelliJ 进行调试

To enhance the rate of transfer, I changed the buffer size from default to 500 byte using below code.为了提高传输速率,我使用以下代码将缓冲区大小从默认值更改为 500 字节。

 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.但是,更改最大传输单位并没有改变数据传输的速率。

  • What stuffs I am missing out with BLE regarding data transfer speed?关于数据传输速度,我错过了 BLE 的哪些内容?
  • Shouldn't increased MTU result in increased speed in data transfer?增加 MTU 不应该提高数据传输速度吗?

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.当我将有效负载长度硬编码为20 个字节并且传输 1MB 的文件花费了太长时间时,我遇到了同样的问题。 So, I read about MTU所以,我读到了MTU

MTU (Maximum Transfer Unit) : MTU specifies the maximum number of bytes that can be sent in a single write operation. MTU(最大传输单元) :MTU 指定在单个写操作中可以发送的最大字节数。

Just requesting for MTU will not speed-up the transfer time.仅仅请求MTU不会加快传输时间。 requestMtu() will simply return the maximum number of bytes that can send to other device while writing on device. requestMtu()将简单地返回在设备上写入时可以发送到其他设备的最大字节数。

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:假设您当前的字节以100 字节/秒的速度发送:

Total Bytes: 5000 bytes总字节数:5000 字节

Payload length: 100 bytes有效载荷长度:100 字节

1 write operation: 100 bytes / sec 1次写操作:100字节/秒

Total write operations will be: 5000/100 = 50 sec总写入操作为:5000/100 = 50 秒

Now let's say after requesting MTU value from device, it comes out to be 250 bytes, so again:现在假设从设备请求MTU值后,它是250字节,所以再次:

Total Bytes: 5000 bytes总字节数:5000 字节

Payload length: 250 bytes有效载荷长度:250 字节

1 write operation: 250 bytes / sec 1次写操作:250字节/秒

Total write operations will be: 5000/250 = 20 sec // see the time is reduced now.总写入操作将是:5000/250 = 20 秒// 看到时间现在减少了。

So, how you can achieve this:那么,如何实现这一目标:

1: Connect to the device 1:连接设备

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 ) 2:请求MTU值(最大512,它会返回设备支持的MTU值。大多数情况下是251,但只是为了信息3字节用于内部目的,所以最大大小为MTU-3 )

3: Use this MTU value to create the payload 3:使用此MTU值创建有效负载

4: Do the write operation 4:做写操作

A large MTU with a short connection interval usually gives the best performance.具有短连接间隔的大 MTU 通常会提供最佳性能。 If both devices support LE Data Length Extension and 2 Mbit/s PHY then you will increase the performance to the max.如果两个设备都支持 LE 数据长度扩展和 2 Mbit/s PHY,那么您将把性能提高到最大值。

See https://www.novelbits.io/bluetooth-5-speed-maximum-throughput/ .请参阅https://www.novelbits.io/bluetooth-5-speed-maximum-throughput/

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

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