Delphi 蓝牙连接打印机演示


本文整理自网络,侵删。

 
unit Main;
interface
uses
  Bluetooth.Printer,
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls;
type
  TfrmMain = class(TForm)
    btnImprimir: TButton;
    cbxDevice: TComboBox;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnImprimirClick(Sender: TObject);
  private
    FPrinter: TBluetoothPrinter;
    procedure PopularComboBoxDevices;
  public
  end;
var
  frmMain: TfrmMain;
implementation
uses
  System.Bluetooth;
{$R *.dfm}
procedure TfrmMain.PopularComboBoxDevices;
var
  Device: TBluetoothDevice;
begin
  cbxDevice.Items.BeginUpdate;
  try
    cbxDevice.Clear;
    for Device in FPrinter.PairedDevices do
      cbxDevice.Items.Add(Device.DeviceName);
  finally
    cbxDevice.Items.EndUpdate;
  end;
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
  FPrinter := TBluetoothPrinter.Create(Self);
  FPrinter.Enabled := True;
  PopularComboBoxDevices;
end;
procedure TfrmMain.FormDestroy(Sender: TObject);
begin
  FPrinter.Free;
end;
procedure TfrmMain.btnImprimirClick(Sender: TObject);
begin
  if cbxDevice.Text = '' then
    raise Exception.Create('Escolha primeiramente uma impressora da lista!');
  if Memo1.Lines.Text.Trim.IsEmpty then
    raise Exception.Create('Nenhum texto foi digitado, impossível continuar!');
  FPrinter.PrinterName := cbxDevice.Text;
  FPrinter.Imprimir(Memo1.Lines.Text);
end;
end.
单元文件:Bluetooth.Printer.pas
unit Bluetooth.Printer;
interface
uses
  System.SysUtils, System.Bluetooth, System.Bluetooth.Components;
type
  EBluetoothPrinter = class(Exception);
  TBluetoothPrinter = class(TBluetooth)
  private
    FSocket: TBluetoothSocket;
    FPrinterName: String;
    function ConectarImpressora(ANomeDevice: String): Boolean;
    function GetDeviceByName(ANomeDevice: String): TBluetoothDevice;
  public
    procedure Imprimir(const ATexto: String);
  published
    property PrinterName: String read FPrinterName write FPrinterName;
  end;
const
  UUID = '{00001101-0000-1000-8000-00805F9B34FB}';
implementation
function TBluetoothPrinter.GetDeviceByName(ANomeDevice: String): TBluetoothDevice;
var
  lDevice: TBluetoothDevice;
begin
  Result := nil;
  for lDevice in Self.PairedDevices do
  begin
    if lDevice.DeviceName = ANomeDevice then
      Result := lDevice;
  end;
end;
function TBluetoothPrinter.ConectarImpressora(ANomeDevice: String): Boolean;
var
  lDevice: TBluetoothDevice;
begin
  lDevice := GetDeviceByName(ANomeDevice);
  if lDevice <> nil then
  begin
    FSocket := lDevice.CreateClientSocket(StringToGUID(UUID), False);
    if FSocket <> nil then
    begin
      FSocket.Connect;
      Result := FSocket.Connected
    end
    else
      raise EBluetoothPrinter.Create('Ocorreu um erro ao obter socket de conex?o bluetooth.');
  end
  else
    raise EBluetoothPrinter.CreateFmt('Impressora "%s" n?o encontrada ou n?o pareada.', [ANomeDevice]);
end;
procedure TBluetoothPrinter.Imprimir(const ATexto: String);
begin
  if ConectarImpressora(Self.PrinterName) then
  begin
    try
      FSocket.SendData(TEncoding.UTF8.GetBytes(ATexto));
    finally
      FSocket.Close;
    end;
  end
  else
    raise EBluetoothPrinter.Create('N?o foi possível conectar a impressora.');
end;
end.

相关阅读 >>

Delphi 判断指定字符串是否开头 startstext用法

Delphi 之 热键组件(thotkey)

Delphi 校验文件大小

Delphi net.httpclient用最精简的代码获取网页数据

Delphi分割字符串的函数--extractstrings

Delphi 获取系统内存状态

Delphi2010 图片格式转换bmp, png,jpeg, gif, tiff , wmphoto

Delphi结合winrar生成自解压文件

Delphi windows 编程[2] - 学习窗体生成的过程二

Delphi unicode转换ansi

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...