I\'m trying to communicate with a Sewoo LK-P32 printer via Bluetooth. For this, I am using Delphi XE7. I made a few examples that come with Del
In your loop, you keep assigning to lDevice
. If there is a second unpaired device then lDevice
is pointing to that. You need top Exit
once you have detected that it is paired.
Also, I personally don't like raising exceptions deliberately. If a class instance is nil then you should exit, nolt drill down into it..
For instance
if FBluetoothManager = nil then
begin
Memo1.Lines.Add('FBluetoothManager esta nulo');
Exit;
end;
I recreated your program, and i get the same problem, but changing your code, it's working fine for me now.
The problem is here
if FSocket <> nil then
begin
// FSocket.Connect;
FSocket.Connect;
Memo1.Lines.Add('Criou socket cliente com o ServerSocket');
Texto := #27 + '|cA' + 'Teste' + #13#10;
ToSend := TEncoding.UTF8.GetBytes(Texto);
FSocket.SendData(ToSend);
Memo1.Lines.Add('Enviou ' + Texto + ' para a impressora.');
end
First, i recommend to add Fsocket as a private property, and create ONLY ONE fsocket object. So, your code will changed to
if (Assigned(LDevice)) And (Assigned(FSocket))
then Begin
if Not FSocket.Connected
then FSocket.Connect
End
Else Begin
FSocket := LDevice.CreateClientSocket(Guid, True);
Memo1.Lines.Add('Device Socked created to '+LDevice.DeviceName);
FSocket.Connect;
End;
After connected, you can call a TTimer to send what you want, o create a method checking if Fsocket is connected.
if Assigned(FSocket)
then Begin
if FSocket.Connected
then Begin
Texto := #27 + '|cA' + 'Teste' + #13#10;
ToSend := TEncoding.UTF8.GetBytes(Texto);
FSocket.SendData(ToSend);
Sleep(100);
End;
End;
Also, you can add a sleep between 2 commands, to be sure the data are received and executed by your printer.
In my case i used an Arduino width Bluetooth hc-06 module.