Dear, I currently have a POS printer connected via Bluetooth to my application, everything works fine with android 7 and 8 prints correctly without failures, the problem comes with Android 5, at the beginning it is good but when printing, close the application and return it to open , there comes a time where you can not find the printer connected by bluetooth or restarting the phone or closing the application completely reestablishes communication, until now the only way I have found is to unpair the connection and re-pair, that way it returns to have connection.
Has someone from the forum gone through this problem before?
Could you solve it?
This is the code that I use
Has someone from the forum gone through this problem before?
Could you solve it?
This is the code that I use
B4X:
Sub Activity_Create(FirstTime As Boolean)
BtAdmin.Initialize("BlueTooth")
BTConnection.Initialize("BTConnection")
clTicket.Initialize
End Sub
Sub Activity_Pause (UserClosed As Boolean)
oStream.Close
BTConnection.Disconnect 'disable this if you like
End Sub
Sub InicioImpresion
If BtAdmin.IsEnabled Then
ProgressDialogShow2("Conectando Impresora", False)
Dim PairedDevices As Map
PairedDevices = BTConnection.GetPairedDevices
Dim l As List
Dim DeviceName, MacAddress As String
l.Initialize
For i = 0 To PairedDevices.Size - 1 'Check all devices
l.Add(PairedDevices.GetKeyAt(i))
DeviceName=PairedDevices.Getkeyat(i)
MacAddress=PairedDevices.GetValueAt(i)
Log(DeviceName & " -> " & MacAddress)
If DeviceName.Contains(ModConn.NOM_BTH_CFG) Then 'Insert the BT-Name of the printer or use the MAC address
Exit
End If
Next
BTConnection.Connect(MacAddress)
Sleep(2000)
Else
ProgressDialogShow2("Encendiendo Bluetooth...", False)
BtAdmin.Enable
Sleep(2000)
ProgressDialogHide
InicioImpresion
End If
End Sub
Sub oStream_Error
Log("oStream ERROR")
End Sub
Sub oStream_Terminated
Log("oStream TERMINATED")
End Sub
Sub BTConnection_Connected (Success As Boolean)
If Success Then
oStream.Initialize(Null,BTConnection.OutputStream,"oStream")
ProgressDialogHide
CallSubDelayed(Me,"generatePrint")
Else
ProgressDialogHide
ToastMessageShow("Impresora no encontrada", False)
End If
End Sub
Sub generatePrint 'Imprime Ticket
ProgressDialogShow2("Imprimiendo...", False)
Dim bufferToPrint() As Byte = clTicket.getBytesForBitmap3("LogoM4.png",80) 'Third method: GS v --> adding white spaces
Log( "Printing bitmap. buffer size is "&bufferToPrint.Length)
Dim result As Boolean = oStream.Write( bufferToPrint )
Printer.Initialize2(BTConnection.OutputStream,"windows-1252") 'important to print f.e. German/French chars
PrintBuffer = Chr(27)&"t"&Chr(16)& CRLF & CRLF & _
"PATENTE : "& PATENTE_PRT & CRLF & _
"CAMION : "& COD_CAMION_PRT & CRLF & _
"TARA : "& ModConn.nf.Format(TARA_PRT) &" Kg"& CRLF & _
"CARGA : "& ModConn.nf.Format(CARGA_PRT) &" Kg"& CRLF & CRLF & _
"TOTAL : "& ModConn.nf.Format(TOTAL_PRT) &" Kg"& CRLF & CRLF & _
"FECHA : "& FECHA_PRT & CRLF & _
"DESPACHADOR : "& DESPACHADOR_PRT & CRLF & _
"N° GUÍA : "& NGUIA_PRT & CRLF & CRLF & _
"PRUEBA."& CRLF & CRLF & _
"TEXTO 1"& CRLF & _
"TEXTO2."& CRLF & CRLF & _
" PAGE" _
& CRLF & CRLF & CRLF & CRLF
Printer.WriteLine(PrintBuffer)
Printer.Flush
Log( "Sent to printer! Success="&result &", "& Not(result)) 'long message if sucess=False
Printer.Close
ProgressDialogHide
End Sub