我正在尝试通过蓝牙与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;