Java Code Examples for android.nfc.NdefRecord#createApplicationRecord()

The following examples show how to use android.nfc.NdefRecord#createApplicationRecord() . 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: 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 3
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 4
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 5
Source File: ActivitySendIdentityByNfc.java    From ploggy with GNU General Public License v3.0 5 votes vote down vote up
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
    try {
        String payload = Json.toJson(Data.getInstance().getSelf().mPublicIdentity);
        return new NdefMessage(
                new NdefRecord[] {
                        NdefRecord.createMime(NFC_MIME_TYPE, payload.getBytes()),
                        NdefRecord.createApplicationRecord(NFC_AAR_PACKAGE_NAME) });
    } catch (Utils.ApplicationError e) {
        Log.addEntry(LOG_TAG, "failed to create outbound NFC message");
    }
    return null;
}
 
Example 6
Source File: RunAppActivity.java    From android-nfc with MIT License 4 votes vote down vote up
/**
 * 往标签写数据的方法
 *
 * @param tag
 */
public void writeNFCTag(Tag tag) {
    if (tag == null) {
        return;
    }
    NdefMessage ndefMessage = new NdefMessage(new NdefRecord[]{NdefRecord
            .createApplicationRecord(mPackageName)});
    //转换成字节获得大小
    int size = ndefMessage.toByteArray().length;
    try {
        //2.判断NFC标签的数据类型(通过Ndef.get方法)
        Ndef ndef = Ndef.get(tag);
        //判断是否为NDEF标签
        if (ndef != null) {
            ndef.connect();
            //判断是否支持可写
            if (!ndef.isWritable()) {
                return;
            }
            //判断标签的容量是否够用
            if (ndef.getMaxSize() < size) {
                return;
            }
            //3.写入数据
            ndef.writeNdefMessage(ndefMessage);
            Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
        } else { //当我们买回来的NFC标签是没有格式化的,或者没有分区的执行此步
            //Ndef格式类
            NdefFormatable format = NdefFormatable.get(tag);
            //判断是否获得了NdefFormatable对象,有一些标签是只读的或者不允许格式化的
            if (format != null) {
                //连接
                format.connect();
                //格式化并将信息写入标签
                format.format(ndefMessage);
                Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show();
            }
        }
    } catch (Exception e) {
    }
}
 
Example 7
Source File: AddFriendQRActivity.java    From NaviBee with GNU General Public License v3.0 3 votes vote down vote up
public NdefRecord[] createRecords() {

        NdefRecord[] records = new NdefRecord[2];

        byte[] payload = (ADD_FRIEND_URL + uid).getBytes(Charset.forName("UTF-8"));

        records[0] = NdefRecord.createMime("application/navibee", payload);

        // Tell OS that which application handles this record
        records[1] =
                NdefRecord.createApplicationRecord(getPackageName());


        return records;
    }