sendPart method

Future<void> sendPart(
  1. int position,
  2. Uint8List data
)

sendPart function which is used to send parts to the esp32

Implementation

Future<void> sendPart(int position, Uint8List data) async {
  final bleRepo = BleRepository();
  int start = (position * part);
  int end = (position + 1) * part;
  if (data.length < end) {
    end = data.length;
  }
  int parts = (end - start) ~/ mtu;
  print("Parts are $parts");
  for (int i = 0; i < parts; i++) {
    print(
        "created $i parts"); //<------------------------------ Print to debug
    Uint8List toSend = Uint8List(mtu + 2);
    toSend[0] = 0XFB;
    toSend[1] = i;
    int variable = 2;
    for (int y = 0; y < mtu; y++) {
      /// toSend.append(data[(position * PART) + (MTU * i) + y])

      int x = data[(position * part) + (mtu * i) + y];
      Uint8List list2 = Uint8List.fromList([x]);

      /// Copy the contents of list1 into the beginning of combinedList
      toSend.setRange(variable, variable + list2.length, list2);
      variable = variable + list2.length;
    }

    /// print statement below should be replaced with actual sending code

    print("Before writing data, to send length is ${toSend.length}");
    await bleRepo.writeDataCharacteristic(writeCharacteristic, toSend);
    //print("--- send data at this line in code ---");  //<------------------------------ Print to debug
  }
  if ((end - start) % mtu != 0) {
    print("in other if");
    int rem = (end - start) % mtu;
    Uint8List toSend = Uint8List(rem + 2);
    toSend[0] = 0XFB;
    toSend[1] = parts;
    int variable = 2;
    for (int y = 0; y < rem; y++) {
      int x = data[(position * part) + (mtu * parts) + y];
      Uint8List list2 = Uint8List.fromList([x]);

      /// Copy the contents of list1 into the beginning of combinedList
      toSend.setRange(variable, variable + list2.length, list2);
      variable = variable + list2.length;
    }

    /// print statement below should be replaced with actual sending code
    print("2nd write");
    await bleRepo.writeDataCharacteristic(writeCharacteristic, toSend);
  }

  Uint8List update = Uint8List.fromList([
    0xFC,
    ((end - start) ~/ 256),
    ((end - start) % 256),
    (position ~/ 256),
    (position % 256)
  ]);

  /// print statement below should be replaced with actual sending code
  await bleRepo.writeDataCharacteristic(writeCharacteristic, update);
  print("---- send update on this line of code --- $update");
}