文章目录
解决方案
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参考代码
相关信息和连接
链接: 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有规格书,可以研究下