蓝牙发送超过20个字节出错(BLE设备发送超过20个字节)

解决方案

BLE数据包空中传输规定了20byte一次,如果单包数据超过20个字节要分包发送,我测试两包数据间隔0.1毫秒也是ok的(启示:如果能把收到的命令用队列保存起来,那么两条完整的命令间隔时间非常短也是可以处理的)

ios(swift)参考代码

 // MARK: - 发数据
    func sendBytes()  {
        var send:[UInt8] = [];
        var mode:Int = 20;
        if bytes.count > mode {
            var i:Int = 1;
            var total = bytes.count/mode+1;
            while i<=total {
                if i==total{
                    if (i-1)*mode <= bytes.count-1 {
                        send = Array(bytes[(i-1)*mode...(bytes.count-1)]);
                    }else{
                        break
                    }
                }else{
                    send = Array(bytes[(i-1)*mode...(mode*i-1)]);
                }
               
                let data:Data = Data.init(bytes: send);
                let nsdata:NSData = NSData.init(bytes: send, length: send.count);
                 XXCoreBluetoothManager.shareInstance().writeData(with: XXCoreBluetoothManager.shareInstance().characteristic, data: data, completion: nil);
                print("发:\(nsdata)");
                //两包数据间间隔0.1毫秒
                Thread.sleep(forTimeInterval: 0.0001)
                i+=1;
            }
        }else{
            let data:Data = Data.init(bytes: bytes);
            let nsdata:NSData = NSData.init(bytes: bytes, length: bytes.count);
            MyPrint("发:\(nsdata)");
            //调试  testDebug
            XXCoreBluetoothManager.shareInstance().writeData(with: XXCoreBluetoothManager.shareInstance().characteristic, data: data, completion: nil);
        }
        
        
    }
    

android参考代码

在这里插入图片描述

相关信息和连接

Swift 分包发送蓝牙数据

链接: ble一次传输20字节分析

你说的是BLE蓝牙么?不同的芯片做的蓝牙模块是不一样的,SKYLAB的SKB369用的是Nodic新款nRF52832芯片,10m内的范围,稳定传输为2KByte/s,这就不止你说的20个字节哦,1KB=1024个字节呢!参考:http://www.skylab.com.cn/productview-110-4.2lanya_mokuai_SKB369.html有规格书,可以研究下

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值