Connect via Bluetooth with Delphi XE7 using portable printer

戏子无情 提交于 2020-03-17 06:51:46

问题


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; 

回答1:


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.




回答2:


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;


来源:https://stackoverflow.com/questions/29946885/connect-via-bluetooth-with-delphi-xe7-using-portable-printer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!