### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: LAMW - (SOLVED) simple esc/POS printing bridge?  (Read 1632 times)

#### Mongkey

• Sr. Member
• Posts: 378
##### LAMW - (SOLVED) simple esc/POS printing bridge?
« on: May 28, 2023, 02:46:21 am »
Library:
https://github.com/DantSu/ESCPOS-ThermalPrinter-Android

Code: Pascal  [Select][+][-]
1. EscPosPrinter printer = new EscPosPrinter(BluetoothPrintersConnections.selectFirstPaired(), 203, 48f, 32);
2. printer
3.     .printFormattedText(
4.         "[C]<img>" + PrinterTextParserImg.bitmapToHexadecimalString(printer, this.getApplicationContext().getResources().getDrawableForDensity(R.drawable.logo, DisplayMetrics.DENSITY_MEDIUM))+"</img>\n" +
5.         "[L]\n" +
6.         "[C]<u><font size='big'>ORDER N°045</font></u>\n" +
7.         "[L]\n" +
8.         "[C]================================\n" +
9.         "[L]\n" +
10.         "[L]<b>BEAUTIFUL SHIRT</b>[R]9.99e\n" +
11.         "[L]  + Size : S\n" +
12.         "[L]\n" +
13.         "[L]<b>AWESOME HAT</b>[R]24.99e\n" +
14.         "[L]  + Size : 57/58\n" +
15.         "[L]\n" +
16.         "[C]--------------------------------\n" +
17.         "[R]TOTAL PRICE :[R]34.98e\n" +
18.         "[R]TAX :[R]4.23e\n" +
19.         "[L]\n" +
20.         "[C]================================\n" +
21.         "[L]\n" +
22.         "[L]<font size='tall'>Customer :</font>\n" +
23.         "[L]Raymond DUPONT\n" +
24.         "[L]5 rue des girafes\n" +
25.         "[L]31547 PERPETES\n" +
26.         "[L]Tel : +33801201456\n" +
27.         "[L]\n" +
28.         "[C]<barcode type='ean13' height='10'>831254784551</barcode>\n" +
29.         "[C]<qrcode size='20'>https://dantsu.com/</qrcode>"
30.     );
31.

