【发布时间】:2016-03-03 05:01:54
【问题描述】:
我创建了一个读取 NFC 卡 ID 的应用。我想如何用我的 Android 设备模拟该卡,以便 NFC 读卡器可以像以前读取 NFC 卡一样读取它。所以最终,我想用 Android 设备替换 NFC 卡。
这是我从卡中读取 ID 的代码:
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity{
TextView txt;
NfcAdapter nfcAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.textView);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
}
@Override
protected void onNewIntent(Intent intent) {
Toast.makeText(this,"Hello, I'm NFCTESTER", Toast.LENGTH_LONG).show();
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
txt.setText(tag.getId().toString());
super.onNewIntent(intent);
}
@Override
protected void onResume() {
super.onResume();
Intent intent = new Intent(this, MainActivity.class).addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
IntentFilter[] intentFilter = new IntentFilter[]{};
nfcAdapter.enableForegroundDispatch(this,pendingIntent,intentFilter,null);
}
@Override
protected void onPause() {
super.onPause();
nfcAdapter.disableForegroundDispatch(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
该卡(学校卡)是 MIFARE Classic,包含一个 8 位十六进制代码(我们从学校的 IT 经理那里获得了此信息):
- 标签 ID(十六进制):d3 72 f5 24
- 标签 ID(十进制):3547526436
- 标签 ID(保留):620065491
- 标签 ID(字符串):24:f5:72:d3
【问题讨论】:
-
您期待什么?上面的代码监听 NFC tags 并显示它们的 ID。是什么让您认为此代码可用于与卡读卡器进行通信?
-
我想将卡片标签发送给手机上的读卡器?所以看手机不用校卡。我对代码的期望。 “你好,我是 NFC TEST”的作品。然后onNewIntent进入,writeNdefMessage...这是我的最终项目。谢谢您的帮助。简而言之,应用程序“Hello”甚至没有显示。
-
不是很清楚。您想用 NFC 手机上的模拟卡替换 NFC 学校卡吗?
-
完全正确。对不起,我的英语不好。我如何才能像使用 nfc 标签一样使用我的手机?
-
你有学校卡的规格吗?必须在手机上的模拟卡上重现功能。
标签: android clone nfc mifare hce