2016-12-28 64 views
5

Story:我的应用程序专为移动平台而设计,并与蓝牙点阵打印机有自己的连接,用于打印区域或现场的一些发票和文档。一些Android设备运行良好,但其中一些在用户尝试打印某些内容时总会抛出破损的管道异常。德尔福Firemonkey“Broken Pipe”异常

过程:

  1. 我用TBluetoothManagerTBluetoothDeviceList用于获取配对设备列表,我存储在配置INI文件的所有配对设备。
  2. 当用户想要打印时,我初始化连接已准备好打印的设备,然后我将发票一行一行地发送到TBluetoothSocket对象。
  3. 我用这个函数把我的每一行改为Byte数组;

function StrToByteArr(strData: String): TArray<Byte>; var eEncoding: TEncoding; begin eEncoding := TEncoding.GetEncoding(857); Result := eEncoding.GetBytes(strData); end;

然后,我把我的值作为字节数组TBluetoothSocket.SendData(Byte>) Method

问题:我试图用10+不同的设备打印我的价值观和成功率不够好( 8/20设备工作正常)。我该怎么办?

回答

3

最简单的方法是使用Androidapi.JNI.BluetoothAdapter.pas库直接从Delphi代码处理所有这些配置。 它有JBluetoothSocketClass它工作正常!

-

将您的字符串值,以字节JavaArray

function StringToJA(Data: String): TJavaArray<Byte>; 
var 
    X: integer; 
    len: integer; 
begin 
    len := Length(Data); 
    Result := TJavaArray<Byte>.Create(len); 
    for X := 0 to len do 
    begin 
    Result.Items[X] := Ord(Data[X]); 
    end; 
end; 

输出流的使用

oStream := Sock.getOutputStream; 
    text := CHR(15) + ' ------ ----- HELLO WORLD ----- ------' 
       + CHR(10) + CHR(13); 
    oStream.write(StringToJA(text));