react-native-nfc-manager icon indicating copy to clipboard operation
react-native-nfc-manager copied to clipboard

requestTechnology Ndef doesn't ever get resolved

Open antekdev opened this issue 7 years ago • 5 comments

Greetings, there is an issue concerning requestTechnology method. It does neither get resolved nor rejected forever. Omitting it is not permittable, there is an exception 'no tech request available' raised in that case. Library version used is 2.0.1. RN 0.61.1. Following snippet is a part of my NfcAndroid service class:

writeNfcTag = async () => {
    try {
      let resp = await NfcManager.requestTechnology(NfcTech.Ndef);
      // isn't ever reached
      let ndef = await NfcManager.getNdefMessage();
      let payload = buildTextPayload(this.textToSend);
      let bytes = Ndef.encodeMessage([buildRecordFromText(payload)]);
      await NfcManager.writeNdefMessage(bytes);
      console.log('!SUCCESSFULLY WROTE NFC TEXT');
    } catch (ex) {
      console.warn('!EXCEPTION', ex);
    }
  }

After going through the library, this is the calls track scheme:

import { NativeModules } from 'react-native'
const NativeNfcManager = NativeModules.NfcManager 
// according to console.log, NfcManager object exists, 
// having { ... "requestTechnology": [Function fn] ... } as its part, 
// BUT the same object is absent when searching through the text of files manually

NfcManager.requestTechnology() -> 
NfcManager._requestTechnology() -> 
NativeNfcManager.requestTechnology(['Ndef'], callback)

And the latter has the body only accessible through console, using function.toString:

function fn() {
  for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
    args[_key2] = arguments[_key2];
  }

  var lastArg = args.length > 0 ? args[args.length - 1] : null;
  var secondLastArg = args.length > 1 ? args[args.length - 2] : null;
  var hasSuccessCallback = typeof lastArg === 'function';
  var hasErrorCallback = typeof secondLastArg === 'function';
  hasErrorCallback && invariant(hasSuccessCallback, 'Cannot have a non-function arg after a function arg.');
  var onSuccess = hasSuccessCallback ? lastArg : null;
  var onFail = hasErrorCallback ? secondLastArg : null;
  var callbackCount = hasSuccessCallback + hasErrorCallback;
  args = args.slice(0, args.length - callbackCount);

  if (type === 'sync') {
    return BatchedBridge.callNativeSyncHook(moduleID, methodID, args, onFail, onSuccess);
  } else {
    BatchedBridge.enqueueNativeCall(moduleID, methodID, args, onFail, onSuccess);
  }
}

I cannot debug the last function, since it is inaccessible for me by hand, as it is absent in file system. I wonder how nobody has mentioned this issue yet, I suspect the solution to be easier than I think now.

antekdev avatar Nov 14 '19 14:11 antekdev

Hi @AnthonyGolo sorry for the late response. The promise you mentioned only get resolved when a tag is detected, or via an explicitly cancelTechnology call. Once it's resolved, you have an active session connects to the tag and so you can do further actions like NDEF writing.

whitedogg13 avatar Nov 19 '19 14:11 whitedogg13

Every time i turn on the app and try to use the nfc with 2 different phone the write method it always stops at the first try{requestTecnology(bla,bla,bla)} so i cant send anything and when i try to read i recieve a default tag of android who send me a link to a play store page @whitedogg13

LordHydra1 avatar Jan 16 '20 13:01 LordHydra1

"The promise you mentioned only get resolved when a tag is detected" Do you mean on NfcEvents.DiscoverTag event, it get resolved? In my case, when requestTechnology is not resolved, this event is not fired.

taibui-vn avatar May 06 '20 04:05 taibui-vn

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar May 11 '21 02:05 github-actions[bot]

This issue was closed because it has been stalled for 5 days with no activity.

github-actions[bot] avatar May 26 '21 03:05 github-actions[bot]