get NFC URI Record - Android Network

Android examples for Network:NFC Record

Description

get NFC URI Record

Demo Code


//package com.java2s;

import java.util.Random;

import android.nfc.NdefRecord;

public class Main {
    private static NdefRecord getURIRecord(String uri) {
        NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
                NdefRecord.RTD_URI, getRandomIdBytes(), getURIPayLoad(uri));
        return record;
    }/*  w  w  w  . ja v a  2  s .c o  m*/

    private static byte[] getRandomIdBytes() {
        Random rand = new Random(System.currentTimeMillis());
        byte buf[] = new byte[4];
        rand.nextBytes(buf);
        return buf;
    }

    public static byte[] getURIPayLoad(String uriNoHttp) {
        byte rawBytes[] = uriNoHttp.getBytes();
        byte httpPrefixByte[] = new byte[] { (byte) 0x03 };
        byte uriBytes[] = new byte[rawBytes.length + 1];

        copySmallArraysToBigArray(
                new byte[][] { httpPrefixByte, rawBytes }, uriBytes);

        return uriBytes;
    }

    public static void copySmallArraysToBigArray(
            final byte[][] smallArrays, final byte[] bigArray) {
        int currentOffset = 0;
        for (final byte[] currentArray : smallArrays) {
            System.arraycopy(currentArray, 0, bigArray, currentOffset,
                    currentArray.length);
            currentOffset += currentArray.length;
        }
    }
}

Related Tutorials