Android ble蓝牙传输丢包问题

lz_0702 2016-11-02 10:11:39
求大神讲解一下,在Android ble蓝牙在传输大于20个字节的数据时,分包发送会出现丢包的情况。我在回调方法中了做了一下操作
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);

Log.e("getValue()!!", Arrays.toString(characteristic.getValue()));
if (characteristic.getValue().length == 20) {
backData = characteristic.getValue();
isSendSuccess = true;
backNumber++;
}
Log.e("isSendSuccess!", Boolean.toString(isSendSuccess));
}
但是每次回调的次数跟内容都不稳定,所以不好判断哪里出现丢包。
哪位大神碰到过类似的情况,给点意见,谢过啦
...全文
2097 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lfqsy 2017-01-03
  • 打赏
  • 举报
回复
楼主,我开发BLE发现服务了,但是始终无法获取BLE返回的数据,为什么
滑大人 2016-12-30
  • 打赏
  • 举报
回复
楼主你好 刚刚接触BLE 现在还不能做到收发数据 求指教
幻影宇寰 2016-11-02
  • 打赏
  • 举报
回复
蓝牙每次只能收发20字节数据,如果大于必须采用分包机制进行收发,下面是我写的接收分包处理:

protected Queue<DataInfo> splitPacketFor20Byte(byte[] data) {
        LinkedList dataInfoQueue = new LinkedList();
        if(data != null) {
            int index = 0;

            do {
                byte[] surplusData = new byte[data.length - index];
                System.arraycopy(data, index, surplusData, 0, data.length - index);
                byte[] currentData;
                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(DataType.SEND);
                dataInfoQueue.offer(dataInfo);
            } while(index < data.length);
        }

        return dataInfoQueue;
    }
我整理过针对BLE的基础操作框架,在Github上有源码和使用demo,希望对你有所帮助!使用介绍地址:http://blog.csdn.net/xiaoyaoyou1212/article/details/52346904

80,349

社区成员

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

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