Example usage for android.nfc NdefRecord RTD_URI

List of usage examples for android.nfc NdefRecord RTD_URI

Introduction

In this page you can find the example usage for android.nfc NdefRecord RTD_URI.

Prototype

null RTD_URI

To view the source code for android.nfc NdefRecord RTD_URI.

Click Source Link

Document

RTD URI type.

Usage

From source file:de.berlin.magun.nfcmime.core.NdefMessageBuilder.java

/**
 * Stores an NDEF record containing a URI, using the URI format definition.
 * @param uri the URI to be written/*from   w ww.  j a  va 2 s  .  c  o  m*/
 * @throws IOException
 */
public void addUriRecord(String uri) throws IOException {
    if (!this.hasChecksum) {
        recordlist.add(new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0],
                ArrayUtils.addAll(new byte[] { (byte) 0x00 }, uri.getBytes(Charset.forName("UTF-8")))));
    } else {
        throw new IOException("Cannot add record - last record is a checksum.");
    }
}

From source file:de.berlin.magun.nfcmime.core.RfidDAO.java

/**
 * Reads URI records from NDEF records//from w  w w  . j  a  va 2 s .c  o m
 * @param tag
 * @return an ArrayList of URIs, represented as Strings.
 */
public ArrayList<String> getUriRecords(Tag tag) {
    readRecords(tag);
    if (this.records != null) {
        ArrayList<String> uriStrings = new ArrayList<String>();
        for (int i = 0; i < this.records.length; i++) {
            if (this.records[i].getTnf() == NdefRecord.TNF_WELL_KNOWN
                    && Arrays.equals(this.records[i].getType(), NdefRecord.RTD_URI)) {
                uriStrings.add(EncodingUtils.getString(ArrayUtils.remove(records[i].getPayload(), 0), "UTF-8"));
            }
        }
        return uriStrings;
    } else {
        return null;
    }
}

From source file:jp.tomorrowkey.android.felicalitewriter.ndef.UriNdefBuilder.java

/**
 * NDEF????/*ww  w. ja v a2s  . c om*/
 * 
 * @return
 */
public NdefMessage build() {
    try {
        int index = getProtocolIndex(mUriString);
        String protocol = sProtocolList.get(index);

        String uriBody = mUriString.replace(protocol, "");
        byte[] uriBodyBytes = uriBody.getBytes("UTF-8");

        ByteArrayBuffer buffer = new ByteArrayBuffer(1 + uriBody.length());
        buffer.append((byte) index);
        buffer.append(uriBodyBytes, 0, uriBodyBytes.length);

        byte[] payload = buffer.toByteArray();
        NdefMessage message = new NdefMessage(new NdefRecord[] {
                new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], payload) });

        return message;
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }

}

From source file:com.google.samples.apps.iosched.ui.NfcBadgeActivity.java

private void readTag(Tag t) {
    byte[] id = t.getId();

    // get NDEF tag details
    Ndef ndefTag = Ndef.get(t);//from w  w  w.ja  v a2s .c  o  m

    // get NDEF message details
    NdefMessage ndefMesg = ndefTag.getCachedNdefMessage();
    if (ndefMesg == null) {
        return;
    }
    NdefRecord[] ndefRecords = ndefMesg.getRecords();
    if (ndefRecords == null) {
        return;
    }
    for (NdefRecord record : ndefRecords) {
        short tnf = record.getTnf();
        String type = new String(record.getType());
        if (tnf == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(type.getBytes(), NdefRecord.RTD_URI)) {
            String url = new String(record.getPayload());
            recordBadge(url);
        }
    }
}

From source file:mobisocial.nfcserver.DesktopNfcServer.java

