加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 10:08:18 所属栏目:大数据 来源:网络整理
导读:我正在尝试通过蓝牙与Sewoo LK-P32打印机进行通信.为此我使用的是Delphi XE7.我做了几个德尔福来的例子,没有成功.我将配对的打印机放在平板电脑中,即使我不能连续打印. 当我打印某些东西必须重新启动应用程序,所以我可以再次打印一下.在我的消息来源 有人可
我正在尝试通过蓝牙与Sewoo LK-P32打印机进行通信.为此我使用的是Delphi XE7.我做了几个德尔福来的例子,没有成功.我将配对的打印机放在平板电脑中,即使我不能连续打印.

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

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

启动与打印机通信的方法

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;

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

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;

解决方法

在你的循环中,你继续分配给lDevice.如果有第二个未配对的设备,那么lDevice指向那个.检测到它已配对后,您需要顶部退出.

此外,我个人不喜欢故意提出例外.如果一个类实例是nil,那么你应该退出,nolt向下钻取它..

例如

if FBluetoothManager = nil then
begin
  Memo1.Lines.Add('FBluetoothManager esta nulo');
  Exit;
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读