CodeMinion / another_brother

Another Brother Flutter SDK
BSD 3-Clause "New" or "Revised" License
21 stars 22 forks source link

Exception in debug mode (ios-no-type-b branch) #12

Closed BentEngbers closed 3 years ago

BentEngbers commented 3 years ago

Hi @CodeMinion, When printing from IOS, the library throws an exception (after successfully printing) at line 1730 in the file printer_info.dart. Screenshot 2021-09-17 at 18 18 58 This exception occurs when calling the Printer.printText() function. I think these are the logs that are related to it (but i could be mistaken).

[connection] nw_endpoint_handler_set_adaptive_write_handler [C2 104.18.244.88:443 ready channel-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, dns)] unregister notification for write_timeout failed
[connection] nw_endpoint_handler_set_keepalive_handler [C2 104.18.244.88:443 ready channel-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, dns)] unregister notification for keepalive failed
[] nw_protocol_instance_access_flow_state [C1.1.1:2] Failed to find flow 107b0bae8
[] nw_protocol_instance_access_flow_state [C1.1.1:2] Failed to find flow 107a09918
[connection] nw_endpoint_handler_set_adaptive_read_handler [C1.1.1 104.18.244.88:443 ready channel-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, dns)] unregister notification for read_timeout failed
[connection] nw_endpoint_handler_set_adaptive_write_handler [C1.1.1 104.18.244.88:443 ready channel-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, dns)] unregister notification for write_timeout failed
[connection] nw_endpoint_handler_set_keepalive_handler [C1.1.1 104.18.244.88:443 ready channel-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, dns)] unregister notification for keepalive failed
[] nw_protocol_instance_access_flow_state [C1.1.1:2] Failed to find flow 107b0bae8
flutter: Device print: {modelName: Brother QL-820NWB, serNo: XXXXXXXXX, ipAddress: 192.168.2.254, macAddress:XX:XX:XX:XX:XX:XX, nodeName: XXXXXXXXXXXXXXX, location: }
flutter: Print Result: true
flutter: Connected to printer? true
[connection] nw_endpoint_handler_set_adaptive_read_handler [C3 192.168.2.254:9100 ready socket-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, dns)] unregister notification for read_timeout failed
[connection] nw_endpoint_handler_set_adaptive_write_handler [C3 192.168.2.254:9100 ready socket-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, dns)] unregister notification for write_timeout failed
flutter: Print Result: {labelType: -1, maxOfBatteryResidualQuantityLevel: -1, labelId: -1, isACConnected: {id: -1, name: Unknown}, batteryResidualQuantityLevel: -1, isBatteryMounted: {id: -1}, batteryLevel: -1, errorCode: {name: ERROR_NONE, id: -1}}

This is the code i use to call your library:

const LabelIndex_DK44205_62mm = 15;

Future<void> printLabelAPI(NetPrinter device) async {
  // If the device is the empty device return;
  if (device.ipAddress.isEmpty) {
    return;
  }

  final printer = Printer();
  var _printerInfo = setupPrinter(device);
  await printer.setPrinterInfo(_printerInfo);

  // Android trick method
  // _printerInfo.labelNameIndex = await getLabelNameIndex(printer);
  // await printer.setPrinterInfo(_printerInfo);
  await connectPrinterAndPrint(printer);
}

Future<void> connectPrinterAndPrint(Printer printer) async {
  // This is used for testing the printer.
  TextStyle style =
      TextStyle(color: Colors.black, fontSize: 30, fontWeight: FontWeight.bold);
  ParagraphBuilder paragraphBuilder = ParagraphBuilder(ParagraphStyle(
    fontSize: style.fontSize,
    fontFamily: style.fontFamily,
    fontStyle: style.fontStyle,
    fontWeight: style.fontWeight,
    textAlign: TextAlign.center,
    maxLines: 10,
  ))
    ..pushStyle(style.getTextStyle())
    ..addText("Test 123 Test 123");

  var paragraph = paragraphBuilder.build()
    ..layout(ParagraphConstraints(width: 300));

  bool success = await printer.startCommunication();
  print("Connected to printer? $success");

  var res = await printer.printText(paragraph);
  print("Result: ${res}");

  success = await printer.endCommunication();
}

PrinterInfo setupPrinter(NetPrinter device) {
  var _printInfo = PrinterInfo();

  _printInfo.port = Port.NET;
  _printInfo.ipAddress = device.ipAddress;

  print("Device print: $device");

  _printInfo.macAddress = device.macAddress;
  // Constant for now.
  _printInfo.printerModel = Model.QL_820NWB;
  _printInfo.printMode = PrintMode.FIT_TO_PAGE;
  _printInfo.isAutoCut = true;

  // Important to set this to 0 if using android trick.
  // Otherwise will not work.
  _printInfo.labelNameIndex = LabelIndex_DK44205_62mm;

  return _printInfo;
}

///
/// Currently only available for Android
/// For getting the label automaticaly.
///
Future<int> getLabelNameIndex(Printer printer) async {
  await printer.startCommunication();
  await printer.endCommunication();
  LabelInfo labelInfo = await printer.getLabelInfo();
  return labelInfo.labelNameIndex;
}

General Info: using ios-no-type-b branch Flutter v2.5.0 IOS: v14.7.1 Xcode: v12.5.1 (12E507) Printer: QL-820NWB

CodeMinion commented 3 years ago

Hi @BentEngbers ,

Thank for the heads up and the detailed report! I'll take a look and have a fix out this weekend along with that other update.

Thanks again!

CodeMinion commented 3 years ago

Hi @BentEngbers ,

Thanks again for the report. This has now been resolved on the on the latest commit of the branch as well as the official version.

Because you are using the branch make sure to use "Pub upgrade" to get the latest version of the branch.

I'll leave this issue open for your to confirm it's working fine.

Thanks again and don't hesitate to reach out if you run into any other issues.

BentEngbers commented 3 years ago

Thanks for the quick fix! The issue has been resolved!