Hi All,
I would like to highlight the process to connect programmatically any android tablet to RFID reader and read tags. SO basically what i did, Downloaded latest RFID SDK (2.0.3.162) and unzip it and added inside Lib folder of android studio(Ladybug) and build gradle(8.7) Added code in my kotlin project to connect reader, where my RFID Reader is FX9600.
Source Code:
import android.os.Bundle
import android.os.StrictMode
import android.os.StrictMode.ThreadPolicy
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import com.dcs.sanitrader.R
import com.dcs.sanitrader.databinding.ActivityRfidScanBinding
import com.dcs.sanitrader.databinding.HomepageBinding
import com.zebra.rfid.api3.ENUM_TRANSPORT
import com.zebra.rfid.api3.InvalidUsageException
import com.zebra.rfid.api3.OperationFailureException
import com.zebra.rfid.api3.RFIDReader
import com.zebra.rfid.api3.ReaderDevice
import com.zebra.rfid.api3.Readers
import com.zebra.rfid.api3.RfidEventsListener
import com.zebra.rfid.api3.RfidReadEvents
import com.zebra.rfid.api3.RfidStatusEvents
class RFIDActivity : AppCompatActivity() {
private var rfidReader: RFIDReader? = null
private var readersList: Readers? = null
private var readerDevice: ReaderDevice? = null
private lateinit var connectionEventHandler: EventHandler
private lateinit var binding: ActivityRfidScanBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityRfidScanBinding.inflate(layoutInflater)
setContentView(binding.root)
enableEdgeToEdge()
val policy = ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
// Initialize the RFID reader
binding.startScanButton.setOnClickListener { initializeRFIDReader() }
}
private fun initializeRFIDReader() {
try {
// Initialize readers
readersList = Readers(this, ENUM_TRANSPORT.SERVICE_USB)
//binding.tagDataTextview.text = "RFID Reader is connecting by SERVICE_USB"
val availableRFIDReaderList = readersList?.GetAvailableRFIDReaderList()
if (!availableRFIDReaderList.isNullOrEmpty()) {
readerDevice = availableRFIDReaderList[0]
rfidReader = readerDevice?.rfidReader
connectToRFIDReader()
} else {
//binding.tagDataTextview.text = "RFID Reader is not connected"
Toast.makeText(this, "No RFID readers available", Toast.LENGTH_SHORT).show()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
private fun connectToRFIDReader() {
try {
if (rfidReader != null && !rfidReader!!.isConnected) {
// Connect to the RFID reader
rfidReader?.connect()
//binding.tagDataTextview.text = "RFID Reader is connected"
Toast.makeText(this, "RFID Reader connected", Toast.LENGTH_SHORT).show()
// Set up the RFID tag event handler
setupReaderHandlers()
// Enable tag reading (once connected)
rfidReader?.Actions?.Inventory?.perform()
}
} catch (e: InvalidUsageException) {
e.printStackTrace()
//binding.tagDataTextview.text = "RFID Reader error: ${e.printStackTrace()}"
} catch (e: OperationFailureException) {
e.printStackTrace()
//binding.tagDataTextview.text = "RFID Reader error ${e.printStackTrace()}"
}
}
private fun setupReaderHandlers() {
// Set up event handler for tag data
rfidReader?.Events?.addEventsListener(connectionEventHandler)
// Configure event notifications (enable RFID data capture)
rfidReader?.Events?.apply {
setInventoryStartEvent(true)
setInventoryStopEvent(true)
setTagReadEvent(true)
}
}
override fun onDestroy() {
super.onDestroy()
// Clean up and disconnect when done
try {
rfidReader?.disconnect()
} catch (e: InvalidUsageException) {
e.printStackTrace()
} catch (e: OperationFailureException) {
e.printStackTrace()
}
}
// Define your event handler to capture tag data
private inner class EventHandler : RfidEventsListener {
override fun eventReadNotify(e: RfidReadEvents) {
// This function is called when a tag is read
val tags = rfidReader?.Actions?.getReadTags(100)//e.readEventData.tagData//rfidReader?.readerCapabilities?.rfidOptions?.rfidTags
tags?.forEach { tag ->
runOnUiThread {
Toast.makeText(
this@RFIDActivity,
"Tag scanned: ${tag.tagID}",
Toast.LENGTH_SHORT
).show()
}
println("Tag read: ${tag.tagID}")
}
}
override fun eventStatusNotify(e: RfidStatusEvents?) {
// Handle reader status events here
}
}
}
Error:
The error am getting is as, Operation Failed: Connection error, no RFID reader available
I don't know where am i wrong? and what should be the better approach to connect the reader from tablet? I am also using same network in reader and tablet. Let me know what should i do?
4 Replies
As per the release notes, the RFID SDK for Android supports Zebra mobile readers only - it is not compatible with fixed readers such as the FX9600. The method recommended to interface to the FX9600 from an Android tablet would be to connect both to a WiFi network and then configure one of the IOT Connector protocol options to send data between the two e.g. MQTT .
There is also an alternative:
The up-to-date RFID SDK for Android Studio - does contain the "ZIOTCSampleApp" project - this is designed to allow the Android Device to connect to either the Fixed "FX-Series" readers via LLRP-Based RFID SDK, as well as the Fixed "FXR-Series" FXR90 using the ZIOTC support to the FXR. You do need to have V2.0.3.148++ of the RFID SDK for Android Studio to try these demo apps out.
NOTE: The ZIOTC uses the IoT connection that the FXR90 is setup with by Default - if you have configured a spearate MQTT endpoint on the FXR90 - you will not be able to connect to it with this demo app. ( I would suggest to backup your settings - and "enterprise" reset the FXR90 settings to defaults before you begin testing.)
the code you are using is trying to connect via USB as if it where a mobile reader.
there is a possibility to connect to fixed readers from android sdk but you need to follow the below (where the method for fixed readers is slightly different):
https://techdocs.zebra.com/dcs/rfid/android/2-0-3-162/guide/connection/
more useful info here to do some other operations:
https://techdocs.zebra.com/dcs/rfid/android/2-0-3-162/tutorials/
https://techdocs.zebra.com/dcs/rfid/android/2-0-3-162/samples/fx_hellorfid/