开发者

Android Nfc Sample Demo - reads only fake information from the Tag

I just installed the Nfc Demo from google, but it doesn´t read the information from the Tag.-> It just provides some fakeTag information. Has anybody an idea, where I can change the sample to read from the nfc Tag? Or has 开发者_如何学编程somebody a working nfc demo for the nexus?

If we could bring a nfc demo to work, many people would have the possibility to develop a nfc demo on their own.

Best regards Alexander


I had the same problem getting tag id. I got some B@2346323143 style data to screen. I got it to work like this:

byte[] byte_id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);

You need to convert byte[] to hex string. For example using following method.

private static final byte[] HEX_CHAR_TABLE = { (byte) '0', (byte) '1',
        (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6',
        (byte) '7', (byte) '8', (byte) '9', (byte) 'A', (byte) 'B',
        (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F' };

public static String getHexString(byte[] raw, int len) {
    byte[] hex = new byte[2 * len];
    int index = 0;
    int pos = 0;

    for (byte b : raw) {
        if (pos >= len)
            break;

        pos++;
        int v = b & 0xFF;
        hex[index++] = HEX_CHAR_TABLE[v >>> 4];
        hex[index++] = HEX_CHAR_TABLE[v & 0xF];
    }

    return new String(hex);
}


There are two parts to the NfcDemo. There is the detector activity, which responds to NFC tag intents, then there is the FakeTag activity which allows you to send fake tag intents to the first part. But the first part will detect real NFC tags too, as long as NFC is enabled. Check under Settings -> Wireless to see if NFC is turned on. If it is and you have the NfcDemo installed, you should be able to detect NFC tags. However, the NfcDemo is only configured to detect NDEF tags, so if you have some other type of NFC tag (e.g., Mifare Classic), you'll either need to get another app, or modify NfcDemo to handle the other NFC tag types.


I have written a class with some basic NFC function, hopefully it helps someone else to get a solution of reading some NFC Tag.

public class StatusMessage extends Activity {

/**
 * Returns the Status of the Nfc Device with a String "enabled" or "disabled"
 * \return Status NfcDevice
 * @author Falkenstein
 * 
 */
public static String getStatusNfcDevice () {
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter();
if (nfcAdapter.isEnabled()) { 
    String status = "enabled";

return status;
    }
    else {
        String status = "disabled";
        return status;


    }

}
/**
 * Returns the TagId. Needs an Intent. So you have to get you intent from your "main" activity and give it to the method -> just add the following   *lines in your "main class"
     *Intent intent =new Intent();
    *System.out.println(com.example.StatusMessage.getNfcAdapterExtraID(intent));
 *@author Falkenstein
 */ 
public static String getNfcAdapterExtraID (Intent intent) {
    byte[] byte_id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
    return byte_id.toString();
}


/**
 * Converts a byte to a String.
 * @param input
 * @return byte
 */
public String byteToStr(byte[] input) {
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < input.length; i++)
        if (input[i] != 0) {
            buffer.append( new Character((char)input[i]).toString());
        }
    return buffer.toString();
}



/**
 * Converts a String to a Byte
 * @param input
 * @return
 */ 
public byte[] strToByte(String input) {

    byte[] buffer = new byte[(input.length()+1)*2];
    for (int i = 0; i < buffer.length-2; i = i+2) {

        buffer[i] = (byte)input.charAt(i/2);
        buffer[i+1] = 0;
    }
    buffer[buffer.length-2] = 0;
    buffer[buffer.length-1] = 0;


    return buffer;
}


I've written some tools for NFC on Android, including a working example project for reading and writing real tags. I've also done a simple rewrite of the NFCDemo project you might be interested in.

I've also added broadcast send / receive capability to the NFC Developer app so that more people can play with NFC, i.e. without needing an NFC device.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