Hi Community I have a Question to my Delphi Android Application. The Task is it to create a Android App with Delphi that can print a string on a POS Printer (I use the Bixolon SPP-R200II Printer).
For Example the User Start the App and Input in the Textfield 'Hello Printer' than I want that the Printer print that only on of this string.
My Problem is it that if I click on Print that the printer print 'Hello PrinterHello PrinterHello PrinterHello PrinterHello PrinterHello Printer' to the end of the line :(
For my Applcation I create a class with 4 functions.
GetDeviceList() : TStringList; // With this I get a TStringList with the Bluetooth Devices, this I bind to my ListView
CreateConnectionToBluetoothDevice(device : TListViewItem) : string; //This create a connection to a Bluetooth device
PrintText(text : string) : string; //this is the print function with a string parameter
StringToJA(Data: String; charset: String = '') : TJavaArray; //This is a helpfunction, with the function I can convert a string to a TJavaArray
Here is my AndroidBluetooth class :
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.
I hope I get help :)