示例#1
0
  @Override
  public NdefMessage createNdefMessage(NfcEvent event) {
    Log.d("ANDROID_LAB", "createNdefMessage thread.name=" + Thread.currentThread().getName());
    StringBuilder sb = new StringBuilder();
    sb.append(Build.MODEL + "("); // 手机型号
    sb.append(Build.VERSION.SDK_INT + ","); // SDK版本号
    sb.append(Build.VERSION.RELEASE + ")"); // Firmware/OS 版本号

    sb.append("发送来一条NFC消息, 时间是");
    Time time = new Time();
    time.setToNow();
    sb.append(time.format("%H:%M:%S"));

    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.and);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] bmpData = baos.toByteArray();

    NdefRecord[] records = new NdefRecord[2];
    records[0] =
        NdefRecord.createMime("application/lab.sodino.nfc", sb.toString().getBytes()); // 文本消息
    records[1] = NdefRecord.createMime("application/lab.sodino.nfc", bmpData); // 图片消息
    //		records[2] = NdefRecord.createApplicationRecord("com.tencent.mobileqq");

    NdefMessage msg = new NdefMessage(records);

    return msg;
  }
示例#2
0
  /**
   * Creates records to send to another device if detected those records are about to send to
   * another device
   *
   * @return NdefRecord[] - array of records to attach
   */
  public NdefRecord[] createRecords() {
    NdefRecord[] records = new NdefRecord[messagesToSendQueue.size()];

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
      for (int counter = 0; counter < messagesToSendQueue.size(); counter++) {
        byte[] payload = messagesToSendQueue.get(counter).getBytes(Charset.forName("UTF-8"));

        NdefRecord record =
            new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload);

        records[counter] = record;
      }
    } else {
      for (int counter = 0; counter < messagesToSendQueue.size(); counter++) {
        byte[] payload = messagesToSendQueue.get(counter).getBytes(Charset.forName("UTF-8"));
        NdefRecord record = NdefRecord.createMime("text/plain", payload);
        records[counter] = record;
      }
    }

    records[messagesToSendQueue.size()] = NdefRecord.createApplicationRecord(getPackageName());
    return records;
  }