Table of Contents
Installation
You can download and install the app from here.
Preamble
There are two different ways to use this app. The “Keyboard” method will write any data through the keyboard buffer to the currently focused text field. This means the RFID reader and scanner can be used in any 3rd party app without any code change.
The “Broadcast” method will send RFID and barcode data as a broadcast which has to be received in your app’s code. The second approach will require you to add some code to your app but will give you much more control over the service.
This document will guide you through the different methods you can integrate this service into your code.
Basic usage
In the most basic implementation, you’ll start the service to make sure it’s running and register a broadcast receiver to get the results.
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 |
// Broadcast receiver to barcode data (device == 1) or tag uid (device == 2) private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("com.handheldgroup.anysend.RESULT") == false) return; // The device which sent this data. RFID = 2 int device = intent.getIntExtra("device", -1); // Numeric representation of the tag type int type = intent.getIntExtra("type", -1); // String representation of the tag type String typeName = intent.getStringExtra("type_name"); // Data as byte array byte[] dataArray = intent.getByteArrayExtra("data"); // Data as String String dataString = intent.getStringExtra("string"); Log.i("anysend-result", "Device #" + device + " returned " + dataString + " for type " + typeName); } }; // Register the receiver registerReceiver(receiver, new IntentFilter("com.handheldgroup.anysend.RESULT")); // Start the service to make sure it's running Intent intent = new Intent("com.handheldgroup.anysend.RfidService.START"); intent.setPackage("com.handheldgroup.anysend"); // This extra is optional and let's you set a message to be shown as a toast after startup. // If set to a empty ("") string, no toast will be shown. intent.putExtra("startup", "Ready to scan tags"); startService(intent); |
When exiting your app you can stop the service and should unregister the receiver
Advanced usage
The basic example will work in most cases but there is one important thing to consider regarding the RFID Reader.
The module will scan for tags at a very short interval. While this means that tags will be detected quickly, it can also mean that the same tag will be scanned twice which will call the receiver more than once.
To prevent this, the reader can be turned on an off from your app. This can be used to only scan if a button is pressed and to stop after a successful read.
The following to functions let you control the reader
1 2 3 4 5 6 7 8 9 10 11 |
public void enableRfid() { Intent intent = new Intent("com.handheldgroup.anysend.SET_STATE"); intent.putExtra("device", true); sendBroadcast(intent); } public void disableRfid() { Intent intent = new Intent("com.handheldgroup.anysend.SET_STATE"); intent.putExtra("device", false); sendBroadcast(intent); } |
To only scan one tag, call disableRfid() at the beginning of your receiver function.
To only scan while a button is pressed you can use the following code in your activity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
private final int TRIGGER = KeyEvent.KEYCODE_F10; // Set to whichever key you want to use @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(event.getKeyCode() == TRIGGER && event.getRepeatCount() == 0){ enableRfid(); return true; } return super.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if(event.getKeyCode() == TRIGGER && event.getRepeatCount() == 0){ disableRfid(); return true; } return super.onKeyUp(keyCode, event); } |