This article will take Android’s USB serial communication library as an example to introduce how Delphi uses Java libraries in Android.

  USB serial communication address:

  https://github.com/felHR85/UsbSerial

compile the Java library

  1. Install Android Studio

  2. In the Android Studio, point the SDK path to the Delphi SDK path (avoid installing the SDK repeatedly)

  3. Download UsbSerial and open it with Android Studio.

  3.1Android Studio settings (File->Settings)

  Gradle setting

Delphi uses Android's USB serial communication Java library under Android

Delphi uses Android’s USB serial communication Java library under Android

Delphi uses Android's USB serial communication Java library under Android

Delphi uses Android’s USB serial communication Java library under Android

 3.2 Setting project properties:

  Select the SDK version:

Delphi uses Android's USB serial communication Java library under Android

Delphi uses Android’s USB serial communication Java library under Android

Delphi uses Android's USB serial communication Java library under Android

Delphi uses Android’s USB serial communication Java library under Android

4. Compile UsbSerial (Build-“Make Project)

  The compilation process may need to download Gradle related files.

  After the compilation is completed, classes.jar will be generated.

 5. Generate a DEX file

 5.1 generate dex need to use a dx.bat, the specific directory is in %Android%\build-tools\28.0.3 (%Android% for the Android SDK directory, 28.0.3 is not specified, under each tools, %Android% does not have to be C:\Users\Public\Documents\Embarcadero\Studio\19.0\PlatformSDKs\android-sdk-windows)

5.2 Enter cmd.exe configuration environment variable: path is the directory under dx.bat

Set ANDROID_HOME=\C:\Users\Public\Documents\Embarcadero\Studio\19.0\PlatformSDKs\android-sdk-windows

Set path=”C:\Program Files (x86)\Java\jdk1.8.0_144\bin”;%PATH%

Dx –dex –output=C:\DelphiComps\UsbSerial\bin\usbserial.dex C:\DelphiComps\UsbSerial\bin\classes.jar

This generates usbserial.dex

2 merge dex files

The generated dex file needs to be merged with the dex file that comes with Delphi.

Delphi’s own dex file is divided into Debug version, and Release version, the path is in %BDS%\lib\android\Debug, %BDS%\lib\android\Release

Continue in cmd.exe:

Set BDS=C:\Program Files (x86)\Embarcadero\Studio\19.0

Set path=”C:\Program Files (x86)\Java\jdk1.8.0_144\bin”;%PATH%

Java -cp %DX_LIB%\dx.jar com.android.dx.merge.DexMerger debug_classes.dex %BDS%\lib\android\Debug\classes.dex .\usbserial.dex

Java -cp %DX_LIB%\dx.jar com.android.dx.merge.DexMerger release_classes.dex %BDS%\lib\android\Release\classes.dex .\usbserial.dex

If it is Debug mode:

Copy debug_classes classes.dex

If it is Release mode:

Copy Release_classes classes.dex

3 generate Delphi access interface

The Java2OP tool that comes with Delphi 10.1 and above can automatically generate interface files of Java class.

In Cmd.exe

Set BDS=C:\Program Files (x86)\Embarcadero\Studio\19.0

Set path=”C:\Program Files (x86)\Java\jdk1.8.0_144\bin”;%PATH% 
set path=%BDS%\bin\converters\java2op”;%PATH% 
java2op -jar classes.jar Android .JNI.UsbSerial.pas

The classes.jar here is the java class library compiled by usbserial, and the interface file of Delphi is generated Android.JNI.UsbSerial.pas

4  Using the Java class library

Reference Android.JNI.UsbSerial.pas in the Delphi project

 Get the USB management service interface

function GetUsbManager: JUsbManager;
var UsbManagerService: JObject;
begin
  UsbManagerService := SharedActivity.getSystemService(TJContext.JavaClass.USB_SERVICE);
  Result := TJUsbManager.Wrap((UsbManagerService as ILocalObject).GetObjectID);
end;

Get a list of USB devices

function GetUsbDevices: TArray<JUsbDevice>;
var
  UsbManager: JUsbManager;
  UsbDeviceList: JHashMap;
  UsbDeviceListIterator: JIterator;
  I: Integer;
begin
  UsbManager := GetUsbManager;
  if UsbManager <> nil then
  begin
    UsbDeviceList := UsbManager.getDeviceList;
    SetLength(Result, UsbDeviceList.size);
 
    UsbDeviceListIterator := UsbDeviceList.values.iterator;
    for I := 0 to Length(Result) - 1 do
      Result[I] := TJUsbDevice.Wrap((UsbDeviceListIterator.next as ILocalObject).GetObjectID);
  end;
end;

Open the USB device and return to the USB connection interface

function GetUsbConnection(Device: JUsbDevice): JUsbDeviceConnection;
var UsbManager: JUsbManager;
begin
  UsbManager := GetUsbManager;
  if UsbManager <> nil then
    Result := UsbManager.openDevice(Device);
end;
//create
  SerialDevice := TJUsbSerialDevice.JavaClass.createUsbSerialDevice(UsbDevice, UsbConnection);
  if SerialDevice = nil then
  begin
    raise Exceptioin.Create('Cannot create serial device');
    Exit;
  end;
 
//Open
  if not SerialDevice.syncOpen then
  begin
    raise Exception.Create('Cannot open serial device');
    SerialDevice := nil;
    UsbConnection.close;
    UsbConnection := nil;
    Exit;
  end;
//setting
SerialDevice.setBaudRate(9600);
SerialDevice.setDataBits(TJUsbSerialInterface_DATA_BITS_8);
SerialDevice.setStopBits(TJUsbSerialInterface_STOP_BITS_1);
SerialDevice.setParity(TJUsbSerialInterface_PARITY_NONE);
SerialDevice.setFlowControl(TJUsbSerialInterface_FLOW_CONTROL_OFF);
//read:
if SerialDevice.syncRead(ReadBuffer, 50) >= 1 then
  Data := Data + TEncoding.UTF8.GetString(ToByteArray(ReadBuffer));
//write
    SerialDevice.syncWrite(WriteBuffer, 1000);

5  Released under Delphi

1. Permission settings, the permissions required for the referenced Java class library, need to be added in the Delphi project setup.

2. Publish the newly generated dex file to the remote path classes\, and also add the files needed by the java library.

Reference: www.cnblogs.com/hezihang/p/9957497.html