0

I try to print labels to my bluetooth thermal printer from an android app. I manage to print text but I can't specify the size of my labels

My material:

My printer can detect the labels. For example, when I load paper, it automatically aligns to print on the first label. With the FlashLabel or PrintLabel app, once the text is printed, the printer outputs the label. I can't do this: my text prints correctly but the label only comes out halfway. It seems to me that the printer uses the TSPL communication protocol. Is it this ? If you want, I decompiled the 2 apps that work to try to see how it's done but without success

Here, where I am actually :

private class PrintTask extends AsyncTask<BluetoothDevice, Void, Void> {

        @Override
        protected Void doInBackground(BluetoothDevice... devices) {
            BluetoothDevice printerDevice = devices[0];
            try {
                // Establish Bluetooth connection with the printer
                UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); // UUID for SPP (Serial Port Profile)
                bluetoothSocket = printerDevice.createRfcommSocketToServiceRecord(uuid);
                bluetoothSocket.connect();

                // Get the output stream to send print data
                outputStream = bluetoothSocket.getOutputStream();

                // Send TSPL commands to set up the label (102mm x 51mm) and print "Hello World" with larger font size
                String printData = "SIZE 100 mm,50 mm\nGAP 0 mm,0 mm\nCLS\nTEXT 10 mm,10 mm,\"0\",0,2,2,\"Hello World\"\nPRINT 1,1\n";

                // Add escape command
                printData += "HOME\n";
                printData += "PRINT 1,1\n"; 
                outputStream.write(printData.getBytes());



                // Close the Bluetooth connection and output stream
                outputStream.close();
                bluetoothSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            // Display a successful print message
            Toast.makeText(MainActivity.this, "Printok", Toast.LENGTH_SHORT).show();
        }
    }

Thanks in advance to anyone who will take the time to help me.

Framboise
  • 31
  • 3

1 Answers1

1

Ok. Finnaly I foud my problem. I have not specify the space between 2 labels. Her the website that explain the TSPL commands : WebSite

And here an example of my code if it can help someone :

public class TSPLPrinter {

private String command = ""; // La commande TSPL à envoyer
private BluetoothAdapter bluetoothAdapter;
private BluetoothDevice bluetoothDevice;
private BluetoothSocket bluetoothSocket;
private OutputStream outputStream;
private UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); // UUID générique pour les appareils Bluetooth série

// Handler pour gérer les messages de connexion/déconnexion Bluetooth
private Handler handler = new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case 0:
                Log.i("TSPLPrinter", "Connexion Bluetooth réussie");
                break;
            case 1:
                Log.e("TSPLPrinter", "Erreur de connexion Bluetooth");
                break;
            case 2:
                Log.i("TSPLPrinter", "Déconnexion Bluetooth réussie");
                break;
        }
        return true;
    }
});

public void connectBluetooth(String macAddress) {

    try {
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        bluetoothDevice = bluetoothAdapter.getRemoteDevice(macAddress);

        // Ouverture du socket Bluetooth et connexion à l'appareil
        bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
        bluetoothSocket.connect();

        // Récupération de l'output stream pour envoyer des données à l'appareil
        outputStream = bluetoothSocket.getOutputStream();

        // Configuration du rouleau d'étiquettes
        String labelConfig = "SIZE 4,2\nGAP 0.12,0\n";
        outputStream.write(labelConfig.getBytes());

        // Commande pour écrire du texte centré
        String textCommand = "TEXT 200,100,\"3\",0,1,1,\"Hello World\"\n";
        outputStream.write(textCommand.getBytes());

        // Commande pour imprimer et terminer l'impression
        String printCommand = "PRINT 1\n";
        outputStream.write(printCommand.getBytes());
        String endCommand = "END\n";
        outputStream.write(endCommand.getBytes());

        // Fermeture du socket Bluetooth
        bluetoothSocket.close();

        // Envoi du message de connexion réussie
        handler.obtainMessage(0).sendToTarget();

    } catch (IOException ex) {
        // Envoi du message d'erreur de connexion
        handler.obtainMessage(1).sendToTarget();
    }
}

public void disconnectBluetooth() {
    try {
        // Fermeture du socket Bluetooth
        bluetoothSocket.close();

        // Envoi du message de déconnexion réussie
        handler.obtainMessage(2).sendToTarget();

    } catch (IOException ex) {
        Log.e("TSPLPrinter", "Erreur de déconnexion Bluetooth");
    }
}

}

Framboise
  • 31
  • 3