本文整理匯總了Java中android.nfc.NdefMessage.getRecords方法的典型用法代碼示例。如果您正苦於以下問題:Java NdefMessage.getRecords方法的具體用法?Java NdefMessage.getRecords怎麽用?Java NdefMessage.getRecords使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.nfc.NdefMessage
的用法示例。
在下文中一共展示了NdefMessage.getRecords方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: tryUpdateIntentFromBeam
import android.nfc.NdefMessage; //導入方法依賴的package包/類
/**
* Checks to see if the activity's intent ({@link android.app.Activity#getIntent()}) is
* an NFC intent that the app recognizes. If it is, then parse the NFC message and set the
* activity's intent (using {@link Activity#setIntent(android.content.Intent)}) to something
* the app can recognize (i.e. a normal {@link Intent#ACTION_VIEW} intent).
*/
public static void tryUpdateIntentFromBeam(Activity activity) {
Intent originalIntent = activity.getIntent();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(originalIntent.getAction())) {
Parcelable[] rawMsgs = originalIntent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage msg = (NdefMessage) rawMsgs[0];
// Record 0 contains the MIME type, record 1 is the AAR, if present.
// In iosched, AARs are not present.
NdefRecord mimeRecord = msg.getRecords()[0];
if (ScheduleContract.makeContentItemType(
ScheduleContract.Sessions.CONTENT_TYPE_ID).equals(
new String(mimeRecord.getType()))) {
// Re-set the activity's intent to one that represents session details.
Intent sessionDetailIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(new String(mimeRecord.getPayload())));
activity.setIntent(sessionDetailIntent);
}
}
}
示例2: extractMimePayload
import android.nfc.NdefMessage; //導入方法依賴的package包/類
@Nullable
public static byte[] extractMimePayload(final String mimeType, final NdefMessage message) {
final byte[] mimeBytes = mimeType.getBytes(Charsets.US_ASCII);
for (final NdefRecord record : message.getRecords()) {
if (record.getTnf() == NdefRecord.TNF_MIME_MEDIA && Arrays.equals(record.getType(), mimeBytes))
return record.getPayload();
}
return null;
}
示例3: parse
import android.nfc.NdefMessage; //導入方法依賴的package包/類
/**
* Parse an NDEF message and return the corresponding Wi-Fi configuration
*
* Source: http://androidxref.com/6.0.1_r10/xref/packages/apps/Nfc/src/com/android/nfc/NfcWifiProtectedSetup.java
*
* @param message the NDEF message to parse
* @return a WifiConfiguration extracted from the NDEF message
*/
private static WifiConfiguration parse(NdefMessage message) {
NdefRecord[] records = message.getRecords();
for (NdefRecord record : records) {
if (new String(record.getType()).equals(NFC_TOKEN_MIME_TYPE)) {
ByteBuffer payload = ByteBuffer.wrap(record.getPayload());
while (payload.hasRemaining()) {
short fieldId = payload.getShort();
short fieldSize = payload.getShort();
if (fieldId == CREDENTIAL_FIELD_ID) {
return parseCredential(payload, fieldSize);
} else {
payload.position(payload.position() + fieldSize);
}
}
}
}
return null;
}
示例4: parseNdefMessage
import android.nfc.NdefMessage; //導入方法依賴的package包/類
private void parseNdefMessage(final Intent intent) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
Log.d(TAG, "#msgs: " + rawMsgs.length);
for (Parcelable rawMsg : rawMsgs) {
NdefMessage msg = (NdefMessage) rawMsg;
Log.d(TAG, "#records: " + msg.getRecords().length);
for (NdefRecord record : msg.getRecords()) {
try {
mStream = new Stream(record);
return;
} catch (Exception e) {
Log.e(TAG, "NDEF record is not a stream", e);
}
}
}
throw new IllegalArgumentException("Invalid NDEF message");
}
示例5: parseNdefFoundResponse
import android.nfc.NdefMessage; //導入方法依賴的package包/類
private static String parseNdefFoundResponse(Context ctx, NdefFoundResponse resp) {
NdefMessage msg = resp.getMessage();
NdefRecord[] records = msg.getRecords();
if(records.length == 0) {
return ctx.getString(R.string.ndef_no_record);
}
else if (records.length == 1) {
return String.format(ctx.getString(R.string.ndef_found_response_single_record),
ByteUtils.bytesToHex(resp.getTagCode()),
parseTagType(ctx, resp.getTagType()),
parseNdefRecord(ctx, records[0]));
}
else {
return String.format(ctx.getString(R.string.ndef_found_response_multi_record),
ByteUtils.bytesToHex(resp.getTagCode()),
parseTagType(ctx, resp.getTagType()),
parseNdefRecord(ctx, records[0]));
}
}
示例6: nfcRead
import android.nfc.NdefMessage; //導入方法依賴的package包/類
public String nfcRead(Tag t)
{
Tag tag = t;
Ndef ndef = Ndef.get(tag);
if (ndef == null) {
return null;
}
NdefMessage ndefMessage = ndef.getCachedNdefMessage();
NdefRecord[] records = ndefMessage.getRecords();
for (NdefRecord ndefRecord : records)
{
if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT))
{
try {return readText(ndefRecord);} catch (UnsupportedEncodingException e) {}
}
}
return null;
}
示例7: doInBackground
import android.nfc.NdefMessage; //導入方法依賴的package包/類
@Override
protected NFCTag doInBackground(Intent... params) {
Intent intent = params[0];
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Ndef ndef = Ndef.get(tag);
if (ndef == null) {
return null;
}
NdefMessage ndefMessage = ndef.getCachedNdefMessage();
if(ndefMessage != null) {
NdefRecord[] records = ndefMessage.getRecords();
for (NdefRecord ndefRecord : records) {
try {
return new NFCTag(ndefRecord).decode();
} catch (UnsupportedEncodingException e) {
Log.e("NFC", "Unsupported Encoding", e);
}
}
}
return null;
}
示例8: getTagText
import android.nfc.NdefMessage; //導入方法依賴的package包/類
public String getTagText() throws Exception
{
if (tag == null)
{
throw new Exception("Please call handle new read as tag is null");
}
Ndef ndef = Ndef.get(tag);
NdefMessage ndefMessage = ndef.getCachedNdefMessage();
NdefRecord[] records = ndefMessage.getRecords();
for (NdefRecord ndefRecord : records)
{
if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT))
{
try
{
return readText(ndefRecord);
}
catch (UnsupportedEncodingException e)
{
Toast.makeText(activity, "Unsupported encoding", Toast.LENGTH_SHORT).show();
}
}
}
return null;
}
示例9: doInBackground
import android.nfc.NdefMessage; //導入方法依賴的package包/類
@Override
protected ArrayList<String> doInBackground(Tag... params) {
Tag tag = params[0];
Ndef ndef = Ndef.get(tag);
if (ndef == null) {
// NDEF is not supported by this Tag.
return null;
}
ArrayList<String> results = new ArrayList<>();
NdefMessage ndefMessage = ndef.getCachedNdefMessage();
NdefRecord[] records = ndefMessage.getRecords();
for (NdefRecord ndefRecord : records) {
if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
try {
results.add(readText(ndefRecord));
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Unsupported Encoding", e);
}
}
}
return results;
}
示例10: toNfcMessage
import android.nfc.NdefMessage; //導入方法依賴的package包/類
/**
* Converts android.nfc.NdefMessage to mojo NfcMessage
*/
public static NfcMessage toNfcMessage(NdefMessage ndefMessage)
throws UnsupportedEncodingException {
NdefRecord[] ndefRecords = ndefMessage.getRecords();
NfcMessage nfcMessage = new NfcMessage();
List<NfcRecord> nfcRecords = new ArrayList<NfcRecord>();
for (int i = 0; i < ndefRecords.length; i++) {
if ((ndefRecords[i].getTnf() == NdefRecord.TNF_EXTERNAL_TYPE)
&& (Arrays.equals(ndefRecords[i].getType(), WEBNFC_URN.getBytes("UTF-8")))) {
nfcMessage.url = new String(ndefRecords[i].getPayload(), "UTF-8");
continue;
}
NfcRecord nfcRecord = toNfcRecord(ndefRecords[i]);
if (nfcRecord != null) nfcRecords.add(nfcRecord);
}
nfcMessage.data = new NfcRecord[nfcRecords.size()];
nfcRecords.toArray(nfcMessage.data);
return nfcMessage;
}
示例11: readTag
import android.nfc.NdefMessage; //導入方法依賴的package包/類
private void readTag(Tag t) {
byte[] id = t.getId();
// get NDEF tag details
Ndef ndefTag = Ndef.get(t);
// 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);
}
}
}
示例12: tryUpdateIntentFromBeam
import android.nfc.NdefMessage; //導入方法依賴的package包/類
/**
* Checks to see if the activity's intent ({@link android.app.Activity#getIntent()}) is
* an NFC intent that the app recognizes. If it is, then parse the NFC message and set the
* activity's intent (using {@link Activity#setIntent(android.content.Intent)}) to something
* the app can recognize (i.e. a normal {@link Intent#ACTION_VIEW} intent).
*/
public static void tryUpdateIntentFromBeam(Activity activity) {
Intent originalIntent = activity.getIntent();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(originalIntent.getAction())) {
Parcelable[] rawMsgs = originalIntent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage msg = (NdefMessage) rawMsgs[0];
// Record 0 contains the MIME type, record 1 is the AAR, if present.
// In iosched, AARs are not present.
NdefRecord mimeRecord = msg.getRecords()[0];
if (ScheduleContract.Sessions.CONTENT_ITEM_TYPE.equals(
new String(mimeRecord.getType()))) {
// Re-set the activity's intent to one that represents session details.
Intent sessionDetailIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(new String(mimeRecord.getPayload())));
activity.setIntent(sessionDetailIntent);
}
}
}
示例13: doInBackground
import android.nfc.NdefMessage; //導入方法依賴的package包/類
@Override
protected String doInBackground(Tag... params) {
Tag tag = params[0];
Ndef ndef = Ndef.get(tag);
if (ndef == null) {
// NDEF is not supported by this Tag.
return null;
}
NdefMessage ndefMessage = ndef.getCachedNdefMessage();
NdefRecord[] records = ndefMessage.getRecords();
for (NdefRecord ndefRecord : records) {
if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
try {
return readText(ndefRecord);
} catch (UnsupportedEncodingException e) {
//Log.e(TAG, "Unsupported Encoding", e);
}
}
}
return null;
}
示例14: doInBackground
import android.nfc.NdefMessage; //導入方法依賴的package包/類
@Override
protected String doInBackground(Tag... params) {
Tag tag = params[0];
Ndef ndef = Ndef.get(tag);
if (ndef == null) {
// NDEF is not supported by this Tag.
return null;
}
NdefMessage ndefMessage = ndef.getCachedNdefMessage();
NdefRecord[] records = ndefMessage.getRecords();
for (NdefRecord ndefRecord : records) {
if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
try {
return readText(ndefRecord);
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Unsupported Encoding", e);
}
}
}
return null;
}
示例15: getBeamedLyrics
import android.nfc.NdefMessage; //導入方法依賴的package包/類
@TargetApi(14)
private Lyrics getBeamedLyrics(Intent intent) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
// only one message sent during the beam
if (rawMsgs != null && rawMsgs.length > 0) {
NdefMessage msg = (NdefMessage) rawMsgs[0];
// record 0 contains the MIME type, record 1 is the AAR, if present
NdefRecord[] records = msg.getRecords();
if (records.length > 0) {
try {
return Lyrics.fromBytes(records[0].getPayload());
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
return null;
}