Spanish Imprimir cadena de texto en Impresora BlueTooth

osdiarte

Member
Licensed User
Longtime User
Hola colegas.

Pongo a disposición un app basado en los ejemplos Bluetooth especialmente en
Downloading and Printing 1 Bit BMP to ESC/POS Printer. Lo deje un
poco mas simple como para quien solo quiera imprimir texto.

B4X:
Sub Process_Globals
    Dim PrintBuffer As String

    Dim btAdmin As BluetoothAdmin
    Dim cmp20 As Serial
    Dim printer As TextWriter
   
    ' Estilo de Fuente ESC/POS
    Dim fEsc As String
    Dim f10 As Int
    Dim f12 As Int
    Dim fCompri As Int
    Dim fBold As Int
    Dim fAncho As Int
    Dim fAlto As Int
    Dim fCurs As Int
    Dim fsubraya As Int
End Sub

Sub Globals
    'Dim Printer2 As Datecs
    Dim Astream As AsyncStreams
    Private BotEscPos As Button
    Private BotSend As Button
    Private EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    fEsc = Chr(27) & "!"
    f10 = 0 ' 10cpp
    f12 = 1 ' 10cpp
    fCompri = 4
    fBold = 8
    fAlto = 16
    fAncho = 32
    fCurs = 64
    fsubraya = 128
   
    Activity.LoadLayout("interface1")
   
    If FirstTime Then
        btAdmin.Initialize("BlueTeeth")
        cmp20.Initialize("Printer")
    End If
   
    StartPrinter
End Sub

Sub StartPrinter
    Dim PairedDevices As Map
    Dim L As List
    Dim Res As Int

    ToastMessageShow("Conectando con la Impresora.....",True)
    BotSend.Enabled = False

    PairedDevices.Initialize
   
    ' Obtengo lista de dispositivos emparejados
    Try
        PairedDevices = cmp20.GetPairedDevices
    Catch
        Msgbox("Obteniendo dispositivos emparejados","Impresora Error")
        printer.Close
        cmp20.Disconnect
    End Try

    'For Each k As String In PairedDevices.Keys
    '    Log(k)
    'Next
   
    ' Conección con el dispositivo
    Select PairedDevices.Size
        Case 0
            Msgbox("Error connección a Impresora - No se encontro impresora","")
        Case 1
            Log("GetKeyAt:"&PairedDevices.GetKeyAt(0))
            Try
                cmp20.ConnectInsecure(btAdmin,PairedDevices.Get(PairedDevices.GetKeyAt(0)),1)
                Log(PairedDevices.Get(PairedDevices.GetKeyAt(0)))
            Catch
                Msgbox("Conectando","Impresora Error")
                printer.Close
                cmp20.Disconnect
            End Try
        Case Else
            L.Initialize

            For i = 0 To PairedDevices.Size - 1
                L.Add(PairedDevices.GetKeyAt(i))
            Next

            Res = InputList(L, "Seleccionar dispositivo", -1)
            Log(Res)
            If Res <> DialogResponse.CANCEL Then
                cmp20.Connect(PairedDevices.Get(L.Get(Res)))
                Log(PairedDevices.Get(L.Get(Res)))
            End If
    End Select
End Sub

Sub Printer_Connected (Success As Boolean)
    If Success Then
        Log("Conectado con suceso")
        BotSend.Enabled = True
          Astream.Initialize(cmp20.InputStream,cmp20.OutputStream,"printerAs")
    Else
        If Msgbox2("", "Error Impresora","Reimprimir","Cancelar","",Null) = DialogResponse.POSITIVE Then
            StartPrinter
        End If
    End If
End Sub

Sub printerAs_NewData (Buffer() As Byte)
    Dim msg As String
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    ToastMessageShow(msg, False)
    Log("printerAs_newData:"&msg)
End Sub

Sub printerAs_Error
    Log("AStreams_Error")
    ToastMessageShow(LastException.Message, True)

End Sub

Sub BotEscPos_Click
    EditText1.Text = Chr(27)&Chr(64) _
        & fEsc&Chr(f12+fBold)&"Hola Mundo1" &fEsc&Chr(f12+fBold+fCurs)&"Hola Mundo2" &CRLF _
        &  fEsc&Chr(f12)&"Hola Mundo3" &fEsc&Chr(f10)&"Hola Mundo4" &CRLF _
        &  fEsc&Chr(f12+fCompri)&"Hola Mundo5" &fEsc&Chr(f10+fCompri)&"Hola Mundo6" &CRLF
End Sub

Sub BotSend_Click
        Log("Write AsTream")
        If Astream.IsInitialized=False Then
            Log("Se inicializo Astream")
              Astream.Initialize(cmp20.InputStream,cmp20.OutputStream,"printerAs")
        Else
            Log("Estaba inicializado Astream")
        End If
        Astream.Write(EditText1.Text.GetBytes("UTF8")) 
End Sub'
 

Attachments

  • BtPrint_String.zip
    42.1 KB · Views: 378

bgsoft

Well-Known Member
Licensed User
Longtime User
Hola:

Gracias por compartir

Saludos:
 

rscheel

Well-Known Member
Licensed User
Longtime User
Excelente estimado, podrías compartirlo en la sección de tutoriales en español para que el hilo no se pierda.
 
Top