本文整理汇总了Java中android.nfc.FormatException类的典型用法代码示例。如果您正苦于以下问题:Java FormatException类的具体用法?Java FormatException怎么用?Java FormatException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FormatException类属于android.nfc包,在下文中一共展示了FormatException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeTAG
import android.nfc.FormatException; //导入依赖的package包/类
protected void writeTAG(Tag tag) throws IOException, FormatException {
Ndef ndefTag = Ndef.get(tag);
byte[] stringBytes = passphrase.getBytes();
NdefRecord data = NdefRecord.createMime(CONST.NFC_MIME_LOGIN, stringBytes);
NdefMessage message = new NdefMessage(data);
if (ndefTag != null) { //write to formatted tag
ndefTag.connect();
ndefTag.writeNdefMessage(message);
} else { //format the tag
NdefFormatable format = NdefFormatable.get(tag);
if(format != null) {
format.connect();
format.format(message);
}
}
}
示例2: writeToNfc
import android.nfc.FormatException; //导入依赖的package包/类
private void writeToNfc(Ndef ndef, String message){
mTvMessage.setText(getString(R.string.message_write_progress));
if (ndef != null) {
try {
ndef.connect();
NdefRecord mimeRecord = NdefRecord.createMime("text/plain", message.getBytes(Charset.forName("US-ASCII")));
ndef.writeNdefMessage(new NdefMessage(mimeRecord));
ndef.close();
//Write Successful
mTvMessage.setText(getString(R.string.message_write_success));
} catch (IOException | FormatException e) {
e.printStackTrace();
mTvMessage.setText(getString(R.string.message_write_error));
} finally {
mProgress.setVisibility(View.GONE);
}
}
}
示例3: openFile
import android.nfc.FormatException; //导入依赖的package包/类
private void openFile(String filePath) throws Exception {
if (filePath == null) {
throw new FileNotFoundException("No file selected");
}
File myFile = new File(filePath);
if (!myFile.exists()) {
throw new FileNotFoundException("Cannot find: " + myFile.toString());
}
if (!myFile.canRead()) {
throw new FormatException("Cannot open: " + myFile.toString());
}
dfuFile.filePath = myFile.toString();
dfuFile.file = new byte[(int) myFile.length()];
//convert file into byte array
FileInputStream fileInputStream = new FileInputStream(myFile);
int readLength = fileInputStream.read(dfuFile.file);
fileInputStream.close();
if (readLength != myFile.length()) {
throw new IOException("Could Not Read File");
}
}
示例4: parseNdefRecord
import android.nfc.FormatException; //导入依赖的package包/类
public static SmartPosterRecord parseNdefRecord(NdefRecord ndefRecord) throws FormatException {
byte[] payload = ndefRecord.getPayload();
normalizeMessageBeginEnd(payload);
SmartPosterRecord smartPosterRecord = new SmartPosterRecord();
if(payload.length > 0) {
List<Record> records = Message.parseNdefMessage(payload);
for (Record record : records) {
if (record instanceof UriRecord) {
smartPosterRecord.setUri((UriRecord)record);
}
else if (record instanceof TextRecord) {
smartPosterRecord.setTitle((TextRecord)record);
}
else if (record instanceof ActionRecord) {
smartPosterRecord.setAction((ActionRecord)record);
}
}
}
return smartPosterRecord;
}
示例5: readFromNFC
import android.nfc.FormatException; //导入依赖的package包/类
private void readFromNFC(Ndef ndef) {
try {
ndef.connect();
NdefMessage ndefMessage = ndef.getNdefMessage();
String message = new String(ndefMessage.getRecords()[0].getPayload());
Log.d(TAG, "readFromNFC: "+message);
mTvMessage.setText(message);
ndef.close();
} catch (IOException | FormatException e) {
e.printStackTrace();
}
}
示例6: write
import android.nfc.FormatException; //导入依赖的package包/类
private void write(String text, Tag tag) throws IOException, FormatException {
/*
http://stackoverflow.com/questions/11427997/android-app-to-add-mutiple-record-in-nfc-tag
*/
// We want to include a reference to the app, for those who don't have one.
// This way, their phones will open this app when a tag encoded with this app is used.
String arrPackageName = "com.briankhuu.nfcmessageboard";
final int AAR_RECORD_BYTE_LENGTH = 50; // I guess i suck at byte counting. well at least this should still work. This approach does lead to wasted space however.
//infoMsg = "\n\n---\n To post here. Use the "NFC Messageboard" app: https://play.google.com/store/search?q=NFC%20Message%20Board ";
// Trim to size (for now this is just a dumb trimmer...) (Later on, you want to remove whole post first
// Seem that header and other things takes 14 chars. For safety. Lets just remove 20.
// 0 (via absolute value) < valid entry size < Max Tag size
final int NDEF_RECORD_HEADER_SIZE = 6;
final int NDEF_STRING_PAYLOAD_HEADER_SIZE = 4;
int maxTagByteLength = Math.abs(tag_size - NDEF_RECORD_HEADER_SIZE - NDEF_STRING_PAYLOAD_HEADER_SIZE - AAR_RECORD_BYTE_LENGTH);
if (text.length() >= maxTagByteLength ){ // Write like normal if content to write will fit without modification
// Else work out what to remove. For now, just do a dumb trimming. // Unicode characters may take more than 1 byte.
text = truncateWhenUTF8(text, maxTagByteLength);
}
// Write tag
//NdefRecord[] records = { createRecord(text), aarNdefRecord };
NdefMessage message = new NdefMessage(new NdefRecord[]{
createRecord(text)
,NdefRecord.createApplicationRecord(arrPackageName)
});
Ndef ndef = Ndef.get(tag);
ndef.connect();
ndef.writeNdefMessage(message);
ndef.close();
}
示例7: readTag
import android.nfc.FormatException; //导入依赖的package包/类
private byte[] readTag(Tag tag) throws IOException, FormatException, ReadingTagException {
List<String> tech = Arrays.asList(tag.getTechList());
if (tech.contains(Ndef.class.getName())) {
Log.v(TAG, "Read formatted tag.");
return readNdeftag(Ndef.get(tag));
} else if (tech.contains(MifareUltralight.class.getName())) {
Log.v(TAG, "Read Mifare ultralight tag.");
return readMifareUltralight(MifareUltralight.get(tag));
}
Toast.makeText(this, "No supported tag found.", Toast.LENGTH_SHORT).show();
Log.e(TAG, "No supported tag found: " + tech);
throw new ReadingTagException("No supported tag found.");
}
示例8: readNdeftag
import android.nfc.FormatException; //导入依赖的package包/类
private byte[] readNdeftag(Ndef tag) throws IOException, FormatException, ReadingTagException {
NdefMessage message = tag.getNdefMessage();
if (message.getRecords().length == 0) {
Toast.makeText(this, "Empty tag found.", Toast.LENGTH_SHORT).show();
throw new ReadingTagException("Empty tag.");
}
return message.getRecords()[0].getPayload();
}
示例9: nfcIntentDetected
import android.nfc.FormatException; //导入依赖的package包/类
@Override
public void nfcIntentDetected(Intent intent, String action) {
Log.d(TAG, "nfcIntentDetected: " + action);
Parcelable[] messages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (messages != null) {
NdefMessage[] ndefMessages = new NdefMessage[messages.length];
for (int i = 0; i < messages.length; i++) {
ndefMessages[i] = (NdefMessage) messages[i];
}
if(ndefMessages.length > 0) {
// read as much as possible
Message message = new Message();
for (int i = 0; i < messages.length; i++) {
NdefMessage ndefMessage = (NdefMessage) messages[i];
for(NdefRecord ndefRecord : ndefMessage.getRecords()) {
try {
message.add(Record.parse(ndefRecord));
} catch (FormatException e) {
// if the record is unsupported or corrupted, keep as unsupported record
message.add(UnsupportedRecord.parse(ndefRecord));
}
}
}
readNdefMessage(message);
} else {
readEmptyNdefMessage();
}
} else {
readNonNdefMessage();
}
}
示例10: parseNdefRecord
import android.nfc.FormatException; //导入依赖的package包/类
public static GcActionRecord parseNdefRecord(NdefRecord ndefRecord) throws FormatException {
byte[] payload = ndefRecord.getPayload();
if ((payload[0] & GcActionRecord.NUMERIC_CODE) != 0) {
return new GcActionRecord(Action.getActionByValue(payload[1]));
} else {
return new GcActionRecord(Record.parse(payload, 1, payload.length - 1));
}
}
示例11: parseNdefRecord
import android.nfc.FormatException; //导入依赖的package包/类
public static GenericControlRecord parseNdefRecord(NdefRecord ndefRecord) throws FormatException {
byte[] payload = ndefRecord.getPayload();
normalizeMessageBeginEnd(payload, 1, payload.length -1);
Message payloadNdefMessage = Message.parseNdefMessage(payload, 1, payload.length - 1);
GenericControlRecord genericControlRecord = new GenericControlRecord();
genericControlRecord.setConfigurationByte(payload[0]);
for (Record record : payloadNdefMessage) {
if (record instanceof GcTargetRecord) {
genericControlRecord.setTarget((GcTargetRecord)record);
} else if (record instanceof GcActionRecord) {
genericControlRecord.setAction((GcActionRecord)record);
} else if (record instanceof GcDataRecord) {
genericControlRecord.setData((GcDataRecord)record);
} else {
throw new IllegalArgumentException("Unexpected record " + record.getClass().getName());
}
}
if (!genericControlRecord.hasTarget()) {
throw new IllegalArgumentException("Expected target record");
}
return genericControlRecord;
}
示例12: Message
import android.nfc.FormatException; //导入依赖的package包/类
/**
* {@link NdefMessage} constructor.
*
* @param ndefMessage
* @throws FormatException if known record type cannot be parsed
*/
public Message(NdefMessage ndefMessage) throws FormatException {
for(NdefRecord record : ndefMessage.getRecords()) {
add(Record.parse(record));
}
}
示例13: parse
import android.nfc.FormatException; //导入依赖的package包/类
/**
* Parse single record.
*
* @param record record to parse
* @return corresponding {@link Record} subclass - or null if not known
* @throws FormatException if known record type cannot be parsed
* @throws IllegalArgumentException if zero or more than one record
*/
protected static Record parse(byte[] record) throws FormatException {
NdefMessage message = new NdefMessage(record);
if(message.getRecords().length != 1) {
throw new IllegalArgumentException("Single record expected");
}
return Record.parse(message.getRecords()[0]);
}
示例14: writeUriCustomHeader
import android.nfc.FormatException; //导入依赖的package包/类
boolean writeUriCustomHeader(String uri, String technology, byte header, boolean readonly) throws IllegalAccessException, NoSuchFieldException, ClassNotFoundException, InsufficientCapacityException, FormatException, ReadOnlyTagException, TagNotPresentException {
final Tag mockTag = mTestUtilities.mockTag(technology);
final Intent intent = new Intent().putExtra(NfcAdapter.EXTRA_TAG, mockTag);
NfcWriteUtility nfcWriteUtility = mTestUtilities.determineMockType(null);
return nfcWriteUtility != null && (readonly ? nfcWriteUtility.makeOperationReadOnly().writeUriWithPayloadToTagFromIntent(uri, header, intent) : nfcWriteUtility.writeUriWithPayloadToTagFromIntent(uri, header, intent));
}
示例15: writeUriCustomHeader
import android.nfc.FormatException; //导入依赖的package包/类
boolean writeUriCustomHeader(String uri, String technology, byte header, boolean readonly) throws IllegalAccessException, NoSuchFieldException, ClassNotFoundException, InsufficientCapacityException, FormatException, ReadOnlyTagException, TagNotPresentException {
final Tag mockTag = mTestUtilities.mockTag(technology);
final Intent intent = new Intent().putExtra(NfcAdapter.EXTRA_TAG, mockTag);
NfcWriteUtility nfcWriteUtility = mTestUtilities.determineMockType(technology);
return nfcWriteUtility != null && (readonly ? nfcWriteUtility.makeOperationReadOnly().writeUriWithPayloadToTagFromIntent(uri, header, intent) : nfcWriteUtility.writeUriWithPayloadToTagFromIntent(uri, header, intent));
}