Code: Pascal  [Select][+][-]
1. package com.dantsu.thermalprinter;
2.
3. import androidx.annotation.NonNull;
4. import androidx.appcompat.app.AppCompatActivity;
5. import androidx.core.app.ActivityCompat;
6. import androidx.core.content.ContextCompat;
7.
8. import android.Manifest;
9. import android.annotation.SuppressLint;
10. import android.app.AlertDialog;
11. import android.app.PendingIntent;
12. import android.content.BroadcastReceiver;
13. import android.content.Context;
14. import android.content.Intent;
15. import android.content.IntentFilter;
16. import android.content.pm.PackageManager;
17. import android.hardware.usb.UsbDevice;
18. import android.hardware.usb.UsbManager;
19. import android.os.Bundle;
20. import android.util.DisplayMetrics;
21. import android.util.Log;
22. import android.widget.Button;
23. import android.widget.EditText;
24.
25. import com.dantsu.escposprinter.connection.DeviceConnection;
26. import com.dantsu.escposprinter.connection.bluetooth.BluetoothConnection;
27. import com.dantsu.escposprinter.connection.bluetooth.BluetoothPrintersConnections;
28. import com.dantsu.escposprinter.connection.tcp.TcpConnection;
29. import com.dantsu.escposprinter.connection.usb.UsbConnection;
30. import com.dantsu.escposprinter.connection.usb.UsbPrintersConnections;
31. import com.dantsu.escposprinter.textparser.PrinterTextParserImg;
32. import com.dantsu.thermalprinter.async.AsyncBluetoothEscPosPrint;
33. import com.dantsu.thermalprinter.async.AsyncEscPosPrint;
34. import com.dantsu.thermalprinter.async.AsyncEscPosPrinter;
35. import com.dantsu.thermalprinter.async.AsyncTcpEscPosPrint;
36. import com.dantsu.thermalprinter.async.AsyncUsbEscPosPrint;
37.
38. import java.text.SimpleDateFormat;
39. import java.util.Date;
40.
41. public class MainActivity extends AppCompatActivity {
42.
43.
44.     @Override
45.     protected void onCreate(Bundle savedInstanceState) {
46.         super.onCreate(savedInstanceState);
47.         setContentView(R.layout.activity_main);
48.         Button button = (Button) this.findViewById(R.id.button_bluetooth_browse);
49.         button.setOnClickListener(view -> browseBluetoothDevice());
50.         button = (Button) findViewById(R.id.button_bluetooth);
51.         button.setOnClickListener(view -> printBluetooth());
52.         button = (Button) this.findViewById(R.id.button_usb);
53.         button.setOnClickListener(view -> printUsb());
54.         button = (Button) this.findViewById(R.id.button_tcp);
55.         button.setOnClickListener(view -> printTcp());
56.
57.     }
58.
59.
60.     /*==============================================================================================
61.     ======================================BLUETOOTH PART============================================
62.     ==============================================================================================*/
63.
64.     public interface OnBluetoothPermissionsGranted {
65.         void onPermissionsGranted();
66.     }
67.
68.     public static final int PERMISSION_BLUETOOTH = 1;
69.     public static final int PERMISSION_BLUETOOTH_ADMIN = 2;
70.     public static final int PERMISSION_BLUETOOTH_CONNECT = 3;
71.     public static final int PERMISSION_BLUETOOTH_SCAN = 4;
72.
73.     public OnBluetoothPermissionsGranted onBluetoothPermissionsGranted;
74.
75.     @Override
76.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
77.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
78.         if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
79.             switch (requestCode) {
80.                 case MainActivity.PERMISSION_BLUETOOTH:
81.                 case MainActivity.PERMISSION_BLUETOOTH_ADMIN:
82.                 case MainActivity.PERMISSION_BLUETOOTH_CONNECT:
83.                 case MainActivity.PERMISSION_BLUETOOTH_SCAN:
84.                     this.checkBluetoothPermissions(this.onBluetoothPermissionsGranted);
85.                     break;
86.             }
87.         }
88.     }
89.
90.     public void checkBluetoothPermissions(OnBluetoothPermissionsGranted onBluetoothPermissionsGranted) {
91.         this.onBluetoothPermissionsGranted = onBluetoothPermissionsGranted;
92.         if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.S && ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
93.             ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH}, MainActivity.PERMISSION_BLUETOOTH);
94.         } else if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.S && ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADMIN) != PackageManager.PERMISSION_GRANTED) {
95.             ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_ADMIN}, MainActivity.PERMISSION_BLUETOOTH_ADMIN);
96.         } else if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S && ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
97.             ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_CONNECT}, MainActivity.PERMISSION_BLUETOOTH_CONNECT);
98.         } else if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S && ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
99.             ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_SCAN}, MainActivity.PERMISSION_BLUETOOTH_SCAN);
100.         } else {
101.             this.onBluetoothPermissionsGranted.onPermissionsGranted();
102.         }
103.     }
104.
105.     private BluetoothConnection selectedDevice;
106.
107.     public void browseBluetoothDevice() {
108.         this.checkBluetoothPermissions(() -> {
109.             final BluetoothConnection[] bluetoothDevicesList = (new BluetoothPrintersConnections()).getList();
110.
111.             if (bluetoothDevicesList != null) {
112.                 final String[] items = new String[bluetoothDevicesList.length + 1];
113.                 items[0] = "Default printer";
114.                 int i = 0;
115.                 for (BluetoothConnection device : bluetoothDevicesList) {
116.                     items[++i] = device.getDevice().getName();
117.                 }
118.
119.                 AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
120.                 alertDialog.setTitle("Bluetooth printer selection");
121.                 alertDialog.setItems(
122.                     items,
123.                     (dialogInterface, i1) -> {
124.                         int index = i1 - 1;
125.                         if (index == -1) {
126.                             selectedDevice = null;
127.                         } else {
128.                             selectedDevice = bluetoothDevicesList[index];
129.                         }
130.                         Button button = (Button) findViewById(R.id.button_bluetooth_browse);
131.                         button.setText(items[i1]);
132.                     }
133.                 );
134.
135.                 AlertDialog alert = alertDialog.create();
136.                 alert.setCanceledOnTouchOutside(false);
137.                 alert.show();
138.             }
139.         });
140.
141.     }
142.
143.     public void printBluetooth() {
144.         this.checkBluetoothPermissions(() -> {
145.             new AsyncBluetoothEscPosPrint(
146.                 this,
147.                 new AsyncEscPosPrint.OnPrintFinished() {
148.                     @Override
149.                     public void onError(AsyncEscPosPrinter asyncEscPosPrinter, int codeException) {
150.                         Log.e("Async.OnPrintFinished", "AsyncEscPosPrint.OnPrintFinished : An error occurred !");
151.                     }
152.
153.                     @Override
154.                     public void onSuccess(AsyncEscPosPrinter asyncEscPosPrinter) {
155.                         Log.i("Async.OnPrintFinished", "AsyncEscPosPrint.OnPrintFinished : Print is finished !");
156.                     }
157.                 }
158.             )
159.                 .execute(this.getAsyncEscPosPrinter(selectedDevice));
160.         });
161.     }
162.
163.     /*==============================================================================================
164.     ===========================================USB PART=============================================
165.     ==============================================================================================*/
166.
167.     private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
168.     private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
169.         public void onReceive(Context context, Intent intent) {
170.             String action = intent.getAction();
171.             if (MainActivity.ACTION_USB_PERMISSION.equals(action)) {
172.                 synchronized (this) {
173.                     UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
174.                     UsbDevice usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
175.                     if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
176.                         if (usbManager != null && usbDevice != null) {
177.                             new AsyncUsbEscPosPrint(
178.                                 context,
179.                                 new AsyncEscPosPrint.OnPrintFinished() {
180.                                     @Override
181.                                     public void onError(AsyncEscPosPrinter asyncEscPosPrinter, int codeException) {
182.                                         Log.e("Async.OnPrintFinished", "AsyncEscPosPrint.OnPrintFinished : An error occurred !");
183.                                     }
184.
185.                                     @Override
186.                                     public void onSuccess(AsyncEscPosPrinter asyncEscPosPrinter) {
187.                                         Log.i("Async.OnPrintFinished", "AsyncEscPosPrint.OnPrintFinished : Print is finished !");
188.                                     }
189.                                 }
190.                             )
191.                                 .execute(getAsyncEscPosPrinter(new UsbConnection(usbManager, usbDevice)));
192.                         }
193.                     }
194.                 }
195.             }
196.         }
197.     };
198.
199.     public void printUsb() {
200.         UsbConnection usbConnection = UsbPrintersConnections.selectFirstConnected(this);
201.         UsbManager usbManager = (UsbManager) this.getSystemService(Context.USB_SERVICE);
202.
203.         if (usbConnection == null || usbManager == null) {
204.             new AlertDialog.Builder(this)
205.                 .setTitle("USB Connection")
206.                 .setMessage("No USB printer found.")
207.                 .show();
208.             return;
209.         }
210.
211.         PendingIntent permissionIntent = PendingIntent.getBroadcast(
212.             this,
213.             0,
214.             new Intent(MainActivity.ACTION_USB_PERMISSION),
215.             android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0
216.         );
217.         IntentFilter filter = new IntentFilter(MainActivity.ACTION_USB_PERMISSION);
218.         registerReceiver(this.usbReceiver, filter);
219.         usbManager.requestPermission(usbConnection.getDevice(), permissionIntent);
220.     }
221.
222.     /*==============================================================================================
223.     =========================================TCP PART===============================================
224.     ==============================================================================================*/
225.
226.     public void printTcp() {
227.         final EditText ipAddress = (EditText) this.findViewById(R.id.edittext_tcp_ip);
228.         final EditText portAddress = (EditText) this.findViewById(R.id.edittext_tcp_port);
229.
230.         try {
231.             new AsyncTcpEscPosPrint(
232.                 this,
233.                 new AsyncEscPosPrint.OnPrintFinished() {
234.                     @Override
235.                     public void onError(AsyncEscPosPrinter asyncEscPosPrinter, int codeException) {
236.                         Log.e("Async.OnPrintFinished", "AsyncEscPosPrint.OnPrintFinished : An error occurred !");
237.                     }
238.
239.                     @Override
240.                     public void onSuccess(AsyncEscPosPrinter asyncEscPosPrinter) {
241.                         Log.i("Async.OnPrintFinished", "AsyncEscPosPrint.OnPrintFinished : Print is finished !");
242.                     }
243.                 }
244.             )
245.                 .execute(
246.                     this.getAsyncEscPosPrinter(
247.                         new TcpConnection(
248.                             ipAddress.getText().toString(),
249.                             Integer.parseInt(portAddress.getText().toString())
250.                         )
251.                     )
252.                 );
253.         } catch (NumberFormatException e) {
254.             new AlertDialog.Builder(this)
255.                 .setTitle("Invalid TCP port address")
256.                 .setMessage("Port field must be an integer.")
257.                 .show();
258.             e.printStackTrace();
259.         }
260.     }
261.
262.     /*==============================================================================================
263.     ===================================ESC/POS PRINTER PART=========================================
264.     ==============================================================================================*/
265.
266.     /**
267.      * Asynchronous printing
268.      */
269.     @SuppressLint("SimpleDateFormat")
270.     public AsyncEscPosPrinter getAsyncEscPosPrinter(DeviceConnection printerConnection) {
271.         SimpleDateFormat format = new SimpleDateFormat("'on' yyyy-MM-dd 'at' HH:mm:ss");
272.         AsyncEscPosPrinter printer = new AsyncEscPosPrinter(printerConnection, 203, 48f, 32);
273.         return printer.addTextToPrint(
274.             "[C]<img>" + PrinterTextParserImg.bitmapToHexadecimalString(printer, this.getApplicationContext().getResources().getDrawableForDensity(R.drawable.logo, DisplayMetrics.DENSITY_MEDIUM)) + "</img>\n" +
275.                 "[L]\n" +
276.                 "[C]<u><font size='big'>ORDER N°045</font></u>\n" +
277.                 "[L]\n" +
278.                 "[C]<u type='double'>" + format.format(new Date()) + "</u>\n" +
279.                 "[C]\n" +
280.                 "[C]================================\n" +
281.                 "[L]\n" +
282.                 "[L]<b>BEAUTIFUL SHIRT</b>[R]9.99€\n" +
283.                 "[L]  + Size : S\n" +
284.                 "[L]\n" +
285.                 "[L]<b>AWESOME HAT</b>[R]24.99€\n" +
286.                 "[L]  + Size : 57/58\n" +
287.                 "[L]\n" +
288.                 "[C]--------------------------------\n" +
289.                 "[R]TOTAL PRICE :[R]34.98€\n" +
290.                 "[R]TAX :[R]4.23€\n" +
291.                 "[L]\n" +
292.                 "[C]================================\n" +
293.                 "[L]\n" +
294.                 "[L]<u><font color='bg-black' size='tall'>Customer :</font></u>\n" +
295.                 "[L]Raymond DUPONT\n" +
296.                 "[L]5 rue des girafes\n" +
297.                 "[L]31547 PERPETES\n" +
298.                 "[L]Tel : +33801201456\n" +
299.                 "\n" +
300.                 "[C]<barcode type='ean13' height='10'>831254784551</barcode>\n" +
301.                 "[L]\n" +
302.                 "[C]<qrcode size='20'>https://dantsu.com/</qrcode>\n"
303.         );
304.     }
305. }
« Last Edit: June 03, 2023, 03:06:08 am by Mongkey »

