Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect via Bluetooth with Delphi XE7 using portable printer

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 Delphi and am not having success. I put the paired printer on tablet and even then I am not able to print continuously.

When I print something have to restart the application, so I can print something again. Below my sources.

Could someone help me? Support on this issue? My time is short to try other technologies.

Method that initiates communication with the printer

procedure TForm2.ButtonClickStart(Sender: TObject);
var
  Msg, Texto: string;
  I, B: Integer;
  BluetoothAdapter: TBluetoothAdapter;
  ListaDeAparelhosPareados: TBluetoothDeviceList;
  LServices: TBluetoothServiceList;
begin
  try
    Memo1.Lines.Add('Ponto 1');
    FBluetoothManager := TBluetoothManager.Current;
    if FBluetoothManager = nil then
      Memo1.Lines.Add('FBluetoothManager esta nulo');

    Memo1.Lines.Add('Ponto 2');
    BluetoothAdapter := FBluetoothManager.CurrentAdapter;
    if BluetoothAdapter = nil then
    Memo1.Lines.Add('BluetoothAdapter esta nulo');

    ListaDeAparelhosPareados := BluetoothAdapter.PairedDevices;
    Memo1.Lines.Add('Ponto 3');
    if ListaDeAparelhosPareados = nil then
      Memo1.Lines.Add('ListaDeAparelhosPareados esta nulo');

    for I := 0 to ListaDeAparelhosPareados.Count - 1 do
    begin
      LDevice := ListaDeAparelhosPareados[I] as TBluetoothDevice;
      if LDevice.IsPaired then
      begin
        LServices := LDevice.GetServices;
        for B := 0 to LServices.Count - 1 do
        begin
          ServiceGUI := GUIDToString(LServices[B].UUID);
          Guid := LServices[B].UUID;
          ServiceName := LServices[B].Name;
          Memo1.Lines.Add(LServices[B].Name + ' --> ' + ServiceGUI);
          Memo1.GoToTextEnd;
        end;
      end;
    end;
  except
   on E: Exception do
   begin
     Msg := E.Message;
     Memo1.Lines.Add('Erro ao Conectar na Impressora: ' + Msg);
     Memo1.GoToTextEnd;
   end;
 end;
end;

The method that sends the text to the printer

procedure TForm2.ButtonClickSendText(Sender: TObject);
var
  FSocket: TBluetoothSocket;
  ToSend: TBytes;
  Msg, Texto: String;
begin
  try
    Memo1.Lines.Add('Aparelho pareado:' + BoolToStr(LDevice.IsPaired));

    Memo1.Lines.Add('Dados do Guid:' + GUIDToString(Guid));
    FSocket := LDevice.CreateClientSocket(Guid, true);
    if FSocket = nil then
    Memo1.Lines.Add('FSocket nulo');

    Memo1.Lines.Add('Criou Bluetooth Cliente.');
    Memo1.GoToTextEnd;
    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
    else
    begin
      Memo1.Lines.Add('FSocket nulo.');
    end;

  except
    on E: Exception do
    begin
      Msg := E.Message;
      Memo1.Lines.Add('Erro ao Conectar na Impressora: ' + Msg);
      Memo1.GoToTextEnd;
    end;
  end;
end; 
like image 991
Vinícius Castro Avatar asked Apr 29 '15 14:04

Vinícius Castro


People also ask

How do I connect my phone to my printer via Bluetooth?

Connecting the Printer to Your Android Device Go to Settings > Wireless & Network and turn on Bluetooth. Wait while your device scans for the printer. The printer is displayed when your device finds it. Tap the printer.

How do I share a Bluetooth printer?

First, make sure your printer is 'discoverable' (check its manual if you're not sure how to do this) and then connect to it as you would any Bluetooth device. With your document or image open, select the Print option and your paired Bluetooth printer will be listed under Select Printer, ready to use.


1 Answers

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.

like image 123
Lionel Siero Avatar answered Nov 09 '22 09:11

Lionel Siero