SimpleNFC


Source link: https://github.com/tawaasalage/SimpleNFC

SimpleNFC

SimpleNFC is a project which include insanely easy way of reading NFC data

Tech

SimpleNFC uses a number of open source lib to work properly:

  • [RXJava] - Reactive Extensions for the JVM
  • [RXAndroid] - Reactive Extensions for Android

Integrate for custom projects

Implement NFCInterface

public class MainActivity extends Activity implements NFCInterface{
 ....

Create Subscription local variable

 private Subscription nfcSubscription;

Fill the implemented methods

 @Override
  protected void onResume() {

super.onResume();

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

IntentFilter filter = new IntentFilter();

filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);

filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);

filter.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);

nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[]{
filter
}
, this.techList);
  
}

@Override
  protected void onPause() {

super.onPause();

// disabling foreground dispatch:

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);

nfcAdapter.disableForegroundDispatch(this);

}

@Override
  protected void onNewIntent(Intent intent) {

if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {

  String type = intent.getType();

 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

 nfcReader(tag);

}

  
}

@Override
  public String nfcRead(Tag t) {

try {

 Tag tag = t;

 Ndef ndef = Ndef.get(tag);

 if (ndef == null) {

  return null;

 
}

 NdefMessage ndefMessage = ndef.getCachedNdefMessage();

 NdefRecord[] records = ndefMessage.getRecords();

 for (NdefRecord ndefRecord : records)

 {

  if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT))

  {

try {
return readText(ndefRecord);
}
 catch (UnsupportedEncodingException e) {

}

  
}

 
}

}

catch (Exception e)

{

 return null;

}

return null;
  
}

@Override
  public String readText(NdefRecord record) throws UnsupportedEncodingException {

byte[] payload = record.getPayload();

String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16"; 

  int languageCodeLength = payload[0] & 0063; 

  return new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
 
 
}
  
 @Override 
 public void nfcReader(Tag tag) {
  

  nfcSubscription= Observable.just(nfcRead(tag))

  .subscribeOn(Schedulers.newThread())

  .observeOn(AndroidSchedulers.mainThread())

  .subscribe(new Observer<String>() {

@Override

public void onCompleted() {

 
}

 @Override

public void onError(Throwable e) {

 
}

 @Override

public void onNext(String s) {

 if (s != null) {

txtNFCID.setText(s);

  
}

}

  
}
);
  
}

Define onDestroy and unsubscribe on that

 @Override
  protected void onDestroy() {

super.onDestroy();

 unsubscribe(nfcSubscription);

}

 private static void unsubscribe(Subscription subscription) {

if (subscription != null && !subscription.isUnsubscribed()) {

 subscription.unsubscribe();

 subscription = null;

}

  
}

Resources

Dexposed is a powerful yet non-invasive runtime AOP (Aspect-oriented Programming) framework for Android app development, based on the work of open-source Xposed framework project.

A method call logging based on dexposed.

A gallery used to host an array of images.

Android library to use Google Analytics through annotations.

The SimpleCropView is an image cropping library for Android. It simplify your code for cropping image and provides easily customizable UI.

With SnackbarAnimation class you can clone the Snackbar animation to move the ViewGroup above of the Snackbar when it is visible and avoid view overlapping (clone of fab animation).

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes