使用便携式打印机通过蓝牙与Delphi XE7连接

Vin*_*tro 18 delphi bluetooth firemonkey delphi-xe7

我正在尝试通过蓝牙与Sewoo LK-P32打印机通信.为此,我使用的是Delphi XE7.我做了一些Delphi附带的例子并没有成功.我把配对的打印机放在平板电脑中,即使这样我也无法连续打印.

当我打印的东西必须重新启动应用程序,所以我可以再打印一些东西.在我的来源下面.

有人能帮助我吗?支持这个问题?我尝试其他技术的时间很短.

启动与打印机通信的方法

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;
Run Code Online (Sandbox Code Playgroud)

将文本发送到打印机的方法

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; 
Run Code Online (Sandbox Code Playgroud)

Roh*_*pta 0

在循环中,您不断分配给lDevice. 如果有第二个未配对的设备,则lDevice指向该设备。Exit一旦检测到它已配对, 您就需要 top 。

另外,我个人不喜欢故意提出异常。如果一个类实例为零,那么你应该退出,而不是深入研究它。

例如

if FBluetoothManager = nil then
begin
  Memo1.Lines.Add('FBluetoothManager esta nulo');
  Exit;
end;
Run Code Online (Sandbox Code Playgroud)