-4

I want to use nfc_manager package in flutter in the app I am getting contact in VCARD type from an api according to login user. what I want to achieve is when two phones near each other I want the contact to be added in the other phone contacts list how to do that? I know I have to write nfc VACRD like this:

 String vCardText = """
BEGIN:VCARD\r\n
VERSION:3.0\r\n
REV:2023-08-22T13:21:45Z\r\n
TEL;TYPE=WORK:1234567890\r\n
LABEL;CHARSET=UTF-8:mobile_number\r\n
END:VCARD\r\n
""";

  Future<void> startNFC() async {
    NfcManager.instance.startSession(
      onDiscovered: (NfcTag tag) async {
        await tag.writeNdef(
          NdefMessage.fromRecords([NdefRecord.createMime('text/vcard', vCardText)]),
        );
        await NfcManager.instance.stopSession();
      },
    );

and to read :

String receivedVCardText = "No vCard received yet";

  Future<void> startNFC() async {
    NfcManager.instance.startSession(
      onDiscovered: (NfcTag tag) async {
        NdefMessage? message = await tag.readNdef();
        if (message != null && message.records.isNotEmpty) {
          NdefRecord record = message.records[0];
          if (record.isMime && record.mimeType == 'text/vcard') {
            String vCardText = record.text!;
            setState(() {
              receivedVCardText = vCardText;
            });
          }
        }
        await NfcManager.instance.stopSession();
      },
    );
  }

so every time the user login I should write and then read, but how the contact will be added to the another phone contacts list. I mean when I read a vcard contact this contact can be added directly to phone contacts list ?

and how to make the read always works ? assume user didn't make a logout and still in app how to make read always work so whenever user turn nfc on it should read the contact?

Melissa
  • 13
  • 4
  • Not possible to share anything via NFC on iOS – Andrew Aug 22 '23 at 16:07
  • What about android? – Melissa Aug 22 '23 at 16:08
  • how to achieve it? – Melissa Aug 22 '23 at 16:08
  • Possible if one of the phones is Android as in Android you are allowed to emulate a NFC Tag. See https://github.com/underwindfall/NFCAndroid as an example. But this is low level stuff so it needs to be in Java/Kotlin – Andrew Aug 22 '23 at 16:12
  • my question is how to add contact directly using nfc_manager package in flutter l already did the functions to read and write , when I read, how to add the contact directly to another phone android – Melissa Aug 22 '23 at 16:14
  • So we go back to not possible with just nfc_manager package in flutter. – Andrew Aug 22 '23 at 16:15
  • I need to use java or Kotlin in flutter? – Melissa Aug 22 '23 at 16:17
  • Yes you would need to write some java/kotlin there is a library that attempts something like this for flutter https://github.com/zxxdanxxz/nfc_host_card_emulation (this wraps the kotlin for you but is setup for limited communication data). Not tried it. – Andrew Aug 22 '23 at 16:24

0 Answers0