Android NFC 标签未检测到按钮的点击
Android NFC Tag is not detecting onClick of a button
我正在开发一个 Android 应用程序,我需要在其中单击按钮时扫描 NFC 标签。但是如果我点击一个按钮它没有检测到卡并且没有什么是happening.Help我来解决这个问题。
public class Check_in extends Activity {
EditText card_details;
PendingIntent pendingIntent;
IntentFilter[] intentFiltersArray;
String[][] techList;
NfcAdapter nfcAdapter;
LinearLayout scanContainer;
private boolean isScanEnabled = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check_in);
card_details = (EditText) findViewById(R.id.editText);
}
public void get_nfc(View v)
{
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if(nfcAdapter == null)
Toast.makeText(this,"This Device Not supporting NFC",Toast.LENGTH_SHORT).show();
else
handleIntent();
}
private void handleIntent() {
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
intentFiltersArray = new IntentFilter[] { ndef };
techList = new String[][] { new String[] { NfcA.class.getName(),
MifareUltralight.class.getName()} };
}
@Override
protected void onResume() {
super.onResume();
if(nfcAdapter !=null) {
if(nfcAdapter.isEnabled())
nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techList);
else
Toast.makeText(this,"NFC Disabled",Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onPause() {
if(nfcAdapter !=null)
nfcAdapter.disableForegroundDispatch(this);
super.onPause();
}
@Override
public void onNewIntent(Intent intent) {
if(!isScanEnabled)
return;
Parcelable tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
card_details.setText(dumpTagData(tag).toString());
Toast.makeText(this, "This is "+dumpTagData(tag).toString(), 5000).show();
}
private String dumpTagData(Parcelable p) {
StringBuilder sb = new StringBuilder();
Tag tag = (Tag) p;
byte[] id = tag.getId();
sb.append(getHex(id));
return sb.toString();
}
private String getHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i] & 0xff;
if (b < 0x10)
sb.append('0');
sb.append(Integer.toHexString(b));
}
return sb.toString();
}
}
在 onResume() 和 onPause() 方法中,nfcAdapter 始终为 null(因为您只能在单击按钮时获得 nfcAdapter)。为什么永远不会调用此 enableForegroundDispatch() 方法。
我对你的代码做了一些修改(但我还没有测试)。
public class Check_in extends Activity {
EditText card_details;
PendingIntent pendingIntent;
IntentFilter[] intentFiltersArray;
String[][] techList;
NfcAdapter nfcAdapter;
LinearLayout scanContainer;
private boolean isToScan = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check_in);
card_details = (EditText) findViewById(R.id.editText);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
}
public void get_nfc(View v){
isToScan = true;
}
@Override
protected void onResume() {
super.onResume();
if(nfcAdapter != null) {
if (nfcAdapter.isEnabled()) {
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
intentFiltersArray = new IntentFilter[] { ndef };
techList = new String[][] { new String[] { NfcA.class.getName(),
MifareUltralight.class.getName()} };
nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techList);
} else {
Toast.makeText(this,"NFC Disabled",Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onPause() {
if (nfcAdapter != null) {
nfcAdapter.disableForegroundDispatch(this);
}
super.onPause();
}
@Override
public void onNewIntent(Intent intent) {
if (isToScan && intent.hasExtra(NfcAdapter.EXTRA_TAG)) {
isToScan = false;
Parcelable tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
card_details.setText(dumpTagData(tag).toString());
Toast.makeText(this, "This is " + card_details.getText().toString(), 5000).show();
}
}
private String dumpTagData(Parcelable p) {
StringBuilder sb = new StringBuilder();
Tag tag = (Tag) p;
byte[] id = tag.getId();
sb.append(getHex(id));
return sb.toString();
}
private String getHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i] & 0xff;
if (b < 0x10)
sb.append('0');
sb.append(Integer.toHexString(b));
}
return sb.toString();
}
}
希望对您有所帮助。
我正在开发一个 Android 应用程序,我需要在其中单击按钮时扫描 NFC 标签。但是如果我点击一个按钮它没有检测到卡并且没有什么是happening.Help我来解决这个问题。
public class Check_in extends Activity {
EditText card_details;
PendingIntent pendingIntent;
IntentFilter[] intentFiltersArray;
String[][] techList;
NfcAdapter nfcAdapter;
LinearLayout scanContainer;
private boolean isScanEnabled = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check_in);
card_details = (EditText) findViewById(R.id.editText);
}
public void get_nfc(View v)
{
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if(nfcAdapter == null)
Toast.makeText(this,"This Device Not supporting NFC",Toast.LENGTH_SHORT).show();
else
handleIntent();
}
private void handleIntent() {
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
intentFiltersArray = new IntentFilter[] { ndef };
techList = new String[][] { new String[] { NfcA.class.getName(),
MifareUltralight.class.getName()} };
}
@Override
protected void onResume() {
super.onResume();
if(nfcAdapter !=null) {
if(nfcAdapter.isEnabled())
nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techList);
else
Toast.makeText(this,"NFC Disabled",Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onPause() {
if(nfcAdapter !=null)
nfcAdapter.disableForegroundDispatch(this);
super.onPause();
}
@Override
public void onNewIntent(Intent intent) {
if(!isScanEnabled)
return;
Parcelable tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
card_details.setText(dumpTagData(tag).toString());
Toast.makeText(this, "This is "+dumpTagData(tag).toString(), 5000).show();
}
private String dumpTagData(Parcelable p) {
StringBuilder sb = new StringBuilder();
Tag tag = (Tag) p;
byte[] id = tag.getId();
sb.append(getHex(id));
return sb.toString();
}
private String getHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i] & 0xff;
if (b < 0x10)
sb.append('0');
sb.append(Integer.toHexString(b));
}
return sb.toString();
}
}
在 onResume() 和 onPause() 方法中,nfcAdapter 始终为 null(因为您只能在单击按钮时获得 nfcAdapter)。为什么永远不会调用此 enableForegroundDispatch() 方法。
我对你的代码做了一些修改(但我还没有测试)。
public class Check_in extends Activity {
EditText card_details;
PendingIntent pendingIntent;
IntentFilter[] intentFiltersArray;
String[][] techList;
NfcAdapter nfcAdapter;
LinearLayout scanContainer;
private boolean isToScan = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check_in);
card_details = (EditText) findViewById(R.id.editText);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
}
public void get_nfc(View v){
isToScan = true;
}
@Override
protected void onResume() {
super.onResume();
if(nfcAdapter != null) {
if (nfcAdapter.isEnabled()) {
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
intentFiltersArray = new IntentFilter[] { ndef };
techList = new String[][] { new String[] { NfcA.class.getName(),
MifareUltralight.class.getName()} };
nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techList);
} else {
Toast.makeText(this,"NFC Disabled",Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onPause() {
if (nfcAdapter != null) {
nfcAdapter.disableForegroundDispatch(this);
}
super.onPause();
}
@Override
public void onNewIntent(Intent intent) {
if (isToScan && intent.hasExtra(NfcAdapter.EXTRA_TAG)) {
isToScan = false;
Parcelable tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
card_details.setText(dumpTagData(tag).toString());
Toast.makeText(this, "This is " + card_details.getText().toString(), 5000).show();
}
}
private String dumpTagData(Parcelable p) {
StringBuilder sb = new StringBuilder();
Tag tag = (Tag) p;
byte[] id = tag.getId();
sb.append(getHex(id));
return sb.toString();
}
private String getHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i] & 0xff;
if (b < 0x10)
sb.append('0');
sb.append(Integer.toHexString(b));
}
return sb.toString();
}
}
希望对您有所帮助。