/**
* 能够发现NFC的Activity
*/
public abstract class NfcActivity extends BaseActivity {
/**
* TECH_DISCOVERED、NDEF_DISCOVERED、TAG_DISCOVERED:NFC发现成功的标志
*/
public boolean mIsNfcDiscoverd = false;
private static final String TAG = NfcActivity.class.getSimpleName();
@Override
public void onNewIntent(Intent intent) {
setIntent(intent);
}
@Override
public void onResume() {
super.onResume();
String action = getIntent().getAction();
mIsNfcDiscoverd = false;
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Log.d(TAG, "ACTION_NDEF_DISCOVERED");
mIsNfcDiscoverd = true;
}
else if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
Log.d(TAG, "ACTION_TAG_DISCOVERED");
mIsNfcDiscoverd = true;
}
else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
Log.d(TAG, "ACTION_TECH_DISCOVERED");
mIsNfcDiscoverd = true;
}
else {
//ACTION NULL DISCOVERED
}
if (mIsNfcDiscoverd) {
// 发现NFC了,进来了。真格的来了。
//1. 去初始化容器啥啥的
。。。
//2. 去取NDEFmessage
getNdefMessages(getIntent());
//3. 留给子类
touchedNFC();
//4. 禁用这一次的触摸,否则可能会又一次重新开始。
getIntent().setAction(null);
}
}
/**
* NFC接触了。留给子类干点啥啥事情。。
*/
protected void touchedNFC() {
}
/**
* 去取NdefMessages
*/
private NdefMessage[] getNdefMessages(Intent intent) {
// 这里边 网络文章多为如下步骤。
// 解析这个 intent
NdefMessage[] msgs = null;
String action = intent.getAction();
Log.d(TAG, "_intent.getAction():" + action);
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String[] list = tagFromIntent.getTechList();
for (String v : list) { Log.d(TAG, "_getTechList():" + v); }
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs == null) {
// 不识别的类型。。
Log.d(TAG, "_Unknown tag type_");
Log.d(TAG, "_rawMsgs == null " + action);
byte[] empty = new byte[]{ };
NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
NdefMessage msg = new NdefMessage(new NdefRecord[]{record});
msgs = new NdefMessage[]{msg};
} else {
Log.d(TAG, "_rawMsgs != null ");
msgs = new NdefMessage[rawMsgs.length];
String nDEFMessageNum = "NDEFMessage: " + rawMsgs.length + " Message(s)" + "\n";
String recordContent = "";
String recordNum = "";
String recordId = "";
String tnf = "";
String type = "";
String id = "";
String payload = "";
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
for (int j = 0; j < msgs[i].getRecords().length; j++) {
recordNum = "NDEFMessage: "
+ msgs[i].getRecords().length + " Record(s)"
+ "\n\n";
recordId = "\"Record\", " + "\"" + String.valueOf(j)
+ "\"" + "\n";
tnf = "\"TNF\", " + "\""
+ getTnfName(msgs[i].getRecords()[j].getTnf())
+ "\"" + "\n";
type = "\"Type\", " + "\""
+ new String(msgs[i].getRecords()[j].getType())
+ "\"" + "\n";
id = "\"ID\", " + "\""
+ new String(msgs[i].getRecords()[j].getId())
+ "\"" + "\n";
payload = "\"Payload\", "
+ "\""
+ new String(
msgs[i].getRecords()[j]
.getPayload()) + "\""
+ "\n";
String recordType = new String(msgs[i].getRecords()[j].getType()).toLowerCase();
try {
parsePayload(msgs[i].getRecords()[j], recordType);
} catch (Exception e) {
// 又TM异常了。。。
e.printStackTrace();
}
recordContent += recordId + tnf + type + id + payload
+ "\n";
}
Log.d(TAG, "得到了的_NDEF Message:" + nDEFMessageNum + recordNum + recordContent);
}
}
return msgs;
}
/**
* 去解析NdefRecord
*/
public void parsePayload(NdefRecord record, String recordType) {
if (recordType.equals(***Type)) {
try {
NDEFinfo info = NDEFreader.getPrinterInfoFromNFC(record);
。。。
} catch (NDEFreadException e) {
//异常了。。
e.printStackTrace();
}
} else if (recordType.equals(lanType)) {
try {
NDEFinfo info = NDEFreader.getPrinterInfoFromNFC(record);
。。。
} catch (NDEFreadException e) {
e.printStackTrace();
}
}
}
/**
* 支持NFC的界面类
*/
public class MyActivity extends NFCActivity {
@Override
public void onResume() {
super.onResume();
if(mIsNfcDiscoverd) {
//NFC 被发现了,,要开始整啥啥任务。通常startService..
//Service 起个异步task...
}
}
}
/**
* 不支持NFC的界面类
*/
public class BaseActivity extends Activity {
protected NfcAdapter mNfcAdapter;
protected PendingIntent mPendingIntent;
protected IntentFilter[] mFilters;
protected String[][] mTechLists;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter != null) {
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// ・NDEF_DISCOVERED
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
// ・TAG_DISCOVERED
IntentFilter tag = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
// ・TECH_DISCOVERED
IntentFilter tech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
try {
ndef.addDataType("application/octet-stream");
} catch (IntentFilter.MalformedMimeTypeException e) {
e.printStackTrace();
}
mFilters = new IntentFilter[]{tag, tech, ndef};
mTechLists = new String[][]{new String[]{MifareClassic.class.getName()},
new String[]{MifareUltralight.class.getName()},
new String[]{IsoDep.class.getName()},
new String[]{NfcB.class.getName()},
new String[]{NfcF.class.getName()},
new String[]{NfcV.class.getName()},
new String[]{Ndef.class.getName()},
new String[]{NdefFormatable.class.getName()}
};
}
}
@Ovverride
public void onNewIntent(Intent intent) {
String action = intent.getAction();
boolean isNfcDiscovered = false;
if (action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)){
isNfcDiscovered = true;
} else if(action.equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
isNfcDiscovered = true;
} else if(action.equals(NfcAdapter.ACTION_TECH_DISCOVERED)){
isNfcDiscovered = true;
} else {
//Action null discovered.
}
if (isNfcDiscovered) {
Toast toast = Toast.makeText(this, "这个界面不支持NFC", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
@Override
public void onResume() {
super.onResume();
if (mNfcAdapter != null) {
mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);
}
}
@Override
protected void onPause() {
super.onPause();
if (mNfcAdapter != null) {
mNfcAdapter.disableForegroundDispatch(this);
}
}
}
网友评论