Android Question RFID and barcode readers without dragging into Keyboard Events

Martin Domian

Member
Licensed User
Longtime User
Hello!

I'm trying to connect USB devices to an Android device, specifically RFID and barcode readers. Of course, I don't want to have the events dragged into the keyboard, but read out directly via the port.

I had already tried this one, but unfortunately it crashes and tells me that the device is not supported

There must be an easy way to listen to a USB port and read the events, or not ?
If someone has experience, I would be very grateful, because so far I have not been successful for two days now

Martin


Test RFID and Barcode Readers:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private TimerPulse As Timer
    Private usbM As UsbManager
    Private myRFID As felUsbSerial       
End Sub

Private Sub TestStart 
    Dim UsbDevices() As UsbDevice
    UsbDevices = usbM.GetDevices
    If UsbDevices.Length = 0 Then
        ToastMessageShow("No connected usb devices.", True)
    Else
        For z = 0 To UsbDevices.Length - 1
          Dim UsbDvc As UsbDevice
          UsbDvc = UsbDevices(z)
            Log (UsbDvc.DeviceName)
            Log (UsbDvc.DeviceId)
            Log (UsbDvc.ProductId)
            Log (UsbDvc.VendorId)
            If UsbDvc.DeviceId = 1004 Then                       
                If usbM.HasPermission(UsbDvc) = True Then
                    myRFID.Close
                    myRFID.Initialize("RFIDReader", UsbDvc,-1)
                    myRFID.BaudRate = 9600
                    myRFID.DataBits =myRFID.DATA_BITS_8
                Else
                    ToastMessageShow("keine Erlaubnis",True)
                    usbM.RequestPermission(UsbDvc)
                End If
            End If           
      Next         
    End If   
End Sub

Sub RFIDReader_DataAvailable (Buffer() As Byte)
    Dim msg As String
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    ToastMessageShow(msg,True)
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
Usually the Device (or Manufacturer) does provide a Api for this. Or at least example code in java.

Write a wrapper for their Api.
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
In my experience with barcode readers, you need to be sure the reader supports "SPP Mode" which will make the scanner operate like any other serial device. If your reader does not support SPP mode then your only option is to have it act as a HID and replace the keyboard.

I dug through the project I did a couple years ago and here's the barcode class that I came up with. I have this class declared in my starter service, and the starter service takes care of managing its events and forwarding the events to the current activity. This class assumes your scanner is SPP mode and appends a CRLF to the scanned barcode.

BarcodeReader Class:
Sub Class_Globals
    ' Raised when connected to scanner, if false .LastError property will contain the malfunction.
    #Event: Connected(Success As Boolean)
    ' Raised if an error occurs talking to the scanner.
    #Event: Error(Message As String)
    ' Raised if we receive data from the scanner, it is raw byte data. For a string use the BarcodeScanned event
    #Event: NewData(Buffer() As Byte)
    ' Raised when we receve data from the scanner, the string returned is the full string with any CRLF stripped from the end.
    #Event: BarcodeScanned(Barcode As String)
    ' Raised when connection to device is terminated
    #Event: Terminated
   

    Private moCaller As Object
    Private msBaseEvent As String
    Private tmrReconnect As Timer
    Private miReconnectDelay As Long = 1500
    Private mbConnecting As Boolean = False
   
    Private moSerial As Serial
    Private moAS As AsyncStreams
    Private mbConnected As Boolean
    Private mbMonitoring As Boolean
    Private msLastError As String = ""
    Private moBC As ByteConverter   
    Private msMacAddress As String = ""
End Sub

' For driving a serial bluetooth barcode scanner in SPP mode.
Public Sub Initialize(Callback As Object, BaseEvent As String)
    moCaller = Callback
    If BaseEvent.trim = "" Then
        msBaseEvent = ""
    Else
        msBaseEvent = BaseEvent & "_"
    End If
    moSerial.Initialize("moSerial")
    mbConnected = False
    mbMonitoring = False
    tmrReconnect.Initialize("tmrReconnect", miReconnectDelay)
    tmrReconnect.Enabled = False
    msLastError = ""
End Sub

Public Sub LastError As String
    Return msLastError
End Sub

Public Sub IsConnected As Boolean
    Return mbConnected
End Sub

Public Sub IsBluetoothOn As Boolean
    Return moSerial.IsEnabled
End Sub

Public Sub getReconnectDelay As Long
    Return miReconnectDelay
End Sub

Public Sub setReconnectDelay(Milliseconds As Long)
    miReconnectDelay = Milliseconds
    tmrReconnect.Interval = Milliseconds
End Sub

Public Sub Connect(MacAddress As String)
    #IF DEBUG
        LogColor("clsBarcodeReader.Connect - Connecting", Colors.Magenta)
    #End If
    Disconnect
    mbMonitoring = False
    mbConnected = False
    If MacAddress <> "" Then
        msMacAddress = MacAddress
        ConnectOrReconnect
    Else
        #If DEBUG
            LogColor("clsBarcodeReader.Connect - invalid mac address", Colors.Magenta)
        #End If
    End If
End Sub

Public Sub Disconnect
    mbMonitoring = False
    Try
        If mbConnected Then
            Try
                moAS.SendAllAndClose
            Catch
                LogColor("clsBarcodeReader.Disconnect - error closing moAS on disconnect: " & LastException.Message, Colors.Magenta)
            End Try
        End If
        moSerial.Disconnect
    Catch
        LogColor("clsBarcodeReader.Disconnect - RTE: " & LastException, Colors.Magenta)
    End Try
    mbConnected = False
