【BLE怎么设置一次发送超过20byte数据?】

大树学长 2017-03-08 11:10:55
1、设置一次发送超过20字节,这个方法要怎么弄,网上说的不明白
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);

if (status == BluetoothGatt.GATT_SUCCESS) {
this.supportedMTU = mtu;//local var to record MTU size
}
}
2、BLE数据包空中传输规定了20byte一次,就算改变了MTU,实际传输的时候还是要分成很多个包的。是这样子的吗?
...全文
1685 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jack_blunt 2018-03-23
  • 打赏
  • 举报
回复
楼上给出分包处理,注意的是包与包之间的延时80-100毫秒;
ink_s 2017-03-10
  • 打赏
  • 举报
回复
分包 分包 分包
幻影宇寰 2017-03-10
  • 打赏
  • 举报
回复
你上层可以一次发送超过20字节,但是必须要在发送给设备之前做分包处理
幻影宇寰 2017-03-10
  • 打赏
  • 举报
回复
引用 2 楼 bigtree_mfc 的回复:
[quote=引用 1 楼 xiaoyaoyou1212 的回复:] 每次只能发送20字节,是需要分包处理的,可以按照如下思路分包

protected Queue<DataInfo> splitPacketFor20Byte(byte[] data) {
        Queue<DataInfo> dataInfoQueue = new LinkedList<>();
        if (data != null) {
            int index = 0;
            do {
                byte[] surplusData = new byte[data.length - index];
                byte[] currentData;
                System.arraycopy(data, index, surplusData, 0, data.length - index);
                if (surplusData.length <= 20) {
                    currentData = new byte[surplusData.length];
                    System.arraycopy(surplusData, 0, currentData, 0, surplusData.length);
                    index += surplusData.length;
                } else {
                    currentData = new byte[20];
                    System.arraycopy(data, index, currentData, 0, 20);
                    index += 20;
                }
                DataInfo dataInfo = new DataInfo();
                dataInfo.setData(currentData);
                dataInfo.setDataType(DataInfo.DataType.SEND);
                dataInfoQueue.offer(dataInfo);
            } while (index < data.length);
        }
        return dataInfoQueue;
    }
难道不能设置一次发超过20字节的数据么[/quote] 不行,这个是协议栈的限制, 每包只能发送最多20字节,而且每包发送间隔需要一两百毫秒
大树学长 2017-03-10
  • 打赏
  • 举报
回复
引用 1 楼 xiaoyaoyou1212 的回复:
每次只能发送20字节,是需要分包处理的,可以按照如下思路分包

protected Queue<DataInfo> splitPacketFor20Byte(byte[] data) {
        Queue<DataInfo> dataInfoQueue = new LinkedList<>();
        if (data != null) {
            int index = 0;
            do {
                byte[] surplusData = new byte[data.length - index];
                byte[] currentData;
                System.arraycopy(data, index, surplusData, 0, data.length - index);
                if (surplusData.length <= 20) {
                    currentData = new byte[surplusData.length];
                    System.arraycopy(surplusData, 0, currentData, 0, surplusData.length);
                    index += surplusData.length;
                } else {
                    currentData = new byte[20];
                    System.arraycopy(data, index, currentData, 0, 20);
                    index += 20;
                }
                DataInfo dataInfo = new DataInfo();
                dataInfo.setData(currentData);
                dataInfo.setDataType(DataInfo.DataType.SEND);
                dataInfoQueue.offer(dataInfo);
            } while (index < data.length);
        }
        return dataInfoQueue;
    }
难道不能设置一次发超过20字节的数据么
幻影宇寰 2017-03-08
  • 打赏
  • 举报
回复
每次只能发送20字节,是需要分包处理的,可以按照如下思路分包

protected Queue<DataInfo> splitPacketFor20Byte(byte[] data) {
        Queue<DataInfo> dataInfoQueue = new LinkedList<>();
        if (data != null) {
            int index = 0;
            do {
                byte[] surplusData = new byte[data.length - index];
                byte[] currentData;
                System.arraycopy(data, index, surplusData, 0, data.length - index);
                if (surplusData.length <= 20) {
                    currentData = new byte[surplusData.length];
                    System.arraycopy(surplusData, 0, currentData, 0, surplusData.length);
                    index += surplusData.length;
                } else {
                    currentData = new byte[20];
                    System.arraycopy(data, index, currentData, 0, 20);
                    index += 20;
                }
                DataInfo dataInfo = new DataInfo();
                dataInfo.setData(currentData);
                dataInfo.setDataType(DataInfo.DataType.SEND);
                dataInfoQueue.offer(dataInfo);
            } while (index < data.length);
        }
        return dataInfoQueue;
    }

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