Bluetooth problem

hpk2000

Member
Hi, I am able to connect to a bluetooth printer but when I send some data the printer doesn't get it all.

Code:

B4X:
   If AStream.IsInitialized = False Then
      'AStream.Initialize(Main.sManager.InputStream, Main.sManager.OutputStream, "AStream")
      AStream.InitializePrefix(Main.sManager.InputStream, True, Main.sManager.OutputStream, "AStream")
   End If
   
   ' Obtenemos los detalles del pedido
   Dim detallesPedidoList As List
   detallesPedidoList = BaseDatos.ObtenerDetallesPedido(Main.tablaSeleccion)
   
   For i = 0 To detallesPedidoList.Size - 1
      Dim detallePedidoObject As detallePedidoObjeto
      detallePedidoObject = detallesPedidoList.Get(i)
      
      AStream.Write(detallePedidoObject.articulo.GetBytes("UTF8"))
      AStream.Write(detallePedidoObject.kilos.GetBytes("UTF8"))
      AStream.Write(detallePedidoObject.piezas.GetBytes("UTF8"))
      AStream.Write(detallePedidoObject.subtotal.GetBytes("UTF8"))
   Next
   
   StartActivity(Main)

Can you help me out.

Thanks!
 

mc73

Well-Known Member
Licensed User
Longtime User
Firstly I see initialization using prefix. I doubt that your printer uses prefix.
Secondly, from my experience with epson compatible printers, I send data as is, not by converting to bytes. Sure you should send it this way? Finally, I think you should send a EndOfPage to your printer, in order for it to begin printing. Still, I don't know your exact printer model etc, thus, probably I cannot help more but I am sure other members here will help :)
 
Upvote 0

asigala

Member
Licensed User
Longtime User
Thanks for answering so fast.

The printer is a Zebra MZ-220.

Could you please post an example code for using "EndOfPage" and how you send data?
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Sorry, I know nothing about this printer. But if it uses the Epson protocol, you can find easily info about it. If it s compatible, I can certainly write here a small example.
 
Upvote 0

timwil

Active Member
Licensed User
Longtime User
Your printer is VERY special. I tried to work with one once but was unable to make heads or tails of it before I gave up in frustration. I know the printer works in Page Mode - that means that you have to build the whole page then send an end of page for it to print.

In contrast I use a Citizen CMP10 or 20 that uses line mode - at the end of each line it prints. And it uses EPSON ESC/POS codes. Your Zebra uses their own codes.

Good luck getting that to work.

Do you have a choice in which printer you get to use?

Do you have a sample project that works? If so you can set the printer to HEX DUMP mode and see exactly what codes are sent to the printer and then work back to how to send to that particular printer.
 
Upvote 0

timwil

Active Member
Licensed User
Longtime User
No idea

Do a google search and you can find the programmers reference for your printer
 
Upvote 0

asigala

Member
Licensed User
Longtime User
Do you know how do I have to send commands?

I´m currently sending like this:

B4X:
Dim inputString As String
inputString = "! 0 200 200 210 1"
inputString = inputString & "TEXT 4 0 30 40 Hello World"
inputString = inputString & "FORM"
inputString = inputString & "PRINT"

AStream.Write(inputString.GetBytes("UTF8"))

Which is a hello world example, but nothing happends. :sign0085:
 
Upvote 0

asigala

Member
Licensed User
Longtime User
It is more complicated than that - here is the link to how I print to my printer

http://www.b4x.com/forum/basic4andr...92-bluetooth-printing-via-spp.html#post101457

Also I think you have to terminate each line with CR (Chr 13) & LF (Chr 10)

I was able to use Zebra commands :sign0060: thank you for all your help.

Unfortunately sometimes the receipt doesn't and I also have an exception when I use the back button.

Here is my code:

B4X:
Sub Process_Globals
   Dim btManager As BluetoothAdmin
   Dim sManager As Serial
   Dim foundDevices As List
   Type NameAndMac (Name As String, Mac As String)
   Dim connectedDevice As NameAndMac
End Sub

Sub Activity_Create(FirstTime As Boolean)
        If FirstTime Then
                btManager.Initialize("btManager")
                sManager.Initialize("sManager")
        End If
End Sub

