-
Notifications
You must be signed in to change notification settings - Fork 1
/
RFIDReaderInterface.kt
134 lines (119 loc) · 5.23 KB
/
RFIDReaderInterface.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.spozebra.zebrarfidsledsample.rfid
import android.content.Context
import android.util.Log
import com.spozebra.zebrarfidsledsample.MainActivity
import com.spozebra.zebrarfidsledsample.ScanConnectionEnum
import com.zebra.rfid.api3.*
import java.util.*
class RFIDReaderInterface(var listener: IRFIDReaderListener) : RfidEventsListener {
private val TAG: String = RFIDReaderInterface::class.java.simpleName
private lateinit var readers: Readers
private var availableRFIDReaderList: ArrayList<ReaderDevice>? = null
private var readerDevice: ReaderDevice? = null
lateinit var reader: RFIDReader
fun connect(context: Context, scanConnectionMode : ScanConnectionEnum): Boolean {
// Init
readers = Readers(context, ENUM_TRANSPORT.ALL)
try {
if (readers != null) {
availableRFIDReaderList = readers.GetAvailableRFIDReaderList()
if (availableRFIDReaderList != null && availableRFIDReaderList!!.size != 0) {
// get first reader from list
readerDevice = availableRFIDReaderList!![0]
reader = readerDevice!!.rfidReader
if (!reader!!.isConnected) {
Log.d(TAG, "RFID Reader Connecting...")
reader!!.connect()
configureReader(scanConnectionMode)
Log.d(TAG, "RFID Reader Connected!")
return true
}
}
}
} catch (e: InvalidUsageException) {
e.printStackTrace()
} catch (e: OperationFailureException) {
e.printStackTrace()
} catch (e: OperationFailureException) {
e.printStackTrace()
} catch (e: InvalidUsageException) {
e.printStackTrace()
}
Log.d(TAG, "RFID Reader connection error!")
return false
}
private fun configureReader(scanConnectionMode : ScanConnectionEnum) {
if (reader.isConnected) {
val triggerInfo = TriggerInfo()
triggerInfo.StartTrigger.triggerType = START_TRIGGER_TYPE.START_TRIGGER_TYPE_IMMEDIATE
triggerInfo.StopTrigger.triggerType = STOP_TRIGGER_TYPE.STOP_TRIGGER_TYPE_IMMEDIATE
try {
// receive events from reader
reader.Events.addEventsListener(this)
// HH event
reader.Events.setHandheldEvent(true)
// tag event with tag data
reader.Events.setTagReadEvent(true)
// application will collect tag using getReadTags API
reader.Events.setAttachTagDataWithReadEvent(false)
// set start and stop triggers
reader.Config.startTrigger = triggerInfo.StartTrigger
reader.Config.stopTrigger = triggerInfo.StopTrigger
// Terminal scan, use trigger for scanning!
if(scanConnectionMode == ScanConnectionEnum.TerminalScan)
reader.Config.setKeylayoutType(ENUM_KEYLAYOUT_TYPE.UPPER_TRIGGER_FOR_SCAN)
else
reader.Config.setKeylayoutType(ENUM_KEYLAYOUT_TYPE.UPPER_TRIGGER_FOR_SLED_SCAN)
} catch (e: InvalidUsageException) {
e.printStackTrace()
} catch (e: OperationFailureException) {
e.printStackTrace()
}
}
}
// Status Event Notification
override fun eventStatusNotify(rfidStatusEvents: RfidStatusEvents) {
Log.d(TAG, "Status Notification: " + rfidStatusEvents.StatusEventData.statusEventType)
if (rfidStatusEvents.StatusEventData.statusEventType === STATUS_EVENT_TYPE.HANDHELD_TRIGGER_EVENT) {
if (rfidStatusEvents.StatusEventData.HandheldTriggerEventData.handheldEvent === HANDHELD_TRIGGER_EVENT_TYPE.HANDHELD_TRIGGER_PRESSED) {
try {
reader.Actions.Inventory.perform();
} catch (e: Exception) {
e.printStackTrace()
}
} else if (rfidStatusEvents.StatusEventData.HandheldTriggerEventData.handheldEvent === HANDHELD_TRIGGER_EVENT_TYPE.HANDHELD_TRIGGER_RELEASED) {
try {
reader.Actions.Inventory.stop()
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
// Read Event Notification
override fun eventReadNotify(e: RfidReadEvents) {
// Recommended to use new method getReadTagsEx for better performance in case of large tag population
val myTags: Array<TagData> = reader.Actions.getReadTags(100)
if (myTags != null) {
for (tag in myTags) {
listener.newTagRead(tag.tagID)
}
}
}
fun onDestroy() {
try {
if (reader != null) {
reader.Events?.removeEventsListener(this)
reader.disconnect()
reader.Dispose()
readers.Dispose()
}
} catch (e: InvalidUsageException) {
e.printStackTrace()
} catch (e: OperationFailureException) {
e.printStackTrace()
} catch (e: Exception) {
e.printStackTrace()
}
}
}