#### jmpessoa

• Hero Member
• Posts: 2249
##### Re: LAMW - simple esc/POS printing bridge?
« Reply #1 on: May 28, 2023, 08:40:57 pm »

Ok. I will try implement a "jEscPosPrinter"  LAMW component!

Thank you!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

#### Mongkey

• Sr. Member
• Posts: 378
##### Re: LAMW - simple esc/POS printing bridge?
« Reply #2 on: May 29, 2023, 04:11:12 am »
Thank you mr. JMP

#### jmpessoa

• Hero Member
• Posts: 2249
##### Re: LAMW - simple esc/POS printing bridge?
« Reply #3 on: June 01, 2023, 01:51:58 am »

Done!

New "jsEscPosThermalPrinter"   component!

need:
1)AppCompat theme

2)gradle:
compileSdkVersion 33
targetSdkVersion 33

New demo "AppCompatEscPosThermalPrinterDemo1"
(please, test it!  I dont have a thermal printer....)

Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

#### Insid3Code

• New Member
• Posts: 20
• Code Immersion
##### Re: LAMW - simple esc/POS printing bridge?
« Reply #4 on: June 01, 2023, 05:41:58 pm »
Hi,
Tested...

Thank you for this Nice work!

#### jmpessoa

• Hero Member
• Posts: 2249
##### Re: LAMW - simple esc/POS printing bridge?
« Reply #5 on: June 01, 2023, 06:15:41 pm »
Quote
Bethoven composed much of his wonderful work without listening anymore....

