ZebraSDK Xamarin iOS Error Write to Connection on Any operation after opening the connection


I'm using the Zebra SDK on a iOS App made with Xamarin framework on .NET using Visual Studio 2019.
I've added ExternalAccessory information and support for bluetooth in info.plist.
I've paried my ZQ320 printer on iOS settings.

var conn = new Zebra.Sdk.Comm.BluetoothConnection(mac);

var printer = Zebra.Sdk.Printer.ZebraPrinterFactory.GetLinkOsPrinter(conn, Zebra.Sdk.Printer.PrinterLanguage.ZPL);
var status = printer.GetCurrentStatus();

The GetCurrentStatus (and any other functionality) will throw an exception "Error writing to connection"

at Zebra.Sdk.Comm.Internal.ZebraBluetoothSocket+OutputStream.Write (System.Byte[] source) [0x00065] in &lt;81e75b784ceb453f94368622a751b567&gt;:0
at Zebra.Sdk.Comm.Internal.ZebraBluetoothSocket+OutputStream.Write (System.Byte[] buffer, System.Int32 offset, System.Int32 count) [0x0003f] in &lt;81e75b784ceb453f94368622a751b567&gt;:0
at System.IO.MemoryStream.WriteAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 count, System.Threading.CancellationToken cancellationToken) [0x00060] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/Common/src/CoreLib/System/IO/MemoryStream.cs:764

Anyone can help?

Submitted by SSi1 on April 04, 2022 Permalink

The problem is with the BluetoothConnection(mac) statement. On iOS, we have to use the friendly_name of the printer's Bluetooth. By default, the friendly_name is the serial number of the printer.

// var conn = new Zebra.Sdk.Comm.BluetoothConnection(mac); // The mac address won't work for iOS.
var conn = new Zebra.Sdk.Comm.BluetoothConnection(serialNumber); // Use the serial number for iOS.


Submitted by tony.rametta@e… on April 04, 2022 Permalink

Hello, thanks for the reply. I've changed the friendly_name, so its not the serialnumber anymore, but if i use the friendly name i get:
<code>Could not connect to device: Object reference not set to an instance of an object</code>

I've also tried then to use the GetConnection from the discovered printers gained from Zebra.Sdk.Printer.Discovery.BluetoothDiscoverer.FindPrinters. But i still get the "Error writing to connection" error.

Submitted by SSi1 on April 04, 2022 Permalink

I saw the same error msg on an old build of the XamarinDevDemo already installed on my iOS device. Then I updated the Visual Studio and created a new build for my iOS 14.8 device. The error msg went away and the Bluetooth connection started working. Please try to update the Visual Studio for the latest Xamarin package. Hope this makes the "Could not connect to device: Object reference not set to an instance of an object" error msg go away for you.

Submitted by tony.rametta@e… on April 05, 2022 Permalink

I've already VS19 with latest version, and latest version of Xamarin Package, and latest version of Zebra SDK, i've also tried to create a new empty project and still not working.

I finally find the solution by myself. In order to work, the printing process should be executed on a different thread of the GUI.

Submitted by SSi1 on April 05, 2022 Permalink

Glad that you've figured it out. The Zebra printers are considered as slow accessories in general. Any interaction to the printer cannot be done in a UI thread. It is recommended to spawn a separate thread when calling the Link-OS SDK API.