Press "Enter" to skip to content

Bluetooth Printing in Xamarin Forms using Shiny

When developing enterprise salesforce applications is very common to support printing orders, collections, receipts using a portable bluetooth thermal printer. If you search around there’s not a lot of content on how to achieve this in a cross-platform way, fortunately now we have an awesome set of libraries called Shiny(designed to help make device services & backgrounding easy on Xamarin & UWP platforms) developed by community hero Allan Ritchie

In this article, I’m going to show you how to do bluetooth printing with a thermal printer by using Shiny.Bluetooth library. I will not go into the basics of Bluetooth LTE but you can find information about it here.

Let’s start

 

1. Install de Shiny.Bluetooth package in your projects. 

2. Create a ShinyAppStartup class in your Forms project 

Add the UseBleCentral(); and UseBlePeripherals();

3. Initialize Shiny in each platform

iOS

Add the Shiny.iOSShinyHost.Init(new ShinyAppStartup()); call in your AppDelegate.cs file

Android 

If you don’t have an Android Application class create one and inherit from ShinyAndroidApplication<ShinyAppStartup>

In your MainActivity override OnRequestPermissionsResult method and call Shiny.AndroidShinyHost.OnRequestPermissionsResult(requestCode, permissions, grantResults);

4. Add platform permissions

iOS

Add the Bluetooth permission in the Info.plist file

Android

Add these permissions in your AndroidManifest file

Let’s code

In this sample project, I have two ViewModels:

  • MainPageViewModel which scans the Bluetooth devices
  • PrintPageViewModel which allows you to print text.

MainPage / MainPageViewModel

To access all the Bluetooth management using we will be using the Central Manager.

ICentralManager _centralManager = Shiny.ShinyHost.Resolve();

1. Check if the Bluetooth permission hasn’t been denied

Before starting, we have to check if the Bluetooth permission hasn’t been denied. In case it was denied will send the user to the Android Settings to be able to allow it.

To do that you can use Xamarin.Essentials ShowSettings UI (More info here).

2. Set Adapter State to true

3. Get the Connected Devices

First, check if any you have device already connected by subscribing to the GetConnectedPeripherals() to get all connected devices.

Validate if you left the central manager scanning if so, stop it. (Another option is to call the StopScan() when closing the app).

4. Add the Scanning option

In the view, I added a “Scan” button that will execute GetDeviceListCommand.

This command will trigger ScanForUniquePeripherals() subscription to start Bluetooth device scanning.

5. Peripheral selection

When a peripheral is selected by the user it will navigate to the PrintPage

The full Page and ViewModel source here:

PrintPage / PrintPageViewModel

1. Connect to peripheral and find characteristic

Connect to the peripheral and subscribe to WhenAnyCharacteristicDiscovered() to find the characteristic that will allow us to write which will be used to print our text.

2. Print the text by using the characteristic Write observable

The full Page and ViewModel source here:

Final Result:

If you are interested in learning more about Shiny go to official documentation.

You can find the full source code of this sample here.

Happy Bluetooth printing 🙂

4 Comments

  1. Buddhima Buddhima

    Hi,

    I tried your code to try to identify nearby mobile phones.

    I was testing on a Samsung Galaxy J7 and I have turned on Bluetooth on iPhone X device and a Samsung Galaxy A50. But nothing was detected during the scanning.

    Is there any way to do it ?

    Thanks !

    • Rendy Rendy

      Check if they have local name in my code I’m filtering those without local name

  2. Lucas Silva Lucas Silva

    Hi,

    I have a printer zebra model ZQ520 and i can’t get success with your projetct. Could you confirm if this code is general to all type bluetooth printer?

    thks.

    • Rendy Rendy

      Is specifically for POS Bluetooth Thermal Printers. In the case of Zebra printers you should use the Zebra SDks

Comments are closed.