Doing a simple coding without having an instrument to test it, is possible for mere mortals....

Thank you!
« Last Edit: June 02, 2023, 12:39:32 am by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

#### Mongkey

• Sr. Member
• Posts: 378
##### Re: LAMW - simple esc/POS printing bridge?
« Reply #6 on: June 02, 2023, 02:09:31 am »

Thank you!
Printing just like on kotlin

#### Mongkey

• Sr. Member
• Posts: 378
##### Re: LAMW - simple esc/POS printing bridge?
« Reply #7 on: June 02, 2023, 03:56:50 am »
Thank you, wishing you good health and happiness  Mr. JMP,

My test result:
It really slow, on every print just like on first print, seems getlist() printer first on all print,

Could you add selectFirstPaired() not getlist() on printing?

#### Mongkey

• Sr. Member
• Posts: 378
##### Re: LAMW - simple esc/POS printing bridge?
« Reply #8 on: June 02, 2023, 05:16:48 am »
I got another method very fast on printing Mr. JMP, you just need add call procedure on jbluetoothclient, image, custom font already added.

this attachment is the result
« Last Edit: June 02, 2023, 06:05:57 am by Mongkey »

#### jmpessoa

• Hero Member
• Posts: 2249
##### Re: LAMW - simple esc/POS printing bridge?
« Reply #9 on: June 02, 2023, 05:21:47 am »
Quote
Could you add selectFirstPaired() not getlist() on printing?