Sub Activity_Resume
        If btManager.IsEnabled = False Then
      If btManager.Enable = False Then 
         ToastMessageShow("Error enabling Bluetooth adapter.", True)
      Else
         ToastMessageShow("Enabling Bluetooth adapter...", False)
         'the StateChanged event will be soon raised
      End If
        End If
End Sub

Sub clientePedidosImprimirButton_Click
   foundDevices.Initialize

   If btManager.StartDiscovery   = False Then 
      ToastMessageShow("Error starting discovery process.", True)
   Else
      ProgressDialogShow("Searching for devices...")
   End If
End Sub

Sub btManager_DiscoveryFinished
   ProgressDialogHide
   If foundDevices.Size = 0 Then
      ToastMessageShow("No device found.", True)
   Else
      Dim l As List
      l.Initialize
      For i = 0 To foundDevices.Size - 1
         Dim nm As NameAndMac
         nm = foundDevices.Get(i)
         l.Add(nm.Name)
      Next
      Dim res As Int
      res = InputList(l, "Choose device to connect", -1)
      If res <> DialogResponse.CANCEL Then
         connectedDevice = foundDevices.Get(res)
         ProgressDialogShow("Trying to connect to: " & connectedDevice.Name & " (" & connectedDevice.Mac & ")")
         'serial1.Connect(connectedDevice.Mac)
         sManager.ConnectInsecure(btManager, connectedDevice.Mac, 1)
      End If
   End If
End Sub

Sub btManager_DeviceFound (Name As String, MacAddress As String)
   Log(Name & ":" & MacAddress)
   Dim nm As NameAndMac
   nm.Name = Name
   nm.Mac = MacAddress
   foundDevices.Add(nm)
   ProgressDialogShow("Searching for devices (~ device found)...".Replace("~", foundDevices.Size))
End Sub

Sub sManager_Connected (Success As Boolean)
   ProgressDialogHide
   
   If Success Then
      StartActivity(ImprimirPedido)
   Else
      Msgbox("Error al conectarse con la impresora", "")   
   End If
End Sub

