Last active
June 7, 2017 17:48
-
-
Save dzindra/4f5288edd757a11a432a to your computer and use it in GitHub Desktop.
Quick and dirty way how to use libnfc in java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# set your own paths and gcc options | |
NFCPATH=/usr/local/Cellar/libnfc/1.7.0 | |
JAVAPATH=/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home | |
swig -java -I$NFCPATH/include/ -outdir src/nfc -package nfc nfc.i | |
gcc -dynamiclib -I$NFCPATH/include/ -I$JAVAPATH/include/ -I$JAVAPATH/include/darwin/ -lnfc -L$NFCPATH/lib -o nfcjni.dylib nfc_wrap.c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nfc; | |
import javax.smartcardio.CommandAPDU; | |
public class LibnfcJava { | |
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray(); | |
public static String bytesToHex(byte[] bytes, int count) { | |
int len = Math.min(bytes.length, count); | |
char[] hexChars = new char[len * 3]; | |
for (int j = 0; j < len; j++) { | |
int v = bytes[j] & 0xFF; | |
hexChars[j * 3] = hexArray[v >>> 4]; | |
hexChars[j * 3 + 1] = hexArray[v & 0x0F]; | |
hexChars[j * 3 + 2] = ' '; | |
} | |
return new String(hexChars); | |
} | |
public static String bytesToHex(byte[] bytes) { | |
return bytesToHex(bytes, bytes.length); | |
} | |
public static void main(String[] args) { | |
System.load("nfcjni.dylib"); // TODO : you must add full path or loading fails | |
SWIGTYPE_p_p_nfc_context ctxPtr = nfc.new_SWIGTYPE_p_p_nfc_context(); | |
nfc.nfc_init(ctxPtr); | |
SWIGTYPE_p_nfc_context ctx = nfc.SWIGTYPE_p_p_nfc_context_value(ctxPtr); | |
nfc.delete_SWIGTYPE_p_p_nfc_context(ctxPtr); | |
SWIGTYPE_p_nfc_device reader = nfc.nfc_open(ctx, null); | |
System.out.println("NFC version: "+nfc.nfc_version()); | |
System.out.println("Initiator init result: "+nfc.nfc_initiator_init(reader)); | |
nfc_modulation mod = new nfc_modulation(); | |
mod.setNbr(nfc_baud_rate.NBR_106); | |
mod.setNmt(nfc_modulation_type.NMT_ISO14443A); | |
nfc_target target = new nfc_target(); | |
System.out.println("Select passive target result: "+nfc.nfc_initiator_select_passive_target(reader, mod, new byte[0], 0, target)); | |
nfc_iso14443a_info nai = target.getNti().getNai(); | |
System.out.println("ATS: " + bytesToHex(nai.getAbtAts(), (int) nai.getSzAtsLen())); | |
byte[] result = new byte[256]; | |
byte[] command = new CommandAPDU(0x00, 0xA4, 0x04, 0x00, "1PAY.SYS.DDF01".getBytes()).getBytes(); | |
int count = nfc.nfc_initiator_transceive_bytes(reader, command, command.length, result, result.length, 0); | |
System.out.println("Response length: " + count); | |
System.out.println("Response data: " + bytesToHex(result, count)); | |
nfc.nfc_close(reader); | |
nfc.nfc_exit(ctx); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%module nfc | |
%{ | |
#include <nfc/nfc.h> | |
#include <nfc/nfc-types.h> | |
#include <nfc/nfc-emulation.h> | |
%} | |
// for some reason these functions caused linking failures on osx | |
%ignore iso14443b_crc; | |
%ignore iso14443b_crc_append; | |
%ignore iso14443a_locate_historical_bytes; | |
%include "arrays_java.i" | |
JAVA_ARRAYS_DECL(uint8_t, jbyte, Byte, Uint8) | |
JAVA_ARRAYS_IMPL(uint8_t, jbyte, Byte, Uint8) | |
JAVA_ARRAYS_TYPEMAPS(uint8_t, byte, jbyte, Uint8, "[B") | |
%apply uint8_t[] { uint8_t* } | |
%include <nfc/nfc.h> | |
%include <nfc/nfc-types.h> | |
%include <nfc/nfc-emulation.h> | |
%include "cpointer.i" | |
%pointer_functions(nfc_context*, SWIGTYPE_p_p_nfc_context) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
Do you know how do I send a file to an android mobile device with this wrapper in java?