使用便携式打印机通过蓝牙与Delphi XE7连接 [英] Connect via Bluetooth with Delphi XE7 using portable printer

查看:471
本文介绍了使用便携式打印机通过蓝牙与Delphi XE7连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过蓝牙与 Sewoo LK-P32 打印机进行通信.为此,我正在使用 Delphi XE7 .我列举了Delphi附带的一些示例,但没有成功.我将配对的打印机放在平板电脑上,即使那样我也无法连续打印.

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; 

推荐答案

我重新创建了您的程序,但遇到了同样的问题,但是更改您的代码,现在对我来说很好用.

I recreated your program, and i get the same problem, but changing your code, it's working fine for me now.

问题在这里

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

首先,我建议将Fsocket添加为私有属性,并仅创建一个fsocket对象.因此,您的代码将更改为

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;

连接后,您可以调用TTimer发送所需的内容,或者创建一种方法来检查Fsocket是否已连接.

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;

此外,您还可以在2个命令之间添加睡眠,以确保打印机接收并执行了数据.

Also, you can add a sleep between 2 commands, to be sure the data are received and executed by your printer.

在我的情况下,我使用了Arduino宽度蓝牙hc-06模块.

In my case i used an Arduino width Bluetooth hc-06 module.

这篇关于使用便携式打印机通过蓝牙与Delphi XE7连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