Code for ImprimirPedido Activity:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   ' Obtenemos el pedido
      Dim pedidoObject As pedidoObjeto
      pedidoObject = BaseDatos.obtenerPedidoPorId(Main.tablaSeleccion)
      
   ' Obtenemos el cliente del pedido
      Dim clienteObject As clienteObjeto
      clienteObject = BaseDatos.ObtenerClientePorId(pedidoObject.cliente)
      
   Dim pagina As String
   Dim impresora As TextWriter
   
   Dim now As Long
   now = DateTime.now
   DateTime.DateFormat = "dd/MM/yyyy"
   
   pagina = "! U1 JOURNAL" & Chr(13) & Chr(10)
   pagina = pagina & "! U1 SETLP 7 0 24" & Chr(13) & Chr(10)
   pagina = pagina & "EMBUTIDOS CORONA S.A. DE C.V." & Chr(13)
   pagina = pagina & DateTime.Date(now) & Chr(13)
   pagina = pagina & "" & Chr(13)
   pagina = pagina & "LUIS ENRIQUE WILLIAMS 865" & Chr(13)
   pagina = pagina & "PARQUE INDUSTRIAL BELENES NORTE" & Chr(13)
   pagina = pagina & "ZAPOPAN, JALISCO MEXICO" & Chr(13)
   pagina = pagina & "C.P. 45150" & Chr(13)
   pagina = pagina & "RFC: EC0771130114" & Chr(13)
   pagina = pagina & "" & Chr(13)
   pagina = pagina & "CREDITO" & Chr(13)
   pagina = pagina & "FACTURADO A:" & Chr(13)
   pagina = pagina & clienteObject.nombre.Trim & Chr(13)
   pagina = pagina & clienteObject.direccion.Trim & Chr(13)
   pagina = pagina & clienteObject.colonia.Trim & Chr(13)
   pagina = pagina & clienteObject.ciudad.Trim & Chr(13)
   pagina = pagina & "RFC: " & clienteObject.rfc.Trim & Chr(13)
   pagina = pagina & "" & Chr(13)
   pagina = pagina & "   CANT DESC     P UNI   IMPORTE" & Chr(13)
   
   ' Obtenemos los detalles del pedido
   Dim detallesPedidoList As List
   detallesPedidoList = BaseDatos.ObtenerDetallesPedido(Main.tablaSeleccion)
   
   ' Variable para calcular el monto final
   Dim montoTotal As String
   montoTotal = "0"
   
   For i = 0 To detallesPedidoList.Size - 1
      Dim detallePedidoObject As detallePedidoObjeto
      detallePedidoObject = detallesPedidoList.Get(i)
      
      Dim articuloObject As articuloObjeto
      articuloObject = BaseDatos.ObtenerArticuloPorId(detallePedidoObject.articulo)
      
      ' Formato de la cantidad
         Dim finalCantidad As String
         finalCantidad = NumberFormat2(detallePedidoObject.kilos.Trim, 1, 3, 3, True)
         
         Dim longitudCantidad As Int
         longitudCantidad = finalCantidad.Length
         
         For j = longitudCantidad + 1 To 7
            pagina = pagina & " "
         Next
         
         pagina = pagina & finalCantidad & " "
         
      ' Formato de la descripcion
         Dim finalArticulo As String
         
         If articuloObject.nombre.Trim.Length > 28 Then
            finalArticulo = articuloObject.nombre.Trim.SubString2(0,28)
         Else
            finalArticulo = articuloObject.nombre.Trim
         End If
         
         Dim longitudArticulo As Int
         longitudArticulo = finalArticulo.Length
         
         Dim numeroRenglones As Int
         
         If longitudArticulo < 8 Then
            numeroRenglones = 1
         Else If longitudArticulo < 15 Then
            numeroRenglones = 2
         Else If longitudArticulo < 22 Then
            numeroRenglones = 3
         Else
            numeroRenglones = 4
         End If
         
         If longitudArticulo > 7 Then
            pagina = pagina & finalArticulo.SubString2(0,7) & " "
         Else
            pagina = pagina & finalArticulo
            
            For j = longitudArticulo + 1 To 8
               pagina = pagina & " "
            Next
         End If
      
      ' Formato del precio unitario
         Dim finalPrecio As String
         finalPrecio = NumberFormat2(detallePedidoObject.precioUnitario, 1, 2, 2, True)
         
         Dim longitudPrecio As Int
         longitudPrecio = finalPrecio.Length
         
         For j = longitudPrecio + 1 To 6
            pagina = pagina & " "
         Next
         
         pagina = pagina & finalPrecio & " "
      
      ' Formato del importe
         Dim finalImporte As String
         finalImporte = NumberFormat2(detallePedidoObject.subtotal, 1, 2, 2, True)
         
         Dim longitudImporte As Int
         longitudImporte = finalImporte.Length
         
         For j = longitudImporte + 1 To 9
            pagina = pagina & " "
         Next
         
         pagina = pagina & finalImporte & Chr(13)
      
      montoTotal = montoTotal + detallePedidoObject.subtotal
      
      If numeroRenglones > 1 Then
         For j = 2 To numeroRenglones
            If j = 2 Then
               pagina = pagina & "        "
               
               If(longitudArticulo > 14) Then
                  pagina = pagina & finalArticulo.SubString2(7,14) & " " & Chr(13)
               Else
                  pagina = pagina & finalArticulo.SubString(7) & Chr(13)
               End If
            Else If j = 3 Then
               pagina = pagina & "        "
               
               If(longitudArticulo > 21) Then
                  pagina = pagina & finalArticulo.SubString2(14,21) & " " & Chr(13)
               Else
                  pagina = pagina & finalArticulo.SubString(14) & Chr(13)
               End If
            Else
               pagina = pagina & "        " & finalArticulo.SubString(21) & Chr(13)
            End If
         Next
      End If
   Next
   
   pagina = pagina & "" & Chr(13)
   pagina = pagina & "             SUBTOTAL: " 
   
   ' Formato del total
      Dim finalSubtotal As String
      finalSubtotal = NumberFormat2(montoTotal, 1, 2, 2, True)
      
      Dim longitudSubtotal As Int
      longitudSubtotal = finalSubtotal.Length
      
      For j = longitudSubtotal + 1 To 9
         pagina = pagina & " "
      Next
      
   pagina = pagina & finalSubtotal & Chr(13)
   pagina = pagina & "             IVA: " & "          0.00" & Chr(13)
   pagina = pagina & "             TOTAL:    " 
   
   ' Formato del total
      Dim finalTotal As String
      finalTotal = NumberFormat2(montoTotal, 1, 2, 2, True)
      
      Dim longitudTotal As Int
      longitudTotal = finalTotal.Length
      
      For j = longitudTotal + 1 To 9
         pagina = pagina & " "
      Next
   
   pagina = pagina & finalTotal & Chr(13)
   pagina = pagina & "" & Chr(13)
   pagina = pagina & "" & Chr(13)
   pagina = pagina & "" & Chr(13)
   pagina = pagina & "     ______________________" & Chr(13)
   pagina = pagina & "       FIRMA DE RECIBIDO" & Chr(13) & Chr(13)
   
   impresora.Initialize(Main.sManager.OutputStream)
   impresora.WriteLine(pagina)
   impresora.Flush
   impresora.Close
   
   'Activity.Finish
   
   StartActivity(Main)
