NFC lifecycle wrapper


License

License

Categories

Categories

Utility General Purpose Libraries
GroupId

GroupId

com.github.skjolber.android.nfc-lifecycle-wrapper
ArtifactId

ArtifactId

utility
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

aar
Description

Description

NFC lifecycle wrapper
NFC lifecycle wrapper
Project URL

Project URL

https://github.com/skjolber/android-nfc-lifecycle-wrapper
Project Organization

Project Organization

Thomas Rorvik Skjolberg
Source Code Management

Source Code Management

https://github.com/skjolber/android-nfc-lifecycle-wrapper

Download utility

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.skjolber.android.nfc-lifecycle-wrapper/utility/ -->
<dependency>
    <groupId>com.github.skjolber.android.nfc-lifecycle-wrapper</groupId>
    <artifactId>utility</artifactId>
    <version>1.0.0</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.github.skjolber.android.nfc-lifecycle-wrapper/utility/
implementation 'com.github.skjolber.android.nfc-lifecycle-wrapper:utility:1.0.0'
// https://jarcasting.com/artifacts/com.github.skjolber.android.nfc-lifecycle-wrapper/utility/
implementation ("com.github.skjolber.android.nfc-lifecycle-wrapper:utility:1.0.0")
'com.github.skjolber.android.nfc-lifecycle-wrapper:utility:aar:1.0.0'
<dependency org="com.github.skjolber.android.nfc-lifecycle-wrapper" name="utility" rev="1.0.0">
  <artifact name="utility" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.github.skjolber.android.nfc-lifecycle-wrapper', module='utility', version='1.0.0')
)
libraryDependencies += "com.github.skjolber.android.nfc-lifecycle-wrapper" % "utility" % "1.0.0"
[com.github.skjolber.android.nfc-lifecycle-wrapper/utility "1.0.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

Build Status

android-nfc-lifecycle-wrapper

Library for NFC on Android using androidx lifecycle extensions and functional interfaces.

Features:

  • per-application or per-activity NFC behaviour
  • simple functional interfaces for
    • reader-callback and foreground-dispatch NFC events
      • tag scanned
      • tag removed
    • NFC adapter settings
  • programmatically enable/disable and optionally ignore NFC events

Bugs, feature suggestions and help requests can be filed with the issue-tracker.

License

Apache 2.0

Obtain

The project is built with Gradle and is available on the central Maven repository. For

ext {
  nfcLifecycleWrapperVersion = '1.0.0'
}

add

api("com.github.skjolber.android-nfc-lifecycle-wrapper:android:${nfcLifecycleWrapper}@aar")

Usage

If you prefer a working appliction, see the example.

The library relies on registering an Application.ActivityLifecycleCallbacks instance in the main Application (see further down).

Activities wanting to interact with NFC must add the interface NfcActivity or NfcCompatActivity and use the NfcFactory passed in theonPreCreated or onPostCreated methods to configure the desired NFC nature:

public void onPreCreated(NfcFactory nfcFactory) {
   nfcFactory.newForegroundDispatchBuilder()
     .withTagDiscovered( (tag, intent) -> {
        // your code here
      })
     .build();
}

Builders are available for foreground dispatch, reader callback and settings (NFC on/off on the device). Keep a reference to the resulting instance to programmatically enable/disable or ignore the resulting events.

Foreground dispatch

Create a NfcForegroundDispatchBuilder and pass in the desired (lambda) functions:

nfcFactory.newForegroundDispatchBuilder()
    .withTagDiscovered( (tag, intent) -> {
        Log.d(TAG, "Tag scanned");

        // your code here
    })
    .withTagRemoved( () -> {
        Log.d(TAG, "Tag removed");

        // your code here
    })
    .build();

and keep the resulting built NfcForegroundDispatch instance if you want to enable/disable or ignore NFC events.

Reader callback

Create a ReaderCallbackBuilder and pass in the desired (lambda) functions:

nfcFactory.newReaderCallbackBuilder()
    .withTagDiscovered( tag -> {
        Log.d(TAG, "Tag discovered    ");

        // your code here
    })
    .withTagRemoved( () -> {
        Log.d(TAG, "Tag removed");

        // your code here
    })
    .withMainThread()
    .withAllTagTechnologies()
    .build();

and keep the resulting built NfcReaderCallback instance if you want to enable/disable or ignore NFC events.

Settings

The current NFC settings is detected via NfcAdapter as the Activity's onResume(..) is called.

Create a SettingsBuilder and pass in the desired (lambda) functions:

factory.newSettingsBuilder()
    .withEnabled( (transition) -> {
        if (transition) {
            Log.d(TAG, "NFC setting transitioned to enabled.");
        } else {
            Log.d(TAG, "NFC setting is still enabled.");
        }
    })
    .withDisabled((transition, available) -> {
        if(available) {
            if (transition) {
                Log.d(TAG, "NFC setting transitioned to disabled.");
            } else {
                Log.d(TAG, "NFC setting is still disabled.");
            }
        } else {
            if (transition) {
                Log.d(TAG, "NFC is not available on this device.");
            } else {
                Log.d(TAG, "NFC is still not available on this device.");
            }
        }
    })
    .build();

and keep the resulting built NfcSettings instance if you want to enable/disable or ignore NFC Adapter events.

The transition passed to the lambda functions is true only on the inital call and when NFC is turned on or off on the device. This simplifies user notification if NFC functionality is optional.

Application setup

Wire up NfcActivityLifecycleCallbacks or (preferably) NfcCompatActivityLifecycleCallbacks with registerActivityLifecycleCallbacksand unregisterActivityLifecycleCallbacks:

public class NfcApplication extends Application {  
  
    protected NfcCompatActivityLifecycleCallbacks callbacks;  
  
    public void onCreate() {  
        super.onCreate();  
  
        callbacks = NfcCompatActivityLifecycleCallbacks.newBuilder().withApplication(this).build();  
        registerActivityLifecycleCallbacks(callbacks);  
    }  
  
    @Override  
    public void onTerminate() {  
        super.onTerminate();  
  
        unregisterActivityLifecycleCallbacks(callbacks);  
    }  
}

If using AppCompatActivity, prefer NfcCompatActivityLifecycleCallbacks, as it is better than NfcActivityLifecycleCallbacks because it can hook the NFC onPause(..) and onResume(..) wireing directly into the Activity's lifecycle instance, which removes 'manual' lookup of activity behaviour as the activity stack push / pop.

History

  • 1.0.0: Initial version.

Versions

Version
1.0.0