End Sub

' Returns a MAP of paired devices, key is the friendly name, value is the MAC of the device.
Public Sub GetDeviceList() As Map
    Dim poMap As Map
    poMap.Initialize
   
    Try
        poMap = moSerial.GetPairedDevices()
    Catch
        LogColor("clsBarcodeReader.GetDeviceList - " & LastException.Message, Colors.Magenta)
    End Try
    Return poMap
End Sub


' Send the contents of the supplied array of bytes to the device
Public Sub WriteBytes(data() As Byte)
    If mbConnected Then
        Try
            moAS.Write(data)
        Catch
            msLastError = LastException.Message
            moAS_Error
        End Try
    End If
End Sub



'##################################################################################################################
'##################################################################################################################
'##################################################################################################################
'##################################################################################################################
#Region  PRIVATE METHODS

Private Sub ConnectOrReconnect
    If mbConnecting Then
        Return
    End If
    mbConnecting = True
    mbConnected = False
    msLastError = ""
    Try
        moSerial.Connect(msMacAddress)
    Catch
        LogColor("clsBarcodeReader.ConnectOrReconnect - " & LastException, Colors.Magenta)
    End Try
End Sub

Private Sub tmrReconnect_Tick
    tmrReconnect.Enabled = False
    If mbMonitoring Then
        #IF DEBUG
            LogColor("clsBarcodeReader.tmrReconnect_Tick - Reconnecting", Colors.Magenta)
        #End If
        ConnectOrReconnect
    End If
End Sub

Private Sub moSerial_Connected(Success As Boolean)
    mbConnecting = False
    If Success Then
        Try
            moAS.Initialize(moSerial.InputStream, moSerial.OutputStream, "moAS")
            mbConnected = True
            msLastError = ""
            mbMonitoring = True
            RaiseEvent2("Connected", Success)
        Catch
            LogColor("clsBarcodeReader.moSerial_Connected: Failed to connect to input stream. " & LastException.Message, Colors.Magenta)
            mbConnected = False
            msLastError = LastException.Message
            If mbMonitoring Then
                tmrReconnect.Enabled = True
            Else
                RaiseEvent2("Connected", Success)
            End If
        End Try
    Else
        mbConnected = False
        msLastError = LastException.Message
        If mbMonitoring Then
            tmrReconnect.Enabled = True
        Else
            RaiseEvent2("Connected", Success)
        End If
    End If
End Sub

Private Sub moAS_NewData(Buffer() As Byte)
    RaiseEvent2("NewData", Buffer)
    Dim psBarcode As String
    psBarcode = moBC.StringFromBytes(Buffer, "UTF-8")
    #IF DEBUG
        LogColor("clsBarcodeReader.moAS_NewData: " & psBarcode, Colors.Magenta)
    #End If
    If psBarcode.EndsWith(CRLF) Then
        psBarcode = psBarcode.SubString2(0, psBarcode.Length - 2)
    Else
        If psBarcode.EndsWith(Chr(13)) Or psBarcode.EndsWith(Chr(10)) Then
            psBarcode = psBarcode.SubString2(0, psBarcode.Length - 1)
        End If
        If psBarcode.EndsWith(Chr(13)) Or psBarcode.EndsWith(Chr(10)) Then
            psBarcode = psBarcode.SubString2(0, psBarcode.Length - 1)
        End If
    End If
    RaiseEvent2("BarcodeScanned", psBarcode)
End Sub

Private Sub moAS_Error
    #If DEBUG
        LogColor("clsBarcodeReader.moAs_Error - "  & LastException.Message, Colors.Magenta)
    #End If
    moAS.Close
    moSerial.Disconnect
    If mbMonitoring Then
        tmrReconnect.Enabled = True
    Else
        RaiseEvent2("Error", msLastError)
    End If
End Sub

Private Sub moAS_Terminated
    #If DEBUG
        Try
            LogColor("clsBarcodeReader.moAs_Terminated - "  & LastException.Message, Colors.Magenta)
        Catch
            Log(LastException)
        End Try
    #End If
    mbConnected = False
    moAS.Close
    moSerial.Disconnect
    If mbMonitoring Then
        tmrReconnect.Enabled = True
    Else
        RaiseEvent("Terminated")
    End If
End Sub



Private Sub RaiseEvent(EventName As String)                   
    If msBaseEvent <> "" Then
        Dim psEvent As String = msBaseEvent & EventName
        Try
            If SubExists(moCaller, psEvent) Then
                CallSubDelayed(moCaller, psEvent)
            End If
        Catch
            Log(LastException)
        End Try
    End If
End Sub

Private Sub RaiseEvent2(EventName As String, Parameter As Object) 
    If msBaseEvent <> "" Then
        Dim psEvent As String = msBaseEvent & EventName
        Try
            If SubExists(moCaller, psEvent) Then
                CallSubDelayed2(moCaller, psEvent, Parameter)
            End If
        Catch
            Log(LastException)
        End Try
    End If
End Sub

#End Region
 
Upvote 0

Martin Domian

Member
Licensed User
Longtime User
Thanks all togeather!

I will try out everything the next days and hope something will fit. I need a little bit time for this.
I will not be bounded on one device only, so installing an external Lib or driver from one Company only is not so best solution.
 
Upvote 0

Martin Domian

Member
Licensed User
Longtime User
The Barcode Readerclass comes with compile error, which i do not understand....
 
Upvote 0
Top