Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created November 8, 2017 16:06
Show Gist options
  • Save ssaurel/7ed45f9b73a39c30d06d98328d8c16ce to your computer and use it in GitHub Desktop.
Save ssaurel/7ed45f9b73a39c30d06d98328d8c16ce to your computer and use it in GitHub Desktop.
NdefMessageParser class for the NFC Reader Tutorial
/*
* Copyright (C) 2010 The Android Open Source Project
* Modified by Sylvain Saurel for a tutorial
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.ssaurel.nfcreader.parser;
import android.app.Activity;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import com.ssaurel.nfcreader.R;
import com.ssaurel.nfcreader.model.History;
import com.ssaurel.nfcreader.record.ParsedNdefRecord;
import com.ssaurel.nfcreader.record.SmartPoster;
import com.ssaurel.nfcreader.record.TextRecord;
import com.ssaurel.nfcreader.record.UriRecord;
import com.ssaurel.nfcreader.utils.NFCReaderApp;
import java.util.ArrayList;
import java.util.List;
public class NdefMessageParser {
private NdefMessageParser() {
}
public static List<ParsedNdefRecord> parse(NdefMessage message) {
return getRecords(message.getRecords());
}
public static List<ParsedNdefRecord> getRecords(NdefRecord[] records) {
List<ParsedNdefRecord> elements = new ArrayList<ParsedNdefRecord>();
for (final NdefRecord record : records) {
if (UriRecord.isUri(record)) {
elements.add(UriRecord.parse(record));
} else if (TextRecord.isText(record)) {
elements.add(TextRecord.parse(record));
} else if (SmartPoster.isPoster(record)) {
elements.add(SmartPoster.parse(record));
} else {
elements.add(new ParsedNdefRecord() {
public String str() {
return new String(record.getPayload());
}
});
}
}
return elements;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment