0
点赞
收藏
分享

微信扫一扫

flutter_blue解决分包问题

陆公子521 2022-04-13 阅读 56
flutter

Flutter_blue 发送包长度大于20时 解决分包问题

 void write(characteristics,device) async{
    final mtu = await device.mtu.first;
    print("mtu:$mtu");
    var buffer = new WriteBuffer();
    List<int> ll=[0x4E,0x57,0x00,0x13,0x00,0x00,0x00,0x00,0x06,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x68,0x00,0x00,0x01,0x27];
    int pos=0;
    int bytes=ll.length;
    print('开始写入数据');
     if(ll.length>0){
      if(ll.length<mtu){
        await characteristics.write(ll,withoutResponse: true);
      }else{
        while(bytes>0){
          List<int> tmp;
          buffer=new WriteBuffer();
          if(bytes>mtu){
            tmp=ll.sublist(pos,pos+mtu);
            pos+=mtu;
            bytes-=mtu;
            tmp.forEach((element) {
              buffer.putUint8(element);
            });
            final ByteData written=buffer.done();
            final ReadBuffer read = ReadBuffer(written);
            characteristics.write(read.getUint8List(mtu),withoutResponse:true).asStream();
          }else{
            return await Future.delayed(Duration(milliseconds: 25),(){
              tmp=ll.sublist(pos,pos+bytes);
              pos+=bytes;
              bytes-=bytes;
              tmp.forEach((element) {
                buffer.putUint8(element);
              });
              final ByteData written=buffer.done();
              final ReadBuffer read = ReadBuffer(written);
              characteristics.write(read.getUint8List(pos%mtu),withoutResponse:true).asStream();
            });
          }
        }
      }
    }
    print('写入数据完成$buffer');
  }
举报

相关推荐

0 条评论