NFC detection fails sometimes
rajeshkanna777 opened this issue · 1 comments
Prerequisites
- Put an X between the brackets on this line if you have done all of the following:
- My Android device/devices supports BLE and has Android OS version >= 4.4
- My Android device/devices have Bluetooth enabled
- My app has Location Permissions granted
Basic information
I have few NFC beacons. When i tap the beacon on to the backside of the phone, some times beacon gets detected and sometimes not. I tried the same in Estimote app also. Even in Estimote app, the Quick connect menu is not working sometimes.
**Expected behavior:**When i tap the NFC beacon i have to read the the Beacon identifier and mac address everytime,
Actual behavior: When i tap the beacon some times i am not able to read it via NFC (i.e Not detected).
Here my code
private NfcAdapter mNfcAdapter;
protected void onCreate(Bundle savedInstanceState) {
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
}
@Override
public void onResume() {
super.onResume();
try {
SystemRequirementsChecker.checkWithDefaultDialogs(this);
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
IntentFilter[] nfcIntentFilter = new IntentFilter[]{tagDetected, ndefDetected};
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
if (mNfcAdapter != null)
mNfcAdapter.enableForegroundDispatch(this, pendingIntent, nfcIntentFilter, null);
} catch (Exception e) {
Log.e(TAG, "Nfc adapter failed");
}
}
@Override
protected void onNewIntent(Intent intent) {
try {
if (intent.getAction() != null &&
intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs != null) {
for (Parcelable rawMsg : rawMsgs) {
NdefMessage msg = (NdefMessage) rawMsg;
NdefRecord[] records = msg.getRecords();
for (NdefRecord record : records) {
if (record.getTnf() == NdefRecord.TNF_EXTERNAL_TYPE) {
String type = new String(record.getType(), Charset.forName("ascii"));
if ("estimote.com:id".equals(type)) {
deviceId = DeviceId.fromBytes(record.getPayload());
} else if ("estimote.com:mac".equals(type)) {
byte[] revMac = new byte[record.getPayload().length];
for (int j = 0; j < revMac.length; j++) {
revMac[j] = record.getPayload()[revMac.length - j - 1];
}
macAddress = MacAddress.fromBytes(revMac);
}
}
}
}
} catch (Exception e) {
Log.e(TAG, "New intend failed");
}
}
@Override
public void onDestroy() {
super.onDestroy();
mNfcAdapter=null;
}
When beacons are not detected, i get rawMsgs as null in the following lines of code
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
The NFC antenna in Estimote Beacons isn't particularly strong, so it sometimes requires a few tries to align the antenna in the beacon with the antenna in the smartphone properly. Not much to do here on the software/SDK side, so I've closed this issue.
You can start a discussion on our forums, tell us how you want to use NFC (configuration/deployment? or in your app?), and we can try to help, maybe suggest some alternative solution, etc.