private static NdefMessage getHandoverNdef(String ref) {
    NdefRecord[] records = new NdefRecord[3];

    /* Handover Request */
    byte[] version = new byte[] { (0x1 << 4) | (0x2) };
    records[0] = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_HANDOVER_REQUEST, new byte[0],
            version);/* w w w . j av a2s .  c o m*/

    /* Collision Resolution */
    records[1] = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, new byte[] { 0x63, 0x72 }, new byte[0],
            new byte[] { 0x0, 0x0 });

    /* Handover record */
    byte[] payload = new byte[ref.length() + 1];
    System.arraycopy(ref.getBytes(), 0, payload, 1, ref.length());
    records[2] = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], payload);

    return new NdefMessage(records);
}

From source file:jp.co.brilliantservice.android.writertduri.HomeActivity.java

private NdefMessage createUriMessage(String uri) {
    try {/*from  ww w .  j  av  a2  s  .  co  m*/
        int index = getProtocolIndex(uri);
        String protocol = sProtocolList.get(index);

        String uriBody = uri.replace(protocol, "");
        byte[] uriBodyBytes = uriBody.getBytes("UTF-8");

        ByteArrayBuffer buffer = new ByteArrayBuffer(1 + uriBody.length());
        buffer.append((byte) index);
        buffer.append(uriBodyBytes, 0, uriBodyBytes.length);

        byte[] payload = buffer.toByteArray();
        NdefMessage message = new NdefMessage(new NdefRecord[] {
                new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], payload) });

        return message;
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.hybris.mobile.activity.AbstractProductDetailActivity.java

/**
 * Handle incoming intents. Do not load a product if we already have one.
 *///from w w w.j ava2 s  . c  o  m
@Override
protected void onResume() {
    super.onResume();

    String[] options = { InternalConstants.PRODUCT_OPTION_BASIC, InternalConstants.PRODUCT_OPTION_CATEGORIES,
            InternalConstants.PRODUCT_OPTION_CLASSIFICATION, InternalConstants.PRODUCT_OPTION_DESCRIPTION,
            InternalConstants.PRODUCT_OPTION_GALLERY, InternalConstants.PRODUCT_OPTION_PRICE,
            InternalConstants.PRODUCT_OPTION_PROMOTIONS, InternalConstants.PRODUCT_OPTION_REVIEW,
            InternalConstants.PRODUCT_OPTION_STOCK, InternalConstants.PRODUCT_OPTION_VARIANT };

    String productCode = null;

    Intent intent = getIntent();

    // Direct Call
    if (intent.hasExtra(DataConstants.PRODUCT_CODE)) //direct call from search list for example
    {
        productCode = intent.getStringExtra(DataConstants.PRODUCT_CODE);
    }
    // NFC Call
    else if (intent.hasExtra(NfcAdapter.EXTRA_TAG)) //NFC 
    {
        Tag tag = getIntent().getExtras().getParcelable(NfcAdapter.EXTRA_TAG);

        Ndef ndef = Ndef.get(tag);
        NdefMessage message = ndef.getCachedNdefMessage();

        NdefRecord record = message.getRecords()[0];
        if (record.getTnf() == NdefRecord.TNF_WELL_KNOWN
                && Arrays.equals(record.getType(), NdefRecord.RTD_URI)) {
            productCode = RegexUtil
                    .getProductCode(new String(record.getPayload(), 1, record.getPayload().length - 1));
        }
    }
    // Call from another application (QR Code) 
    else if (StringUtils.equals(intent.getAction(), Intent.ACTION_VIEW)) {
        productCode = RegexUtil.getProductCode(intent.getDataString());
    }

    if (StringUtils.isNotEmpty(productCode)) {
        this.enableAndroidBeam(productCode);
    }

    // Only load if we don't have a product already
    if (mProduct == null) {
        populateProduct(productCode, options);
    }

    invalidateOptionsMenu();
}

From source file:mobisocial.musubi.ui.FeedListActivity.java

public void writeGroupToTag(Uri uri) {
    NdefRecord urlRecord = new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, NdefRecord.RTD_URI, new byte[] {},
            uri.toString().getBytes());/*from   w ww.j  av  a2 s.  c om*/
    NdefMessage ndef = new NdefMessage(new NdefRecord[] { urlRecord });
    mNfc.enableTagWriteMode(ndef);
    Toast.makeText(this, "Touch a tag to write the group...", Toast.LENGTH_SHORT).show();
}

