【发布时间】:2015-07-08 22:12:01
【问题描述】:
我正在尝试通过蓝牙与 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;
将文本发送到打印机的方法
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;
【问题讨论】:
-
您可能在发送完数据后错过了
FSocket.Close电话。 -
Dalija,我将在这里测试关闭套接字并与您交谈。谢谢。
-
我什至关闭套接字在Android中给出以下错误:java.io.IOException:读取失败,关闭或套接字可能超时,读取ret:-1
-
抱歉,我自己无法测试。看起来当您关闭套接字时它仍在与打印机通信。
-
对。你知道我该如何解决这个问题吗?你觉得用Delphi XE7可以吗?在Android和iOS本机打印机中运行的示例中,我需要让它在Delphi XE7中运行。
标签: delphi bluetooth firemonkey delphi-xe7