revtel / react-native-nfc-manager

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

writing data for nfc tag to configure wifi on Tag #188

Closed deenMuhammad closed 3 years ago

deenMuhammad commented 5 years ago

I am trying to write wifi information in application/vnd.wfa.wsc format. how can I achieve this?

deenMuhammad commented 5 years ago

is there any utility to make payload wifi compatible?

deenMuhammad commented 5 years ago

I seem to have come up with a functionality that can enable the module to write wifi information to let the tag become connectable to wifi when tagged!

deenMuhammad commented 5 years ago
@ReactMethod
    public void requestNdefWifiWrite(ReadableArray rnArray, ReadableMap options, Callback callback) {
        // System.out.print(rnArray);
        synchronized(this) {
            if (!isForegroundEnabled) {
                callback.invoke("you should requestTagEvent first");
                return;
            }

            if (hasPendingRequest()) {
                callback.invoke("You can only issue one request at a time");
            } else {
                boolean format = options.getBoolean("format");
                boolean formatReadOnly = options.getBoolean("formatReadOnly");

                //try {
                    NdefMessage msgToWrite;

                    /// the only case we allow ndef message to be null is when formatting, see:
                    /// https://developer.android.com/reference/android/nfc/tech/NdefFormatable.html#format(android.nfc.NdefMessage)
                    /// this API allows the `firstMessage` to be null
                    if (format && rnArray == null) {
                        msgToWrite = null;
                    } else {
                        // String[] arr = new String(2);
                        // arr[0] = "123";
                        // arr[1] = "123";
                        String[] arr = {rnArray.getString(0),rnArray.getString(1)};
                        byte[] payload = generateNdefPayload(arr);
                        //byte[] bytes = rnArrayToBytes(rnArray);
                        //msgToWrite = new NdefMessage(bytes);
                        NdefRecord mimeRecord = new NdefRecord(
                        NdefRecord.TNF_MIME_MEDIA,
                        NFC_TOKEN_MIME_TYPE.getBytes(Charset.forName("US-ASCII")),
                        new byte[0],
                        payload);

                        msgToWrite = new NdefMessage(new NdefRecord[] {mimeRecord});
                    }

                    writeNdefRequest = new WriteNdefRequest(
                            msgToWrite,
                            callback, // defer the callback
                            format,
                            formatReadOnly
                    );
                //} catch (FormatException e) {
                //    callback.invoke("Incorrect ndef format");
                //}
            }
        }
    }

private static byte[] generateNdefPayload(String[] arr) {
        String ssid = arr[0]; // wifi ssid
        short ssidSize = (short) ssid.getBytes().length;

        short authType;
        authType = AUTH_TYPE_WPA2_PSK;

        String networkKey = arr[1]; // wifi password
        short networkKeySize = (short) networkKey.getBytes().length;

        byte[] macAddress = new byte[MAX_MAC_ADDRESS_SIZE_BYTES];
        for (int i = 0; i < MAX_MAC_ADDRESS_SIZE_BYTES; i++) {
            macAddress[i] = (byte) 0xff;
        }

        /* Fill buffer */

        int bufferSize = 18 + ssidSize + networkKeySize; // size of required credential attributes

        ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
        buffer.putShort(CREDENTIAL_FIELD_ID);
        buffer.putShort((short) (bufferSize - 4));

        buffer.putShort(SSID_FIELD_ID);
        buffer.putShort(ssidSize);
        buffer.put(ssid.getBytes());

        buffer.putShort(AUTH_TYPE_FIELD_ID);
        buffer.putShort((short) 2);
        buffer.putShort(authType);

        buffer.putShort(NETWORK_KEY_FIELD_ID);
        buffer.putShort(networkKeySize);
        buffer.put(networkKey.getBytes());

        return buffer.array();
    }
deenMuhammad commented 5 years ago
//new statics 
    public static final short CREDENTIAL_FIELD_ID = 0x100e;
    public static final short SSID_FIELD_ID = 0x1045;
    public static final short AUTH_TYPE_FIELD_ID = 0x1003;
    public static final short AUTH_TYPE_WPA2_PSK = 0x0020;
    public static final short NETWORK_KEY_FIELD_ID = 0x1027;
    public static final int MAX_SSID_SIZE_BYTES = 32;
    public static final int MAX_MAC_ADDRESS_SIZE_BYTES = 6;
    public static final int MAX_NETWORK_KEY_SIZE_BYTES = 64;
    private static final String NFC_TOKEN_MIME_TYPE = "application/vnd.wfa.wsc";
    //new end
deenMuhammad commented 5 years ago

Whenever you tag nfc your phone prompts message to connect to wifi. I have tested the new function and it works

whitedogg13 commented 5 years ago

hi @deenMuhammad sorry for the late resonse!

It will be an awesome utility if we have that! However, I think this kind of utility is better to be put in JS side, so both iOS and Android can benefit. If you're available, please feel free to submit that PR, and I will be very welcome to accept that!

deenMuhammad commented 5 years ago

i will try to do that! I have one more question! I am trying to put password authentication to my mifare Ultralight nfc tag, do you think that is possible using existing transceive method?

whitedogg13 commented 5 years ago

Yes, I think transceive method is low-level enough to achieve that. I’m

deenMuhammad commented 5 years ago

Yes, I think transceive method is low-level enough to achieve that. I’m I have done the password protection using the . transceive . function and it saved my life! But i had to alter the internal java function to achieve that, at first. but now i do think that it is even possible without altering the existing code. The only thing that i had to do is to understand how the nfc and Mifare Ultralight type of nfc memory structure is constructed!

deenMuhammad commented 5 years ago

I have used these resources:

  1. https://www.nxp.com/docs/en/data-sheet/NTAG213_215_216.pdf
  2. https://stackoverflow.com/questions/22719465/ntag212-mifare-ultralight-with-authentication
github-actions[bot] commented 3 years 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.

github-actions[bot] commented 3 years ago

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