From source file:edu.cmu.mpcs.dashboard.TagViewer.java

void buildTagViews(NdefMessage[] msgs) {
    if (msgs == null || msgs.length == 0) {
        return;/*from ww  w  .  ja  v a  2s.com*/
    }
    LayoutInflater inflater = LayoutInflater.from(this);
    // LinearLayout content = mTagContent;
    // Clear out any old views in the content area, for example if
    // you scan
    // two tags in a row.
    // content.removeAllViews();
    // Parse the first message in the list
    // Build views for all of the sub records
    Log.i("TEST", "test");
    List<ParsedNdefRecord> records = NdefMessageParser.parse(msgs[0]);
    NdefRecord[] ndefRecords = msgs[0].getRecords();
    short tnf = ndefRecords[0].getTnf();

    Log.d("RECORD", "tnf is : " + Short.toString(tnf));

    byte[] type = ndefRecords[0].getType();
    byte[] standard = NdefRecord.RTD_TEXT;

    System.out.println("printing what we got in the message");
    for (byte theByte : type) {
        System.out.println(Integer.toHexString(theByte));
        Log.d("RECORD", Integer.toHexString(theByte));
    }

    System.out.println("printing what the actual val is ");
    for (byte theByte : standard) {
        System.out.println(Integer.toHexString(theByte));
        Log.d("RECORD", Integer.toHexString(theByte));
    }
    if (ndefRecords[0].getType().equals(standard))
        Log.d("RECORD", " type is : RTD_TXT");
    else if (ndefRecords[0].getType().equals(NdefRecord.RTD_URI))
        Log.d("RECORD", " type is : RTD_URI");
    else
        Log.d("RECORD", " type is : none of the 2");
    final int size = records.size();
    for (int i = 0; i < size; i++) {
        ParsedNdefRecord record = records.get(i);
        Log.i("RECORD", record.toString());
        String text;
        if (record instanceof TextRecord) {
            TextRecord t = (TextRecord) record;
            text = t.getText();
        } else {
            UriRecord t = (UriRecord) record;
            text = t.getUri().toString();
        }
        Log.d("WIFI", text);

        /*
         * At this point we have obtained the name of the profile . We
         * should not append .txt and see if the file exists in the
         * /mnt/Profile folder.
         * 
         * If it does exist, then we read the file, and apply various
         * settings
         * 
         * If it does note exists, then we show an error message indicating
         * that the user does not have a profile corresponding to this TAG.
         * Alternatively: We can take him to the create TAG activity where
         * he can create a new TAG with this name.
         */

        String contents[] = text.split("#");
        String filename = null;
        if (contents.length > 1) {

            String readHash = contents[0];
            filename = contents[1];

            Log.d("TAG_VIEWER", "readHash: " + readHash);
            Log.d("TAG_VIEWER", "filename: " + filename);

            if (readHash.equals(Utility.hashOfId)) {
                Log.d("TAG_VIEWER", "Hash Valid");

                File root = Environment.getExternalStorageDirectory();
                String path = root + "/Profiles/" + Utility.userUID + "/";
                String filePath = path + filename + ".txt";
                String settingString = "";
                boolean exists = (new File(path).exists());

                if (exists) {
                    Log.d("TAG_VIEWER", "Found file:" + filePath);
                    /*
                     * Parse the contents of the file, and apply settings
                     */
                    try {

                        FileReader logReader = new FileReader(filePath);
                        BufferedReader in = new BufferedReader(logReader);
                        try {
                            settingString = in.readLine();

                            Log.d("TAG_VIEWER", "In " + filePath + " settingString: " + settingString);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            Log.d("TAG_VIEWER", " read(buf) exception");
                            e.printStackTrace();
                        }
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        Log.d("TAG_VIEWER", "FileReader exception");
                        e.printStackTrace();
                    }

                    /*
                     * One call for each setting that we support
                     */
                    StringBuilder returnVal = new StringBuilder();

                    returnVal.append(wifiSetting(settingString));

                    returnVal.append(bluetoothSetting(settingString));

                    returnVal.append(ringerSetting(settingString));

                    launchmusicSetting(settingString);

                    returnVal.append(alarmSetting(settingString));

                    fbSetting(settingString);

                    Log.d("TAG_VIEWER", "the toast should be : " + returnVal.toString());

                    Context context = getApplicationContext();
                    CharSequence toastMessage = returnVal;
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, toastMessage, duration);
                    toast.show();

                } else {
                    /*
                     * Give the user the option of creating a new tag or
                     * ignoring this tag
                     */
                }

            } else {
                Log.d("TAG_VIEWER", "Hash InValid");
                int duration = Toast.LENGTH_LONG;
                CharSequence msg = "Authentication failure.This tag does not belong to you";
                Toast toast = Toast.makeText(TagViewer.this, msg, duration);
                toast.show();
                finish();
                // return;
            }

        } else {
            // public tag
            Log.d("PUBLIC_TAG", "PUBLIC_TAG");
        }

        // For Vibrate mode
        //

        /** Sending an SMS to a particular number **/

        // Intent intent = new
        // Intent(Intent.ACTION_VIEW,
        // Uri.parse("sms:" + "5189515772"));
        // intent.putExtra("sms_body",
        // "Hi This is a test message");
        // startActivity(intent);

        /** Toggle Airplane mode **/

        // Context context = getApplicationContext();
        // boolean isEnabled =
        // Settings.System.getInt(this.getApplicationContext().getContentResolver(),
        // Settings.System.AIRPLANE_MODE_ON, 0) == 1;
        // Settings.System.putInt(context.getContentResolver(),Settings.System.AIRPLANE_MODE_ON,isEnabled
        // ? 0 : 1);
        // Intent intent = new
        // Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        // intent.putExtra("state", !isEnabled);
        // sendBroadcast(intent);

        /** Launching the browser **/

        // String url = "http://www.google.com";
        // Intent intent = new
        // Intent(Intent.ACTION_VIEW);
        // intent.setData(Uri.parse(url));
        // startActivity(intent);

        /** Check into Facebook **/

        // Bundle params = new Bundle();
        //
        // //String access_token =
        // AndroidDashboardActivity.mPrefs.getString("access_token",
        // null);
        // //params.putString("access_token", access_token);
        // params.putString("place", "203682879660695"); // YOUR PLACE
        // ID
        // params.putString("Message","I m here in this place");
        // JSONObject coordinates = new JSONObject();
        // try
        // {
        // coordinates.put("latitude", 40.756);
        // coordinates.put("longitude", -73.987);
        // }
        // catch (JSONException e)
        // {
        // // TODO Auto-generated catch block
        // e.printStackTrace();
        // }
        //
        // params.putString("coordinates",coordinates.toString());
        // JSONArray frnd_data=new JSONArray();
        // params.putString("tags", "waves.mpcs@gmail.com");//where xx
        // indicates the User Id
        // String response;
        // try
        // {
        // response = facebook.request("me/checkins", params, "POST");
        // Log.d("Response",response);
        // }
        // catch (FileNotFoundException e)
        // {
        // // TODO Auto-generated catch block
        // e.printStackTrace();
        // }
        // catch (MalformedURLException e)
        // {
        // // TODO Auto-generated catch block
        // e.printStackTrace();
        // }
        // catch (IOException e)
        // {
        // // TODO Auto-generated catch block
        // e.printStackTrace();
        // }
        // //Log.d("Response",response);

        /*
         * TODO: Hashing TODO: Facebook Auth TODO: File Storage (Profile
         * Storage) TODO: Sample Profiles TODO: Social Network Check in
         * TODO: Alarms TODO: Airplane Mode (Enabling NFC while switching to
         * Airplane mode) TODO: NFC Launcher List (Market App) TODO: UI
         */

        // content.addView(record.getView(this, inflater, content, i));
        // inflater.inflate(R.layout.tag_divider, content, true);
    }

}