如何用Delphi Android和JNI在Pos打印机上打印字符串?

时间:2014-07-24 08:36:19

标签: android delphi printing bluetooth bixolon-printer

Hi社区我对我的Delphi Android应用程序有疑问。任务是用Delphi创建一个可以在POS打印机上打印字符串的Android应用程序(我使用的是Bixolon SPP-R200II打印机)。

例如,用户在文本字段“Hello Printer”中启动应用程序和输入,而不是我希望打印机仅打印此字符串。

我的问题是,如果我点击打印,打印机打印'Hello PrinterHello PrinterHello PrinterHello PrinterHello PrinterHello Printer'到行尾:(

对于我的Applcation,我创建了一个包含4个函数的类。

GetDeviceList():TStringList; //有了这个,我得到了一个带蓝牙设备的TStringList,这个我绑定到我的ListView

CreateConnectionToBluetoothDevice(device:TListViewItem):string;  //这创建了与蓝牙设备的连接

PrintText(text:string):string;  //这是带有字符串参数的打印函数

StringToJA(Data:String; charset:String =''):TJavaArray;  //这是一个帮助功能,我可以将字符串转换为TJavaArray

这是我的AndroidBluetooth类:

unit AndroidBluetooth;

interface

  uses

    System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
    FMX.ListView,System.math, FMX.Forms,
    Androidapi.JNI.BluetoothAdapter,
    Androidapi.Helpers,
    Androidapi.JNI.JavaTypes,
    Androidapi.JNIBridge,
    Android.JNI.Toast;

  type TAndroidBluetooth = class

    private

    public

      devicename : string;
      uid:JUUID;
      targetMAC:string;
      Adapter:JBluetoothAdapter;
      remoteDevice:JBluetoothDevice;
      Sock:JBluetoothSocket;
      ostream:JOutputStream;
      //istream:JInputstream;

      constructor create();

      function StringToJA(Data: String; charset: String = '') : TJavaArray<Byte>;
      function GetDeviceList() : TStringList;
      function CreateConnectionToBluetoothDevice(device : TListViewItem) : string;
      function PrintText(text : string) : string;
  end;

implementation

  // ## Klassenexterne Funktionen ##

  {Konstruktor}
  constructor TAndroidBluetooth.create;
  begin
     inherited Create;
  end;

  {Funktion: Gibt alle Bluetooth Geräte in der nähe zurück}
  function TAndroidBluetooth.GetDeviceList : TStringList;
  var
    s:string;
    list:TStringList;
  begin

    list:=TStringList.Create;
    s:=checkBluetooth;  // Make sure bluetooth is enabled
    Toast(s);
    if pos('disabled',s)<>0 then exit;

    uid := TJUUID.JavaClass.fromString(stringtojstring('00001101-0000-1000-8000-00805F9B34FB'));

    list.Clear;
    list.AddStrings(getbonded);

    result := list;

  end;

  {Funktion: Stellt eine Verbindung zu einem Bluetooth Gerät her}
  function TAndroidBluetooth.CreateConnectionToBluetoothDevice(device : TListViewItem) : string;
  var
    len,idx,i:integer;
    s:string;
    buffer:TJavaArray<byte>;

  begin
     Toast('Selected '+device.Text);
  targetMAC:=device.Detail;
  if trim(targetMAC)='' then exit;

  Adapter:=TJBluetoothAdapter.JavaClass.getDefaultAdapter;
  remoteDevice:=Adapter.getRemoteDevice(stringtojstring(targetMAC));
  Toast('Connecting to '+device.Text+' ('+device.Detail+')');

  devicename := device.Text;

  sock:=remoteDevice.createRfcommSocketToServiceRecord(uid);

  try sock.connect;
  except Toast('Could not create service record!');
  end;
  if not sock.isConnected then
  begin
    Toast('Failed to connect to '+targetMAC+'! Try again...');
    exit;
  end;
  Toast('Connected!');

  ostream:=sock.getOutputStream;
  //istream:=sock.getInputStream;

    Application.ProcessMessages;
    ostream.write(ord(255)); //
    ostream.write(ord(255)); // get device id   (nur Chitanda)
    sleep(200);

  end;

  {Funktion: Druckt einen String auf einem Bluetooth Gerät}
  function TAndroidBluetooth.PrintText(text : string) : string;
  var
    txt: string;
    data: TJavaArray<Byte>;
    sendText : TJavaArray<Byte>;
  begin
      txt := 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);

   end;

   {Funktion: Konvertiert ein String in ein TJavaArray<Byte>}
   function TAndroidBluetooth.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;

end.

我希望得到帮助:)

0 个答案:

没有答案