revtel / react-native-nfc-manager

React Native NFC module for Android & iOS
MIT License
1.37k stars 317 forks source link

Ndef not writing the empty tag in android only #709

Open Nardeep opened 6 months ago

Nardeep commented 6 months ago

When tag is empty , then ndef.writeNdefMessage is not working its throwing error : [Error: unsupported tag api]

we are using below code: const writeDataToNfcTag = async (data) => { console.log('data-=-=-',data); try { await NfcManager.requestTechnology(NfcTech.Ndef, { alertMessage: 'Ready to write some NDEF', }); const bytes = Ndef.encodeMessage([Ndef.textRecord(data)]); console.log('bytes-=-=',bytes); if (bytes) { try{ await NfcManager.ndefHandler.writeNdefMessage(bytes);

            if (Platform.OS === 'ios') {
                await NfcManager?.setAlertMessageIOS('Successfully write NDEF');
            }
         }catch(err){
            console.log('writeError-=-',err);
         }

        }

    } catch (error) {
        console.error('Error writing data to NFC tag:', JSON.stringify(error));
    } finally {
        await NfcManager.cancelTechnologyRequest();
    }
}

After debugging the android code we found that when tag is empty then its not detecting  techRequest.
Ndef ndef = (Ndef)techRequest.getTechHandle();
ndef is coming null. So please check why its happening 
K-Leon commented 6 months ago

Same issue on our side. Did you find a solution / workaround?

github-actions[bot] commented 3 months ago

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.

Nardeep commented 2 months ago

@K-Leon Hi , we resolved this issue. The issue is in technology type we added the technology in requestTechnology and use the correct write function according to nfc technology. Here is the below code: let technology = await NfcManager.requestTechnology([NfcTech.NdefFormatable, NfcTech.Ndef],

let promise = (technology == "NdefFormatable" ? NfcManager.ndefFormatableHandlerAndroid.formatNdef(bytes) : NfcManager.ndefHandler.writeNdefMessage(bytes))