Yes, "selectFirstPaired()" is already the API used by  LAMW "InitConnection" for Bluetooth!

Quote
I got another method very fast on printing Mr. JMP....

What another method?
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

#### Mongkey

• Sr. Member
• Posts: 378
##### Re: LAMW - simple esc/POS printing bridge?
« Reply #10 on: June 02, 2023, 05:27:51 am »
jBluetoothclientsocket.java

you just need to make proper call procedure and improve, bridging from java to pas is hard for me  .

More and more simple step by step java to pas bridging tutorial would be appreciated

Ability:
custom font
image
unicode

need improvement:
adding barcode print ability would be nice.
« Last Edit: June 02, 2023, 05:39:51 am by Mongkey »

#### jmpessoa

• Hero Member
• Posts: 2249
##### Re: LAMW - simple esc/POS printing bridge?
« Reply #11 on: June 02, 2023, 08:27:41 am »
Quote
you just need to make proper call procedure and improve, bridging from java to pas is hard for me

Ok.

What functions from the new "jBluetoothclientsocket.java"  you need a bridge to be called from to Pascal/LAMW side?
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

#### Mongkey

• Sr. Member
• Posts: 378
##### Re: LAMW - simple esc/POS printing bridge?
« Reply #12 on: June 02, 2023, 09:23:24 am »
Hello Mr JMP, i already tried bridging, but still failed, how to solve?
Code: Java  [Select][+][-]
1.     public void print_Custom(String _message, int _size, int _align) {
2.         printCustom(_message, _size, _align); // total 32 char in a single line
3.     }
4.

Code: Pascal  [Select][+][-]
1.     procedure print_Custom(var _message: string; _size:integer; _align:integer);  overload;
2.

Code: Pascal  [Select][+][-]
1. procedure jBluetoothClientSocket.print_Custom(var _message: string; _size:integer; _align:integer);
2. begin
3.   //in designing component state: set value here...
4.   if FInitialized then
5.      jBluetoothClientSocket_print_Custom(gApp.jni.jEnv, FjObject, _message, _size, _align);
6. end;
7.

