How to write a library wrapper around objects?

SydneyWaterBoy

New Member
Licensed User
Longtime User
I am working out how to incorporate Zebra's mobile printer sdk for android into Basic4Android (which I will happily provide as open code for others to use).

The simple library example from Basic4Android generates a wrapper for a function, however I really want to provide access to the library classes that enables a bluetooth connection object to the mobile printer.

The std code it uses for connection is given below of which I would like to use the sendCpclOverBluetooth member function

Can I easily put a wrapper in eclipse around the BluetoothPrinterConnection object which i will modify to accept additional input parameters to support what I want to print?

Are there any tutorials/examples on creating libraries like this? i.e. objects rather than functions.

thanks, Anton


B4X:
public class BluetoothPrinterConnection
extends ZebraPrinterConnectionA
Establishes a Bluetooth® connection to a printer
 import com.zebra.android.comm.ZebraPrinterConnection;
 import com.zebra.android.comm.ZebraPrinterConnectionException;
 import com.zebra.android.comm.BluetoothPrinterConnection;
 
 private void sendZplOverBluetooth(final String theBtMacAddress) {
   
      new Thread(new Runnable() {
          public void run() {
             try {
                  // Instantiate connection for given Bluetooth® MAC Address.
                  ZebraPrinterConnection thePrinterConn = new BluetoothPrinterConnection(theBtMacAddress);
     
                  // Initialize 
                  Looper.prepare();
     
                  // Open the connection - physical connection is established here.
                  thePrinterConn.open();
     
     
                  // This example prints "This is a ZPL test." near the top of the label.
                  String zplData = "^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";
     
                  // Send the data to printer as a byte array.
                  thePrinterConn.write(zplData.getBytes());
     
                  //Make sure the data got to the printer before closing the connection
                  Thread.sleep(500);
     
                  // Close the connection to release resources.
                  thePrinterConn.close();
     
                  Looper.myLooper().quit();
              } catch (Exception e) {
                  // Handle communications error here.
                  e.printStackTrace();
              }
          }
      }).start();
 }
 
 private void sendCpclOverBluetooth(final String theBtMacAddress) {
 
      new Thread(new Runnable() {
          public void run() {
              try {
 
                  // Instantiate connection for given Bluetooth® MAC Address.
                  ZebraPrinterConnection thePrinterConn = new BluetoothPrinterConnection(theBtMacAddress);
 
                  // Initialize
                  Looper.prepare();
                  // Open the connection - physical connection is established here.
                  thePrinterConn.open();
 
                  // This example prints "This is a CPCL test." near the top of the label.
                   String cpclData = "! 0 200 200 210 1\r\n"
                                   + "TEXT 4 0 30 40 This is a CPCL test.\r\n"
                                   + "FORM\r\n"
                                   + "PRINT\r\n";
 
                  // Send the data to printer as a byte array.
                  thePrinterConn.write(cpclData.getBytes());
 
                  // Make sure the data got to the printer before closing the connection
                  Thread.sleep(500);
 
                  // Close the connection to release resources.
                  thePrinterConn.close();
 
                  Looper.myLooper().quit();
 
              } catch (Exception e) {
 
                  // Handle communications error here.
                  e.printStackTrace();
 
              }
          }
      }).start();
 }
 

fransvlaarhoven

Active Member
Licensed User
Longtime User
Hallo,

I'm making a first attempt to write a library for B4A, a wrapper around .jar files.

for generating the jar-file, I'm using SimpleLibraryCompiler.
for building the source code, I'm using netbeans.

code in netbeans is simple:

import com.didisoft.pgp.PGPLib;
import com.didisoft.pgp.CypherAlgorithm;

public class PGP4B4A {

// create an instance of the library
public static final PGPLib pgp = new PGPLib();

public int add(int x, int y) {
return x + y;
}

public void setCypher( ) {
pgp.setCypher(CypherAlgorithm.AES_128);
}


}

building the library for B4A also works (add works...)

problem is:
public void setCypher( ) {
pgp.setCypher(CypherAlgorithm.AES_128);
}

CypherAlgorithm is an enum and I want to pass this enum from B4A as meaningful name.

How can I do this?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Give a name like "aes128" as string.
Use a if else structure to check for the different string values and use the equivalent enum in the wrapper
 
Upvote 0
Top