End Sub

Sub Activity_Resume
   ' Obtenemos el pedido
      Dim pedidoObject As pedidoObjeto
      pedidoObject = BaseDatos.obtenerPedidoPorId(Main.tablaSeleccion)
      
   ' Obtenemos el cliente del pedido
      Dim clienteObject As clienteObjeto
      clienteObject = BaseDatos.ObtenerClientePorId(pedidoObject.cliente)
      
   Dim pagina As String
   Dim impresora As TextWriter
   
   Dim now As Long
   now = DateTime.now
   DateTime.DateFormat = "dd/MM/yyyy"
   
   pagina = "! U1 JOURNAL" & Chr(13) & Chr(10)
   pagina = pagina & "! U1 SETLP 7 0 24" & Chr(13) & Chr(10)
   pagina = pagina & "EMBUTIDOS CORONA S.A. DE C.V." & Chr(13)
   pagina = pagina & DateTime.Date(now) & Chr(13)
   pagina = pagina & "" & Chr(13)
   pagina = pagina & "LUIS ENRIQUE WILLIAMS 865" & Chr(13)
   pagina = pagina & "PARQUE INDUSTRIAL BELENES NORTE" & Chr(13)
   pagina = pagina & "ZAPOPAN, JALISCO MEXICO" & Chr(13)
   pagina = pagina & "C.P. 45150" & Chr(13)
   pagina = pagina & "RFC: EC0771130114" & Chr(13)
   pagina = pagina & "" & Chr(13)
   pagina = pagina & "CREDITO" & Chr(13)
   pagina = pagina & "FACTURADO A:" & Chr(13)
   pagina = pagina & clienteObject.nombre.Trim & Chr(13)
   pagina = pagina & clienteObject.direccion.Trim & Chr(13)
   pagina = pagina & clienteObject.colonia.Trim & Chr(13)
   pagina = pagina & clienteObject.ciudad.Trim & Chr(13)
   pagina = pagina & "RFC: " & clienteObject.rfc.Trim & Chr(13)
   pagina = pagina & "" & Chr(13)
   pagina = pagina & "   CANT DESC     P UNI   IMPORTE" & Chr(13)
   
   ' Obtenemos los detalles del pedido
   Dim detallesPedidoList As List
   detallesPedidoList = BaseDatos.ObtenerDetallesPedido(Main.tablaSeleccion)
   
   ' Variable para calcular el monto final
   Dim montoTotal As String
   montoTotal = "0"
   
   For i = 0 To detallesPedidoList.Size - 1
      Dim detallePedidoObject As detallePedidoObjeto
      detallePedidoObject = detallesPedidoList.Get(i)
      
      Dim articuloObject As articuloObjeto
      articuloObject = BaseDatos.ObtenerArticuloPorId(detallePedidoObject.articulo)
      
      ' Formato de la cantidad
         Dim finalCantidad As String
         finalCantidad = NumberFormat2(detallePedidoObject.kilos.Trim, 1, 3, 3, True)
         
         Dim longitudCantidad As Int
         longitudCantidad = finalCantidad.Length
         
         For j = longitudCantidad + 1 To 7
            pagina = pagina & " "
         Next
         
         pagina = pagina & finalCantidad & " "
         
      ' Formato de la descripcion
         Dim finalArticulo As String
         
         If articuloObject.nombre.Trim.Length > 28 Then
            finalArticulo = articuloObject.nombre.Trim.SubString2(0,28)
         Else
            finalArticulo = articuloObject.nombre.Trim
         End If
         
         Dim longitudArticulo As Int
         longitudArticulo = finalArticulo.Length
         
         Dim numeroRenglones As Int
         
         If longitudArticulo < 8 Then
            numeroRenglones = 1
         Else If longitudArticulo < 15 Then
            numeroRenglones = 2
         Else If longitudArticulo < 22 Then
            numeroRenglones = 3
         Else
            numeroRenglones = 4
         End If
         
         If longitudArticulo > 7 Then
            pagina = pagina & finalArticulo.SubString2(0,7) & " "
         Else
            pagina = pagina & finalArticulo
            
            For j = longitudArticulo + 1 To 8
               pagina = pagina & " "
            Next
         End If
      
      ' Formato del precio unitario
         Dim finalPrecio As String
         finalPrecio = NumberFormat2(detallePedidoObject.precioUnitario, 1, 2, 2, True)
         
         Dim longitudPrecio As Int
         longitudPrecio = finalPrecio.Length
         
         For j = longitudPrecio + 1 To 6
            pagina = pagina & " "
         Next
         
         pagina = pagina & finalPrecio & " "
      
      ' Formato del importe
         Dim finalImporte As String
         finalImporte = NumberFormat2(detallePedidoObject.subtotal, 1, 2, 2, True)
         
         Dim longitudImporte As Int
         longitudImporte = finalImporte.Length
         
         For j = longitudImporte + 1 To 9
            pagina = pagina & " "
         Next
         
         pagina = pagina & finalImporte & Chr(13)
      
      montoTotal = montoTotal + detallePedidoObject.subtotal
      
      If numeroRenglones > 1 Then
         For j = 2 To numeroRenglones
            If j = 2 Then
               pagina = pagina & "        "
               
               If(longitudArticulo > 14) Then
                  pagina = pagina & finalArticulo.SubString2(7,14) & " " & Chr(13)
               Else
                  pagina = pagina & finalArticulo.SubString(7) & Chr(13)
               End If
            Else If j = 3 Then
               pagina = pagina & "        "
               
               If(longitudArticulo > 21) Then
                  pagina = pagina & finalArticulo.SubString2(14,21) & " " & Chr(13)
               Else
                  pagina = pagina & finalArticulo.SubString(14) & Chr(13)
               End If
            Else
               pagina = pagina & "        " & finalArticulo.SubString(21) & Chr(13)
            End If
         Next
      End If
   Next
   
   pagina = pagina & "" & Chr(13)
   pagina = pagina & "             SUBTOTAL: " 
   
   ' Formato del total
      Dim finalSubtotal As String
      finalSubtotal = NumberFormat2(montoTotal, 1, 2, 2, True)
      
      Dim longitudSubtotal As Int
      longitudSubtotal = finalSubtotal.Length
      
      For j = longitudSubtotal + 1 To 9
         pagina = pagina & " "
      Next
      
   pagina = pagina & finalSubtotal & Chr(13)
   pagina = pagina & "             IVA: " & "          0.00" & Chr(13)
   pagina = pagina & "             TOTAL:    " 
   
   ' Formato del total
      Dim finalTotal As String
      finalTotal = NumberFormat2(montoTotal, 1, 2, 2, True)
      
      Dim longitudTotal As Int
      longitudTotal = finalTotal.Length
      
      For j = longitudTotal + 1 To 9
         pagina = pagina & " "
      Next
   
   pagina = pagina & finalTotal & Chr(13)
   pagina = pagina & "" & Chr(13)
   pagina = pagina & "" & Chr(13)
   pagina = pagina & "" & Chr(13)
   pagina = pagina & "     ______________________" & Chr(13)
   pagina = pagina & "       FIRMA DE RECIBIDO" & Chr(13) & Chr(13)
   
   impresora.Initialize(Main.sManager.OutputStream)
   impresora.WriteLine(pagina)
   impresora.Flush
   impresora.Close
   
   'Activity.Finish
   
   StartActivity(Main)
End Sub

Hope someone can help me :sign0085:
 
Upvote 0
Top