4

I am working on Android Bluetooth Printing. Printer is standard POS thermal printer.

This is my way to send data to printer (THIS PART IS WORKING):

var
    Adapter: JBluetoothAdapter; 
    remoteDevice: JBluetoothDevice;
    Sock: JBluetoothSocket;
    ostream: JOutputStream;
begin
    Adapter := TJBluetoothAdapter.JavaClass.getDefaultAdapter;
    remoteDevice := Adapter.getRemoteDevice(stringtojstring(targetMAC));
    Sock := remoteDevice.createRfcommSocketToServiceRecord(uid);
    ostream := Sock.getOutputStream;

    ....    
    function StringToJA(Data: String): TJavaArray<Byte>;
    var
      X: integer;
      len: integer;
    begin
      len := Length(Data);
      Result := TJavaArray<Byte>.Create(len);
      for X := 0 to len do
      begin
        Result.Items[X] := Ord(Data[X]);
      end;
    end;    
    // THIS LINE PRINT TEXT
    ostream.write(StringToJA(txt));

Now I want to load different char encoing and this is code in JAVA:

 int a=23;
 String codeL="iso8859-2";
 String txt="SOME TEXT...ščž";
 byte data[] = txt.getBytes(codeL);
 byte sendText[]=new byte[3];
 sendText[0]=0x1B;
 sendText[1]=0x74;
 sendText[2]=(byte)a;
 mobileprint.PRTSendBuffer(sendText, sendText.length);   

How can I convert these lines?

  • txt.getBytes(codeL);
  • sendText[2]=(byte)a;

Delphi XE5 code :

 ...
  A : Integer;
  txt, codeL: string;
  data : TJavaArray<Byte>;
  sendText : TJavaArray<Byte>;
begin
  A := 23;
  codeL := 'iso8859-2';
  txt := 'SOME TEXT...ščž';
  data := ??? txt.getBytes(codeL); ???
  sendText := TJavaArray<Byte>.Create(3);
  sendText.Items[0] := $1B;
  sendText.Items[1] := $74;
  sendText.Items[2] := ???Byte(A)???;

  ostream.write(sendText);
  ostream.write(data);

This are JAVA Expressions values from Exlipse:

enter image description here

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
xJernej
  • 194
  • 2
  • 3
  • 14
  • Java bytes are signed and Delphi bytes are not. However, you don't have to worry about that. It's still just 8 bits being sent over the wire. What's the issue? – Marcus Adams Dec 20 '13 at 00:46

1 Answers1

4

for X := 0 to len do

That should be using len-1, otherwise you will go out of bounds of both the array and the string.

You are also not taking Unicode into account when copying the string to the byte array, so you are likely to run into problems if the string contains any non-ASCII characters that require the use of UTF-16 surrogates. You should use the SysUtils.TEncoding class to convert the string to a Delphi byte array first, and then copy that into the Java byte array, eg:

function StringToJA(Data: String): TJavaArray<Byte>;
var
  Arr: TBytes;
  len, X: integer;
begin
  Arr := TEncoding.Default.GetBytes(Data); // or any other TEncoding you need
  len := Length(Arr);
  Result := TJavaArray<Byte>.Create(len);
  for X := 0 to len-1 do
    Result.Items[X] := Arr[X];
end;    

Alternatively, get rid of the loop:

function StringToJA(Data: String): TJavaArray<Byte>;
var
  Arr: TBytes;
  len: integer;
begin
  Arr := TEncoding.Default.GetBytes(Data);
  len := Length(Arr);
  Result := TJavaArray<Byte>.Create(len);
  if len > 0 then Move(Arr[0], Result.Data^, len);
end;

How can I convert these lines?

txt.getBytes(codeL);
sendText[2]=(byte)a;

Again, by making use of the TEncoding class, eg:

function StringToJA(Data: string; charset: String = ''): TJavaArray<Byte>;
var
  Encoding: TEncoding;
  Arr: TBytes;
  len: integer;
begin
  if charset <> '' then 
    Encoding := TEncoding.GetEncoding(charset)
 else
    Encoding := TEncoding.Default;
  Arr := Encoding.GetBytes(Data);
  len := Length(Arr);
  Result := TJavaArray<Byte>.Create(len);
  if len > 0 then Move(Arr[0], Result.Data^, len);
end;

var
  txt: string;
  data : TJavaArray<Byte>;
  sendText : TJavaArray<Byte>;
begin
  txt := 'SOME TEXT...ščž';
  data := StringToJA(txt, 'iso8859-2');

  sendText := TJavaArray<Byte>.Create(3);
  sendText.Items[0] := $1B;
  sendText.Items[1] := $74;
  sendText.Items[2] := Byte(23);

  ostream.write(sendText);
  ostream.write(data);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • http://stackoverflow.com/questions/24928867/how-i-can-printing-a-string-on-a-pos-printer-by-delphi-android-with-jni Can you help me :) – Tarasov Jul 24 '14 at 08:38