0

Estoy intentando imprimir desde una Aplicacion en Android (Android Studio Jellyfish | 2023.3.1) a una impresora Impresora Zebra ZSB DP14N

Estoy siguiendo los pasos de su Documentación pero sigo sin tener éxito

Documentación: https://techdocs.zebra.com/link-os/2-14/android/ https://techdocs.zebra.com/link-os/

Cuando llega a esta parte la aplicacion se cierra, y en el debug muesta este error:

Error de conexión: Could not connect to device: read failed, socket might closed or timeout, read ret: -1

   connection = printer.getConnection();
                connection.open();

Aca mi código:

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

        // Verificar y solicitar permisos en tiempo de ejecución
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            checkBluetoothPermissions();
        } else {
            initialize();
        }
    }

 private void initialize() {
        Button printButton = findViewById(R.id.printButtonTWO);
        if (printButton != null) {
            printButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    discoverAndPrint();
                }
            });
        } else {
            Toast.makeText(this, "Button not found", Toast.LENGTH_SHORT).show();
        }
    }

    private void checkBluetoothPermissions() {
        Log.d(TAG, "Checking Bluetooth permissions");
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
            Log.d(TAG, "Requesting Bluetooth permissions");
            ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.BLUETOOTH_SCAN,
                    Manifest.permission.BLUETOOTH_CONNECT
            }, REQUEST_BLUETOOTH_PERMISSIONS);
        } else {
            Log.d(TAG, "Bluetooth permissions already granted");
            initialize();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_BLUETOOTH_PERMISSIONS) {
            boolean allPermissionsGranted = true;
            for (int result : grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    allPermissionsGranted = false;
                    break;
                }
            }
            if (allPermissionsGranted) {
                Log.d(TAG, "All Bluetooth permissions granted");
                initialize();
            } else {
                Log.d(TAG, "Bluetooth permissions denied");
                Toast.makeText(this, "Bluetooth permissions are required for this app", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void discoverAndPrint() {
        Log.d(TAG, "Starting printer discovery");
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setMessage("Discovering Printers...");
        dialog.setCancelable(false);
        dialog.show();

        try {
            BluetoothDiscoverer.findPrinters(this, new DiscoveryHandler() {
                @Override
                public void foundPrinter(final DiscoveredPrinter printer) {
                    Log.d(TAG, "Printer found: " + printer.address);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            dialog.dismiss();
                            new PrintTask(printer).execute();
                        }
                    });
                }

                @Override
                public void discoveryFinished() {
                    Log.d(TAG, "Discovery finished");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            dialog.dismiss();
                            Toast.makeText(ChooseFormatScreen.this, "No Printers Found", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                @Override
                public void discoveryError(final String message) {
                    Log.d(TAG, "Discovery error: " + message);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            dialog.dismiss();
                            Toast.makeText(ChooseFormatScreen.this, "Discovery Error: " + message, Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });
        } catch (ConnectionException e) {
            Log.e(TAG, "Connection Exception: ", e);
            dialog.dismiss();
            Toast.makeText(this, "Connection Exception: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    private class PrintTask extends AsyncTask<Void, Void, String> {
        private DiscoveredPrinter printer;

        PrintTask(DiscoveredPrinter printer) {
            this.printer = printer;
        }

        @Override
        protected String doInBackground(Void... voids) {
            Log.d(TAG, "Starting print task");
            Connection connection = null;
            try {
                connection = printer.getConnection();
                connection.open();
                ZebraPrinter zebraPrinter = ZebraPrinterFactory.getInstance(connection);

                String zpl = "^XA^FO50,50^ADN,36,20^FDHello, World!^FS^XZ";
                connection.write(zpl.getBytes());

                return "Print Successful";
            } catch (ConnectionException | ZebraPrinterLanguageUnknownException e) {
                Log.e(TAG, "Print error: ", e);
                return "Error: " + e.getMessage();
            } finally {
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (ConnectionException e) {
                        Log.e(TAG, "Error closing connection: ", e);
                    }
                }
            }
        }

        @Override
        protected void onPostExecute(String result) {
            Log.d(TAG, "Print task completed with result: " + result);
            Toast.makeText(ChooseFormatScreen.this, result, Toast.LENGTH_SHORT).show();
        }
    }

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.