Introduction:
android-nfc is an open-source NFC library based on Java, offering NFC-related functionalities to Android developers. It supports Android 4.4 (KitKat) and above.
Example Code (Java):
import android.nfc.NfcAdapter;
import android.nfc.Tag;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the NFC adapter
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
// Register an NFC listener
nfcAdapter.setOnTagDiscoveredListener(new NfcAdapter.OnTagDiscoveredListener() {
@Override
public void onTagDiscovered(Tag tag) {
// Get NFC tag information
byte[] tagId = tag.getId();
String tagType = tag.getTechList()[0].toString();
// Process NFC tag information
Log.d("NFC", "tagId: " + Arrays.toString(tagId));
Log.d("NFC", "tagType: " + tagType);
}
});
}
}
Please use the code with caution. Refer to the documentation for more details.
Key Features:
- Read NFC tag information.
- Write NFC tag information.
- Start NFC card emulation.
Downloading:
android-nfc can be downloaded from GitHub:
git clone https://github.com/android/android-nfc.git
Installation:
android-nfc has the following dependencies:
- Android SDK
You can install it as follows:
cd android-nfc
./gradlew build
Usage:
For detailed instructions on how to use android-nfc, please refer to the documentation:
https://developer.android.com/reference/android/nfc/package-summary
In conclusion, android-nfc is a powerful NFC library suitable for various NFC application scenarios. It offers the following advantages:
- Supports Android 4.4 and above.
- Provides rich functionality.
- Clear and understandable code.
- Comprehensive documentation.
Additional notes:
android-nfc also provides advanced features such as:
- NFC card emulation.
- NFC near-field communication.
- NFC security.
When using android-nfc, please consider the following:
- Devices with Android 4.4 and below do not support NFC card emulation.
- Devices with Android 6.0 and above require NFC permissions to be enabled.