Android Question How to print to USB Printer?

Richard Goh

Active Member
Licensed User
Longtime User
I had managed to connect to the usb but there is no response when sending some data to the device. Any advice?

B4X:
    Dim usb1 As UsbSerial
    Dim usbDev As UsbDevice
    Dim manager As UsbManager   
    Dim devices() As UsbDevice   
       
    Dim astreams1 As AsyncStreams
   
    manager.Initialize   
    devices = manager.GetDevices   
   
    usb1.Open(9600)
    usbDev = devices(0)
   
    If usb1.UsbPresent = usb1.USB_NONE Then    ' Ver_2.4
        'Label1.Text = "No device USB NONE"
        Return
    End If
   
    If (usb1.HasPermission) Then    ' Ver_2.4
        Log(usb1.DeviceInfo)
        'This is very important - ( VendorId, ProductId )
        usb1.SetCustomDevice(usb1.DRIVER_SILABS, 0x416, 0x5011)

        Dim dev As Int
        dev = usb1.Open(9600) 'STMicroeletronics
        If dev <> usb1.USB_NONE Then

           
            Log("Connected successfully! 1")

            'Label1.Text = ""
            Log("CONNECTED SUCCESSFULLY!!!")

             astreams1.Initialize(usb1.GetInputStream, usb1.GetOutputStream, "astreams1")

            'This is important
            usb1.SetParameters(9600, usb1.DATABITS_8,usb1.STOPBITS_1, usb1.PARITY_NONE)
            'Here if you want codce that call print
        Dim  cmd(3) As Byte
        cmd(0) = 0x1b
        cmd(1) = 0x21      
        cmd(2) = 0x10       
        Try   
            astreams1.Write(cmd) '"Testing".GetBytes("UTF8"))
            astreams1.Write("Testing".GetBytes("UTF8"))
        Catch
        End Try
        Else
            Log("Error opening USB port 1")
        End If
    Else
        usb1.RequestPermission  ' Ver_2.4
    End If
 

Richard Goh

Active Member
Licensed User
Longtime User
How to implement the printer driver? I had below information on usb details.
Is it using this line usb1.SetCustomDevice(usb1.DRIVER_SILABS, 0x416, 0x5011)?

Manufacturer : not available
Product : not available
Serial : not available
DeviceName : /dev/bus/usb/001/004
DeviceClass : USB_CLASS_PER_INTERFACE (per-interface basis)
DeviceSubClass : 0
Device ID : 0x3EC
ProductId : 0x5011
VendorId :0x416
B4aInterfaceNumber : 0
InterfaceClass : USB_CLASS_PRINTER (printer)
InterfaceSubClass : 1
InterfaceProtocol : 1
EndpointNumber : 1
EndpointDirection : out
EndpointType : USB_ENDPOINT_XFER_BULK (bulk)
EndpointAttribute : 2
EndpointInterval : 0
EndpointMaxPacketSize : 64
B4aInterfaceNumber : 1
InterfaceClass : USB_CLASS_PRINTER (printer)
InterfaceSubClass : 1
InterfaceProtocol : 2
EndpointNumber : 1
EndpointDirection : out
EndpointType : USB_ENDPOINT_XFER_BULK (bulk)
EndpointAttribute : 2
EndpointInterval : 0
EndpointMaxPacketSize : 64
EndpointNumber : 2
EndpointDirection : In
EndpointType : USB_ENDPOINT_XFER_BULK (bulk)
EndpointAttribute : 2
EndpointInterval : 0
EndpointMaxPacketSize : 64
B4aInterfaceNumber : 2
InterfaceClass : USB_CLASS_VENDOR_SPEC (vendor specific)
InterfaceSubClass : 0
InterfaceProtocol : 255
EndpointNumber : 1
EndpointDirection : out
EndpointType : USB_ENDPOINT_XFER_BULK (bulk)
EndpointAttribute : 2
EndpointInterval : 0
EndpointMaxPacketSize : 64
EndpointNumber : 2
EndpointDirection : In
EndpointType : USB_ENDPOINT_XFER_BULK (bulk)
EndpointAttribute : 2
EndpointInterval : 0
EndpointMaxPacketSize : 64
EndpointNumber : 3
EndpointDirection : In
EndpointType : USB_ENDPOINT_XFER_INT (interrupt)
EndpointAttribute : 3
EndpointInterval : 1
EndpointMaxPacketSize : 4
Connected successfully! 1
CONNECTED SUCCESSFULLY!!!
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
RawDescriptors (96) : 12 01 10 01 00 00 00 10 16 04 11 50 00 00 01 02 00 01 09 02 4E 00 01 01 00 80 32 09 04 00 00 01 07 01 01 00 07 05 01 02 40 00 00 09 04 00 01 02 07 01 02 00 07 05 01 02 40 00 00 07 05 82 02 40 00 00 09 04 00 02 03 FF 00 FF 00 07 05 01 02 40 00 00 07 05 82 02 40 00 00 07 05 83 03 04 00 01
Manufacturer (-1) : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Product (-1) : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Serial (4) : 04 03 09 04 00 00 00 00 00 00 00 00 00 00 00 00
 
Upvote 0
Top