How to easily connect and start printing via a portable Bluetooth EPS / POS printer in Xamarin Android app

Where do we start?

Add a package for working with EPS / POS teams in the Nuget project. To do this, open the Package Manager Console and add Zebra.Printer.SDK with the command

Install-Package Zebra.Printer.SDK 

In fact, it does not matter which model of printer you are using or who the manufacturer is. You need to make sure that the printer supports EPS / POS commands (most of them). In this case, the library will be able to work with it without problems.

All further steps are described with the assumption that you went into the settings of the Android device and connected to the printer.

First of all, add permissions to the AndroidManifest.xml file:

 <manifest ....> ... <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> ... </manifest> 

Get the adapter for working with Bluetooth:

 var bluetoothAdapter = BluetoothAdapter.DefaultAdapter; 

We find printers among all connected devices and take the first one:

 var printers = bluetoothAdapter.BondedDevices.Where(c => c.BluetoothClass.MajorDeviceClass == MajorDeviceClass.Imaging && (int)c.BluetoothClass.DeviceClass == PrinterBluetoothMinorDeviceClassCode && c.BondState == Bond.Bonded); var printer = printers.First(); 

In a real application, you should select a printer by name, for example, display a dialog for the user where he can select a printer

The code for the minor device class that corresponds to printers is for some reason missing from the Xumarin Android enum so I just set it as a constant:

 private const int PrinterBluetoothMinorDeviceClassCode = 1664; 

Next we need to create a join:

 Connection connection; try { var simpleConnectionString = $"BT:{printer.Address}"; connection = ConnectionBuilder.Build(simpleConnectionString); } catch (Exception exception) { try { var multiChannelConnectionString = $"BT_MULTI:{printer.Address}"; connection = ConnectionBuilder.Build(multiChannelConnectionString); } catch (Exception multichannelException) { Console.WriteLine(multichannelException); throw; } } 

We are trying to connect in two different ways, the first normal, the second multi-channel. The printer can work in one of them, so we try both in succession.

After you manage to create a connection, you will need to open a connection, send data to the printer, and close the connection:

 try { var testString = "This a test text for printer."; var stringBuilder = new StringBuilder(); stringBuilder.Append(testString); stringBuilder.Append("\n"); connection.Open(); connection.Write(Encoding.UTF8.GetBytes(stringBuilder.ToString())); } catch (Exception exception) { Console.WriteLine(exception); throw; } finally { if (connection.Connected) { connection.Close(); } } 

Do not forget that the operation of opening a connection is lengthy, therefore it should be performed in a separate thread. Running it in the main UI thread is not recommended.

After the Write command, a listing of the characters you sent should go.


All Articles