0 Replies - 294 Views - Last Post: 02 April 2019 - 05:32 AM

#1 Muhammad.7   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 09-September 16

android print arabic with bluetooth printer

Posted 02 April 2019 - 05:32 AM

iam using this code from internet to print but i can only print english characters, how can i print arabic characters

BluetoothAdapter bluetoothAdapter;
    BluetoothSocket bluetoothSocket;
    BluetoothDevice bluetoothDevice;

    OutputStream outputStream;
    InputStream inputStream;
    Thread thread;

    byte[] readBuffer;
    int readBufferPosition;
    volatile boolean stopWorker;

    TextView lblPrinterName;
    EditText textBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create object of controls
        Button btnConnect = findViewById(R.id.btnConnect);
        Button btnDisconnect = findViewById(R.id.btnDisconnect);
        Button btnPrint = findViewById(R.id.btnPrint);

        btnConnect.setonclickListener(new View.onclickListener() {
            @Override
            public void onclick(View v) {
                try {
                    FindBluetoothDevice();
                    openBluetoothPrinter();
                } catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });

        btnDisconnect.setonclickListener(new View.onclickListener() {
            @Override
            public void onclick(View v) {
                try {
                    disconnectBT();
                } catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });

        btnPrint.setonclickListener(new View.onclickListener() {
            @Override
            public void onclick(View v) {
                try {
                    printData();
                } catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });

        textBox = findViewById(R.id.txtText);

        lblPrinterName = findViewById(R.id.lblPrinterName);

    }

    void FindBluetoothDevice(){

        try{
          bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

          if (bluetoothAdapter == null){
              lblPrinterName.setText("No Bluetooth Adapter found");
          }

          if (bluetoothAdapter.isEnabled()){
              Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
              startActivityForResult(enableBT, 0);
          }

          Set<BluetoothDevice> pairedDevice = bluetoothAdapter.getBondedDevices();

          if (pairedDevice.size() > 0){
              for (BluetoothDevice pairedDev: pairedDevice){
                  bluetoothDevice = pairedDev;
                  lblPrinterName.setText(pairedDev.getName());
              }
          }

        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    void openBluetoothPrinter() throws IOException{
        try {

            //Standard  uuid from string //
            UUID uuidSting = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
            bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(uuidSting);
            bluetoothSocket.connect();
            outputStream = bluetoothSocket.getOutputStream();
            inputStream = bluetoothSocket.getInputStream();

            beginListenData();

        } catch (Exception ex){

        }
    }

    void beginListenData(){
        try {

            final Handler handler = new Handler();
            final byte delimiter = 10;
            stopWorker = false;
            readBufferPosition = 0;
            readBuffer = new byte[1024];

            thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (!Thread.currentThread().isInterrupted() && !stopWorker){
                        try {
                            int byteAvailable = inputStream.available();
                            if (byteAvailable>0){
                                byte[] packetByte = new byte[byteAvailable];
                                inputStream.read(packetByte);

                                for (int i = 0; i<byteAvailable; i++){
                                    byte b = packetByte[i];
                                    if (b == delimiter){
                                        byte[] encodedByte = new byte[readBufferPosition];
                                        System.arraycopy(
                                                readBuffer,0,
                                                encodedByte,0,
                                                encodedByte.length
                                        );
                                        final String data = new String(encodedByte,"utf-32");
                                        readBufferPosition = 0;
                                        handler.post(new Runnable() {
                                            @Override
                                            public void run() {
                                                lblPrinterName.setText(data);
                                            }
                                        });
                                    } else {
                                        readBuffer[readBufferPosition++]=b;
                                    }
                                }
                            }
                        }catch (Exception ex){
                            //ex.printStackTrace();
                            stopWorker=true;
                        }
                    }
                }
            });

            thread.start();
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    // Printing Text to Bluetooth Printer //
    void printData() throws IOException{
        try {
            String msg = textBox.getText().toString();
            msg += "\n";
            outputStream.write(msg.getBytes());
            lblPrinterName.setText("Printing Text...");
        } catch (Exception ex){
            ex.printStackTrace();
        }
    }

    // Disconnect Printer //
    void disconnectBT() throws IOException{
        try {
            stopWorker = true;
            outputStream.close();
            inputStream.close();
            bluetoothSocket.close();
            lblPrinterName.setText("Printer Disconnected");
        } catch (Exception ex){
            ex.printStackTrace();
        }
    }



Is This A Good Question/Topic? 0
  • +

Page 1 of 1