Delphi 10.3 - Android Intent to Zebra Printer with HashMap

886 views Asked by At

I have foud the following Java code in the Zebra documentation.

https://www.zebra.com/us/en/products/software/barcode-printers/link-os/print-connect.html

Button templatePrint = (Button) findViewById(R.id.templatePrint);
templatePrint.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    // Define a hash map of variable data
    // Strings used for keys will be replaced by their corresponding values in your template file's ZPL
    HashMap<String, String> variableData = new HashMap<>();
    variableData.put("%PRODUCT_NAME%", "Apples");
    variableData.put("%MSRP%", "$1.00");
    variableData.put("%PCT%", "50");
    variableData.put("%FINAL%", "$0.50");
    variableData.put("%UPC_CODE%", "12345678");
   Intent intent = new Intent();
   intent.setComponent(new ComponentName("com.zebra.printconnect",
   "com.zebra.printconnect.print.TemplatePrintService"));
   intent.putExtra("com.zebra.printconnect.PrintService.TEMPLATE_FILE_NAME", "PriceTagTemplate.zpl");
   intent.putExtra("com.zebra.printconnect.PrintService.VARIABLE_DATA", variableData);
   intent.putExtra("com.zebra.printconnect.PrintService.RESULT_RECEIVER", buildIPCSafeReceiver(new
       ResultReceiver(null) {
       @Override
       protected void onReceiveResult(int resultCode, Bundle resultData) {
         if (resultCode == 0) { // Result code 0 indicates success
           // Handle successful print
         } else {
           // Handle unsuccessful print
           // Error message (null on successful print)
         String errorMessage = resultData.getString("com.zebra.printconnect.PrintService.ERROR_MESSAGE");
         }
       }
    }));
    startService(intent);
  }
 });

I'm using it in Delphi 10.3 and I can successfully print, but I can't pass "VARIABLE_DATA", how can I manage it?

procedure TfrmMain.PrintIntent;
var
  Intent: JIntent;
  NativeComponent : JComponentName;
  PackageName, AppName : JString;
begin
  Intent := TJIntent.Create;
  PackageName := StringToJString('com.zebra.printconnect');
  AppName := StringToJString('com.zebra.printconnect.print.TemplatePrintService');
  NativeComponent := TJComponentName.JavaClass.init(PackageName, AppName);
  Intent.SetComponent(NativeComponent);
  Intent.putExtra(StringToJString('com.zebra.printconnect.PrintService.TEMPLATE_FILE_NAME'), StringToJString('PriceTagTemplate.zpl'));
  // Intent.putExtra('com.zebra.printconnect.PrintService.VARIABLE_DATA', ???);
  MainActivity.startService(Intent);
end;
1

There are 1 answers

1
teo.red86 On

Thanks to Dave Nottage. It works:

procedure TfrmMain.PrintIntent;
var
  Intent: JIntent;
  NativeComponent : JComponentName;
  PackageName, AppName : JString;
  Variable: JHashMap;
begin
  Intent := TJIntent.Create;
  PackageName := StringToJString('com.zebra.printconnect');
  AppName := StringToJString('com.zebra.printconnect.print.TemplatePrintService');
  NativeComponent := TJComponentName.JavaClass.init(PackageName, AppName);
  Intent.SetComponent(NativeComponent);
  Intent.putExtra(StringToJString('com.zebra.printconnect.PrintService.TEMPLATE_FILE_NAME'), StringToJString('PriceTagTemplate.zpl'));
  //
  Variable := TJHashMap.Create;
  Variable.put(StringToJString('%PRODUCT_NAME%'),StringToJString('Apples'));
  Variable.put(StringToJString('%MSRP%'),StringToJString('$1.00'));
  Variable.put(StringToJString('%PCT%'),StringToJString('50'));
  Variable.put(StringToJString('%FINAL%'),StringToJString('$0.50'));
  Variable.put(StringToJString('%UPC_CODE%'),StringToJString('12345678'));

  Intent.putExtra (StringToJString('com.zebra.printconnect.PrintService.VARIABLE_DATA'), TJSerializable.Wrap(JObjectToID(Variable));
  //
  MainActivity.startService(Intent);
end;