首页 > 解决方案 > Xamarin Zebra Sdk - 蓝牙打印“读取失败,套接字可能关闭或超时,读取 ret:-1”

问题描述

我正在使用最新的 Xamarin Zebra SDK 打印到 ZQ520 打印机。大约70% 的时间打印工作正常。其他 30% 的时间它因错误而失败

“读取失败,套接字可能关闭或超时,读取 ret:-1”

并且需要关闭/打开打印机才能进行打印。

我发送的内容是签名和标签,我正在通过不安全的蓝牙连接进行打印。

事实证明,很难始终如一地重现错误。我认为这可能与我下面代码中的 initialResponseTimeout 和 responseCompletionTimeout 有关。有没有人有设置这些值的经验?

        IConnection connection = null;

        try
        {

            connection = new BluetoothConnectionInsecure(address);
            connection.Open();

            using (var printer = ZebraPrinterFactory.GetInstance(
                               PrinterLanguage.Cpcl, connection))
            {

                using (var image = ZebraImageFactory.GetImage(signature))
                {
                   printer.StoreImage(SignatureFilename, image,
                              image.Width, image.Height);
                }

                // pause to ensure image is saved
                Thread.Sleep(1000);

                var initialResponseTimeout = 3000;
                var responseCompletionTimeout = 1000;

                // is the timeout too small or large ?
                connection.SendAndWaitForResponse(printLabel,
                initialResponseTimeout, responseCompletionTimeout, null);
            }

        }

        catch (Exception exception)
        {
            Microsoft.AppCenter.Crashes.Crashes.TrackError(exception);
        }
        finally
        {
            connection.Close();
        } 

标签: xamarinbluetoothxamarin.androidzebra-printers

解决方案


当我尝试直接与我的设备连接时,我遇到了相关问题。我建议您在本地计算机中创建一个应用程序来与您的手机和打印机进行通信。还尝试增加代码中的超时。

 using (var printer = ZebraPrinterFactory.GetInstance(
                           PrinterLanguage.Cpcl, connection))
        {

            using (var image = ZebraImageFactory.GetImage(signature))
            {
               printer.StoreImage(SignatureFilename, image,
                          image.Width, image.Height);
            }

            // pause to ensure image is saved
            //Thread.Sleep(1000); I recommend you to remove this line

            var initialResponseTimeout = 15000;
            var responseCompletionTimeout = 15000;

            // is the timeout too small or large ?
            connection.SendAndWaitForResponse(printLabel,
            initialResponseTimeout, responseCompletionTimeout, null);
        }

推荐阅读