我们正在开发一个由Delphi 11 FMX编写的移动应用程序,我们需要从中打印小标签到蓝牙标签打印机。
我们在安卓手机上测试,蓝牙连接成功,但是当我们尝试向打印机发送打印命令(ESC/POS)时,我们可以看到打印机有响应(发出蜂鸣声,并且屏幕上显示“正在等待数据”)小屏幕),然后打印机将标签移动到下一个,但它不打印任何内容,只是空白。
首先我们以为打印机有问题,然后我们从Google play下载了2个不同的标签打印应用程序,两者都可以工作,并成功打印文本。
这是源代码:
const
UUID = '{00001101-0000-1000-8000-00805F9B34FB}';
Var
FSocket: TBluetoothSocket ;
//connect Bluetooth printer
procedure TForm1.Button2Click(Sender: TObject);
function ConnectPrinter(pDeviceName: String): boolean;
var
lDevice: TBluetoothDevice;
begin
Result := False;
lDevice := GetDeviceByName(pDeviceName);
if lDevice <> nil then
begin
FSocket := lDevice.CreateClientSocket(StringToGUID(UUID), False);
if FSocket <> nil then
begin
FSocket.Connect;
Result := FSocket.Connected
end;
end;
end;
begin
if (printerName.Text <> '') then
begin
if ConnectPrinter(printerName.Text) then
begin
Label1.Text := 'Connected';
end
else
begin
Label1.Text := 'Disconnected';
end;
end
else
begin
ShowMessage('No device name provided');
end;
end;
// print text to Bluetooth Printer
procedure TForm1.Button3Click(Sender: TObject);
begin
if (FSocket <> nil) and (FSocket.Connected) then
begin
FSocket.SendData(TEncoding.UTF8.GetBytes(chr(27) + chr(64))); //initial printer
//the printer responds with beep, and 'waiting for data' shown on the small screen`
FSocket.SendData(TEncoding.UTF8.GetBytes(chr(27) + chr(97) + chr(0))); //Left aligned
FSocket.SendData(TEncoding.UTF8.GetBytes(chr(27) + chr(33) + chr(0))); //Character parameter set
FSocket.SendData(TEncoding.UTF8.GetBytes(chr(29) + chr(33) + chr(0))); //default font size
FSocket.SendData(TEncoding.UTF8.GetBytes('Print via Bluetooth ')); // send text to printer
FSocket.SendData(TEncoding.UTF8.GetBytes(chr(27) + chr(100) + chr(1))); // Print and feed paper 1 line
// the printer feeds the paper by 1 line, but nothing printed on the label
end;
end;
遇到同样的问题,你找到解决办法了吗?