@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_nfc_lab); init();//刷新控件 //获取默认的NFC控制器 nfcAdapter = NfcAdapter.getDefaultAdapter(this); if (nfcAdapter == null) {
tv1.setText(\设备不支持NFC!\ finish(); return; }
if (!nfcAdapter.isEnabled()) {
tv1.setText(\请在系统设置中先启用NFC功能!\ finish(); return; }
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent. FLAG_ACTIVITY_SINGLE_TOP), 0); // Setup an intent filter for all MIME based dispatches IntentFilter ndef = new IntentFilter(NfcAdapter. ACTION_TECH_DISCOVERED); try { ndef.addDataType(\ } catch (MalformedMimeTypeException e) { throw new RuntimeException(\ } mFilters = new IntentFilter[] {ndef, }; // Setup a tech list for all NfcA tags mTechLists = new String[][] { new String[] {NfcA.class.getName() } };
}
private void init() { // TODO 自动生成的方法存根 tv1=(TextView) findViewById(R.id.tv1); tv2=(TextView) findViewById(R.id.tv2); tv3=(TextView) findViewById(R.id.tv3); }
@Override
protected void onResume() { // TODO 自动生成的方法存根 super.onResume(); nfcAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);
}
@Override
protected void onNewIntent(Intent intent) { // TODO 自动生成的方法存根 super.onNewIntent(intent); tv1.setText(\发现新的 Tag: \计数 String intentActionStr = intent.getAction();// 获取到本次启动的action
if (NfcAdapter.ACTION_NDEF_DISCOVERED. equals(intentActionStr)// NDEF类型 ||
NfcAdapter.ACTION_TECH_DISCOVERED.equals(intentActionStr)// 其他类型
||
NfcAdapter.ACTION_TAG_DISCOVERED.equals(intentActionStr)) {// 未知类型
//在intent中读取Tag id Tag tag = intent.getParcelableExtra(NfcAdapter. EXTRA_TAG); byte[] bytesId = tag.getId();// 获取id数组 info=ByteArrayChange.ByteArrayToHexString(bytesId); tv2.setText(\标签UID: \ tv3.setText(readTag(tag)); new SendDateToServer(handler).SendDataToServer(info); } }
public String readTag(Tag tag) { tv3=(TextView) findViewById(R.id.tv3); MifareClassic mfc = MifareClassic.get(tag); for (String tech : tag.getTechList()) { System.out.println(tech);//显示设备支持技术 } boolean auth = false; //读取TAG
try { String metaInfo = \ //Enable I/O operations to the tag from this TagTechnology object.
mfc.connect();//部分设备可能会连接失败,不支持读取下面功能
int type = mfc.getType();//获取TAG的类型 int sectorCount = mfc.getSectorCount();//获取TAG中包含的扇区数
String typeS = \ switch (type) { case MifareClassic.TYPE_CLASSIC: typeS = \ break; case MifareClassic.TYPE_PLUS: typeS = \ break; case MifareClassic.TYPE_PRO: typeS = \ break; case MifareClassic.TYPE_UNKNOWN: typeS = \ break; } metaInfo += \ 卡片类型:\共\sectorCount + \个扇区\\n共\
+ mfc.getBlockCount() + \个块\\n存储空间: \mfc.getSize()
+ \ return metaInfo; } catch (Exception e) { //Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace(); } finally { if (mfc != null) { try { mfc.close(); } catch (IOException e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG)
.show(); } }
} return \您的设备不支持查询标签信息功能\如果连接失败 }
@Override public void onPause() { super.onPause(); nfcAdapter.disableForegroundDispatch(this); }
}
在com.example.nfc_lab下的ByteArrayChange.java其中加入如下代码:
package com.example.nfc_lab; public class ByteArrayChange { public static String ByteArrayToHexString(byte[] bytesId) { //Byte数组转换为16进制字符串
// TODO 自动生成的方法存根 int i, j, in; String[] hex = { \\
}; String output = \ for (j = 0; j < bytesId.length; ++j) { in = bytesId[j] & 0xff; i = (in >> 4) & 0x0f; output += hex[i]; i = in & 0x0f; output += hex[i]; } return output; } }
在com.example.service下的SendDataToServer.java中加入以下代码: package com.example.service;
import java.net.HttpURLConnection; import java.net.URL;
import java.net.URLEncoder; import java.util.HashMap; import java.util.Map;
import android.os.Handler; /**
* 通过GET方式向服务器发送数据
*/
public class SendDateToServer {
//添加访问IP地址,注:改为自己的IP地址格式为 http://你的IP地址:8080 private static String
url=\ //模拟机测试的话,IP设为10.0.2.2 //192.168.0.106本机的IPv4地址 //关于本机的IP地址,点击电脑左下角的开始菜单,在搜索框内输入cmd,按下回车,然后在里面输入ipconfig,再按下回车键 //并在其中找到自己的Ipv4地址
public static final int SEND_SUCCESS=0x123; public static final int SEND_FAIL=0x124; private Handler handler;
public SendDateToServer(Handler handler) { // TODO Auto-generated constructor stub this.handler=handler; } /**
* 通过Get方式向服务器发送数据 * @param UID 标签号 */
public void SendDataToServer(String UID) { // TODO Auto-generated method stub final Map
* 发送GET请求
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库实验指导书 - -Android平台下的NFC读写 - 图文(6)在线全文阅读。
相关推荐: