Java Code Examples for android.nfc.NdefRecord#TNF_MIME_MEDIA

The following examples show how to use android.nfc.NdefRecord#TNF_MIME_MEDIA . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BeamActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
private NdefMessage createMessage(String payload) {
  String mimeType = "application/com.professionalandroid.apps.nfcbeam";
  byte[] tagId = new byte[0];

  NdefMessage nfcMessage = new NdefMessage(new NdefRecord[] {
    // Create the NFC payload.
    new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
      mimeType.getBytes(Charset.forName("US-ASCII")),
      tagId,
      payload.getBytes(Charset.forName("US-ASCII"))),

    // Add the AAR (Android Application Record)
    NdefRecord.createApplicationRecord("com.professionalandroid.apps.nfcbeam")
  });
  return nfcMessage;
}
 
Example 2
Source File: NfcTypeConverter.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Converts android.nfc.NdefRecord to mojo NfcRecord
 */
private static NfcRecord toNfcRecord(NdefRecord ndefRecord)
        throws UnsupportedEncodingException {
    switch (ndefRecord.getTnf()) {
        case NdefRecord.TNF_EMPTY:
            return createEmptyRecord();
        case NdefRecord.TNF_MIME_MEDIA:
            return createMIMERecord(
                    new String(ndefRecord.getType(), "UTF-8"), ndefRecord.getPayload());
        case NdefRecord.TNF_ABSOLUTE_URI:
            return createURLRecord(ndefRecord.toUri());
        case NdefRecord.TNF_WELL_KNOWN:
            return createWellKnownRecord(ndefRecord);
    }
    return null;
}
 
Example 3
Source File: BeamActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@Override
protected void onResume() {
  super.onResume();

  // Listing 18-21: Creating an Android Beam NDEF message
  String payload = "Two to beam across";
  String mimeType = "application/com.professionalandroid.apps.nfcbeam";

  byte[] tagId = new byte[0];
  NdefMessage nfcMessage = new NdefMessage(new NdefRecord[] {
    // Create the NFC payload.
    new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
      mimeType.getBytes(Charset.forName("US-ASCII")),
      tagId,
      payload.getBytes(Charset.forName("US-ASCII"))),

    // Add the AAR (Android Application Record)
    NdefRecord.createApplicationRecord("com.professionalandroid.apps.nfcbeam")
  });

  // Set static beam message
  NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
  nfcAdapter.setNdefPushMessage(nfcMessage, this);

  // Set dynamic beam message
  setBeamMessage();
}
 
Example 4
Source File: NfcUtils.java    From WiFiKeyShare with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Generate an NDEF message containing the given Wi-Fi configuration
 *
 * @param wifiNetwork the Wi-Fi configuration to convert
 * @return an NDEF message containing the given Wi-Fi configuration
 */
public static NdefMessage generateNdefMessage(WifiNetwork wifiNetwork) {
    byte[] payload = generateNdefPayload(wifiNetwork);

    NdefRecord mimeRecord = new NdefRecord(
            NdefRecord.TNF_MIME_MEDIA,
            NfcUtils.NFC_TOKEN_MIME_TYPE.getBytes(Charset.forName("US-ASCII")),
            new byte[0],
            payload);
    NdefRecord aarRecord = NdefRecord.createApplicationRecord(PACKAGE_NAME);

    return new NdefMessage(new NdefRecord[] {mimeRecord, aarRecord});
}
 
Example 5
Source File: NfcHandler.java    From PowerSwitch_Android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Converts a Long into a NdefMessage in application/vnd.facebook.places MIMEtype.
 * <p/>
 * for writing Places
 */
public static NdefMessage getAsNdef(String content) {
    byte[] textBytes = content.getBytes();
    NdefRecord textRecord = new NdefRecord(
            NdefRecord.TNF_MIME_MEDIA,
            "application/eu.power_switch".getBytes(),
            new byte[]{},
            textBytes);
    return new NdefMessage(new NdefRecord[]{
            textRecord,
            NdefRecord.createApplicationRecord("eu.power_switch")});
}
 
Example 6
Source File: NfcMessageUtilityImpl.java    From android-nfc-lib with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 * Precondition : macAddress should not be null
 */
@Override
public NdefMessage createBluetoothAddress(@NotNull String macAddress) throws FormatException {
    byte[] payload = convertBluetoothToNdefFormat(macAddress);
    NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, NfcType.BLUETOOTH_AAR, null, payload);

    return new NdefMessage(record);
}
 
Example 7
Source File: ZannenNfcWriter.java    From effective_android_sample with Apache License 2.0 5 votes vote down vote up
private NdefMessage getNoteAsNdef() {
	String dummy = "dummy";
    byte[] textBytes = dummy.toString().getBytes();
    NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "text/plain".getBytes(),
            new byte[] {}, textBytes);
    return new NdefMessage(new NdefRecord[] {
        textRecord
    });
}
 
Example 8
Source File: MimeRecord.java    From effective_android_sample with Apache License 2.0 5 votes vote down vote up
@Override
public NdefRecord getNdefRecord() {
	if(!hasMimeType()) {
		throw new IllegalArgumentException("Expected content type");
	}

	// the android api normalizes the content type, I dont see why you would want that
	return new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeType.getBytes(Charset.forName("US-ASCII")), id != null ? id : EMPTY, data != null ? data : EMPTY);
}
 
Example 9
Source File: Record.java    From effective_android_sample with Apache License 2.0 4 votes vote down vote up
/**
    * Parse a byte-based {@link NdefRecord} into a high-level {@link Record}.
    * 
    * @param ndefRecord record to parse
    * @return corresponding {@link Record} subclass - {@link UnsupportedRecord} is not known.
    * @throws FormatException if known record type cannot be parsed
    */

public static Record parse(NdefRecord ndefRecord) throws FormatException {
	short tnf = ndefRecord.getTnf();
	
	Record record = null;
	switch (tnf) {
       case NdefRecord.TNF_EMPTY: {
       	record = EmptyRecord.parse(ndefRecord);
       	
       	break;
       }
       case NdefRecord.TNF_WELL_KNOWN: {
       	record = parseWellKnown(ndefRecord);
       	
       	break;
       }
       case NdefRecord.TNF_MIME_MEDIA: {
       	record = MimeRecord.parse(ndefRecord);
       	
       	break;
       }
       case NdefRecord.TNF_ABSOLUTE_URI: {
       	record = AbsoluteUriRecord.parse(ndefRecord);
       	
       	break;
       }
       case NdefRecord.TNF_EXTERNAL_TYPE: {
       	record = ExternalTypeRecord.parse(ndefRecord);

       	break;
       }
       case NdefRecord.TNF_UNKNOWN: {
       	record = UnknownRecord.parse(ndefRecord);
       	
       	break;
       }
       /*
       case NdefRecord.TNF_UNCHANGED: {
       	throw new IllegalArgumentException("Chunked records no supported"); // chunks are abstracted away by android so should never happen
       }
       */
       	
	}

	if(record == null) { // pass through
		record = UnsupportedRecord.parse(ndefRecord);
	}
	
	if(ndefRecord.getId().length > 0) {
		record.setId(ndefRecord.getId());
	}
	
	return record;
}