Code: Pascal  [Select][+][-]
1. procedure jBluetoothClientSocket_print_Custom(env: PJNIEnv; _jbluetoothclientsocket: JObject; var _message: string; _size: integer; _align: integer);
2. var
3.   jParams: array[0..0] of jValue;
4.   jMethod: jMethodID=nil;
5.   jCls: jClass=nil;
6. begin
7.   jParams[0].l:= env^.NewStringUTF(env, PChar(_message));
8.   jCls:= env^.GetObjectClass(env, _jbluetoothclientsocket);
9.   jMethod:= env^.GetMethodID(env, jCls, 'print_Custom', '(Ljava/lang/String;)V');
10.   env^.CallVoidMethodA(env, _jbluetoothclientsocket, jMethod, @jParams);
11. env^.DeleteLocalRef(env,jParams[0].l);
12.   env^.DeleteLocalRef(env, jCls);
13. end;
14.

f_bluetooth.jBluetoothClientSocket1.print_Custom('test',3,1);

this one still raising error:
u_order.pas(2696,43) Error: identifier idents no member "print_Custom"

Thank you.

#### jmpessoa

• Hero Member
• Posts: 2249
##### Re: LAMW - simple esc/POS printing bridge?
« Reply #13 on: June 02, 2023, 10:50:50 am »
......................
« Last Edit: June 02, 2023, 10:52:31 am by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

#### jmpessoa

• Hero Member
• Posts: 2249
##### Re: LAMW - simple esc/POS printing bridge?
« Reply #14 on: June 02, 2023, 10:53:00 am »
Using IDE menu "Tools" --> [LAMW ...] --->  "New jComponent Create"  :   (see picture)

You got into the page/tab  "Pascal":

Code: Pascal  [Select][+][-]
1. unit ujbluetoothclientsocket;
2.
3. {\$mode delphi}
4.
5. interface
6.
7. uses
8.   Classes, SysUtils, And_jni, AndroidWidget;
9.
10. type
11.
12. {warning: draft "jBluetoothClientSocket" complement....}
13. {         copy/add to *jbluetoothclientsocket.pas" file}
14.
15. jBluetoothClientSocket = class
16.  public
17.     procedure print_Custom(_message: string; _size: integer; _align: integer);
18.
19. end;
20.
21. procedure jBluetoothClientSocket_print_Custom(env: PJNIEnv; _jbluetoothclientsocket: JObject; _message: string; _size: integer; _align: integer);
22.
23.
24. implementation
25.
26. {---------  jBluetoothClientSocket  --------------}
27.
28. procedure jBluetoothClientSocket.print_Custom(_message: string; _size: integer; _align: integer);
29. begin
30.   //in designing component state: set value here...
31.   if FInitialized then
32.      jBluetoothClientSocket_print_Custom(gApp.jni.jEnv, FjObject, _message ,_size ,_align);
33. end;
34.
35. {-------- jBluetoothClientSocket_JNI_Bridge ----------}
36.
37. procedure jBluetoothClientSocket_print_Custom(env: PJNIEnv; _jbluetoothclientsocket: JObject; _message: string; _size: integer; _align: integer);
38. var
39.   jParams: array[0..2] of jValue;
40.   jMethod: jMethodID=nil;
41.   jCls: jClass=nil;
42. label
43.   _exceptionOcurred;
44. begin
45.   if (env = nil) or (_jbluetoothclientsocket = nil) then exit;
46.   jCls:= env^.GetObjectClass(env, _jbluetoothclientsocket);
47.   if jCls = nil then goto _exceptionOcurred;
48.   jMethod:= env^.GetMethodID(env, jCls, 'print_Custom', '(Ljava/lang/String;II)V');
49.   if jMethod = nil then begin env^.DeleteLocalRef(env, jCls); goto _exceptionOcurred; end;
50.
51.   jParams[0].l:= env^.NewStringUTF(env, PChar(_message));
52.   jParams[1].i:= _size;
53.   jParams[2].i:= _align;
54.
55.   env^.CallVoidMethodA(env, _jbluetoothclientsocket, jMethod, @jParams);
56. env^.DeleteLocalRef(env,jParams[0].l);
57.
58.   env^.DeleteLocalRef(env, jCls);
59.   _exceptionOcurred: jni_ExceptionOccurred(env);
60. end;
61.
62. end.
63.

Now copy the generated code "by parts' to the  real  "bluetoothClientSocket.pas" in the folder "android_bridges"....

after that do a "Run" --> "Clean up and Build"  on your LAMW project.... do fix the
"Error: identifier idents no member "print_Custom""

« Last Edit: June 02, 2023, 05:41:26 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard