Skip to content

Commit

Permalink
支持NFC
Browse files Browse the repository at this point in the history
  • Loading branch information
CeuiLiSA committed Jul 10, 2020
1 parent a879471 commit b98220b
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
<activity android:name=".activities.UserActivity" />
<activity android:name=".activities.SearchActivity" />
<activity android:name=".activities.VActivity" />

</application>

</manifest>
69 changes: 69 additions & 0 deletions app/src/main/java/ceui/lisa/activities/NfcDemoActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ceui.lisa.activities;

import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;

import ceui.lisa.R;
import ceui.lisa.base.BaseActivity;
import ceui.lisa.databinding.ActivityNfcBinding;
import ceui.lisa.utils.Common;

public class NfcDemoActivity extends BaseActivity<ActivityNfcBinding> {

private NfcAdapter mNfcAdapter;
private PendingIntent pi;

@Override
protected int initLayout() {
return R.layout.activity_nfc;
}

@Override
protected void initView() {
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
processIntent(getIntent());
}

@Override
protected void initData() {

}

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
processIntent(intent);
}
}

private void processIntent(Intent intent) {
//取出封装在intent中的TAG
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String CardId =ByteArrayToHexString(tagFromIntent.getId());
Common.showLog("CardId " + tagFromIntent.toString());
Common.showLog("intent " + intent.toString());
}

private String ByteArrayToHexString(byte[] inarray) {
int i, j, in;
String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
"B", "C", "D", "E", "F" };
String out = "";


for (j = 0; j < inarray.length; ++j) {
in = (int) inarray[j] & 0xff;
i = (in >> 4) & 0x0f;
out += hex[i];
i = in & 0x0f;
out += hex[i];
}
return out;
}
}
23 changes: 23 additions & 0 deletions app/src/main/res/layout/activity_nfc.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>

</data>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.qmuiteam.qmui.widget.QMUIVerticalTextView
android:layout_width="wrap_content"
android:text="NFC"
android:textSize="100sp"
android:layout_centerInParent="true"
android:layout_height="wrap_content">


</com.qmuiteam.qmui.widget.QMUIVerticalTextView>

</RelativeLayout>
</layout>
30 changes: 30 additions & 0 deletions app/src/main/res/xml/nfc_tech_filter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.IsoDep</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcB</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcF</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcV</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.Ndef</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NdefFormatable</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.MifareClassic</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
</resources>

0 comments on commit b98220b

Please sign in to comment.