android nfc读写 demo,Android Nfc Sample Demo - reads only fake information from the Tag

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值