4

I am doing a cross platform app in which I need to print a set of data through bluetooth printer.For that i have gone through internet and found a way to print from android.Here is the code i implemented:

mBluetoothAdapter = BluetoothAdapter.DefaultAdapter;

if (mBluetoothAdapter == null)
{
    await MyFavHelper.InformUser("No bluetooth adapter found", "Bluetooth");
    return;
}
else
{
    await MyFavHelper.InformUser("Bluetooth found", "Success");
}

if (!mBluetoothAdapter.IsEnabled)
{
    mBluetoothAdapter.Enable();
}
else
{
    await MyFavHelper.InformUser("Bluetooth Enabled", "Success");
}

ICollection<BluetoothDevice> pairedDevices = mBluetoothAdapter.BondedDevices;

if (pairedDevices.Count > 0)
{
    foreach (BluetoothDevice device in pairedDevices)
    {
        if (device.Name.Contains("TSP"))
        {
            mmDevice = device;
            break;
        }
                }
    }
    else
    {
        await MyFavHelper.InformUser("Paired Devices not found", "Bluetooth");
        return;
    }

    ParcelUuid uuid = mmDevice.GetUuids().ElementAt(0);

    if (mmDevice == null)
    {
        await MyFavHelper.InformUser("No Device Found", "Sorry");
    }

    mmsSocket = mmDevice.CreateInsecureRfcommSocketToServiceRecord(uuid.Uuid);

    mmsSocket.Connect();

    if (mmsSocket.IsConnected)
    {
        await MyFavHelper.InformUser("Socket Connected Successfully", "Success");
    }
    else
    {
        await MyFavHelper.InformUser("Socket Not Connected Successfully", "Sorry");
    }

    var datastream = mmsSocket.OutputStream;

    byte[] byteArray = Encoding.ASCII.GetBytes("Sample Text");

    datastream.Write(byteArray, 0, byteArray.Length);

I am successful upto "Socket connected successfully".But i am not able to print the data from printer.I am using the star micronics printer with model "TSP 100 III BI".

Can anyone please suggest me an idea if i am missing anything.

That would be a great help for me.Thanks in advance.

Praveen Baruri
  • 330
  • 3
  • 19
  • The TSP100III BlueTooth/WLAN/Ethernet models uses a Java-based API on Android. You can create a `Xamarin.Android` binding library for `StarIOPort3.1.jar` and `starioextension.jar` (also `smcloudservices.aar` if you are doing Internet/Cloud-based printing) – SushiHangover Oct 18 '17 at 07:06
  • Thanks for your reply Sushi. Can you please confirm that the format which i followed is correct or not? and also I am new to xamarin.So i don't know how to add the sdk in my cross platform project. Please guide me to get this sorted out. – Praveen Baruri Oct 18 '17 at 07:09
  • Their SDK handles the BlueTooth connection. Take their JAR files and review : https://developer.xamarin.com/guides/android/advanced_topics/binding-a-java-library/ – SushiHangover Oct 18 '17 at 07:10
  • So there is no need of our code? – Praveen Baruri Oct 18 '17 at 07:12
  • The `StarBluetoothManager` class handles (in the `StarIOPort3.1.jar`) handles the connection, bluetooth settings, Pin codes, security, port selection, pairing, timeouts, etc.. The `starioextension.jar` contains the `ICommandBuilder` that you use to build the print documents (line printing, barcode generation, logo inclusion, etc..) You should review their `Android SDK User’s Manual` as it contains the API documentation and code examples – SushiHangover Oct 18 '17 at 07:23
  • Hi Sushi.Thanks for your suggestion.I tried to implement it.For that i created JarBinding project and added jars in that and added the reference dll to our android project and implemented the code in https://www.codeproject.com/Articles/796977/Printing-from-a-Xamarin-Android-Application-Develo But I am getting the exception Java.Lang.ClassNotFoundException: com.starmicronics.stario.StarIOPort can you please suggest an idea to solve my issue. Thanks in advance. – Praveen Baruri Oct 18 '17 at 12:08
  • @PraveenBaruri : I was am trying to achieve something similar, have you had any luck with the Java Bindings as I'm getting a couple of ClassNotFoundExpections too. Thanks – AbsoluteSith Mar 05 '18 at 12:00

1 Answers1

0

The code looks good. However printer sometimes does not print if data sent to it is small. Try sending more data in multiple lines for sample print:

byte[] byteArray = Encoding.ASCII.GetBytes("Xamarin bluetooth\nPrinting text test\nSample Text");

or

byte[] byteArray = Encoding.UTF8.GetBytes("Xamarin bluetooth\nPrinting text test\nSample Text");

For further details refer to.

Sumiran
  • 11
  • 4
  • Welcome to Stack Overflow! Please be careful with linking to your own content on different sites, you don't want to be a [spammer](//stackoverflow.com/help/promotion). You should be including the majority of the content here, and use the link only as a reference. – Filnor Jan 23 '18 at 12:25