-
Notifications
You must be signed in to change notification settings - Fork 13
WriteUtility
| Home | Reading NFC Tags | Writing NFC Tags | Creating NdefMessages | Exception Handling |
|---|---|---|---|---|
| How it works | Reading | Writing | NdefMessages | Exceptions |
The write class is seperated into several modules.
There is the NdefWriteUtility, which goes over the really low level stuff.
In here we do the actual writing, meaning connecting to our Ndef/NdefFormatable tag, writing and eventually make the tag read only.
In WriteUtility you'll find some methods which'll allow you to pass a Tag, and to make it read only. The abstraction here is done so you don't need to care whether or not the tag is already Ndef Formatted or capable of being formatted, just plug it in, and examine the result to see if it worked.
Finally there is the NfcWriteUtility.
This is the class which makes it convenient for you to write some data to the NFC tag, without having to worry about the implementation, so you can look at this as your convenient API for quickly writing data to the tag.
####Some snippets
- Async call :
public void onNewIntent(final Intent paramIntent) {
super.onNewIntent(paramIntent);
final Context context = this;
/**
* Called on UI thread, do NOT perform UI logic here !
*/
AsyncOperationCallback operationCallback = new AsyncOperationCallback() {
@Override
public boolean performWrite(NfcWriteUtility writeUtility) throws ReadOnlyTagException, InsufficientCapacityException, TagNotPresentException, FormatException {
return writeUtility.writeUriToTagFromIntent("google.be",paramIntent);
}
};
/**
* Called on the UI thread
*/
AsyncUiCallback uiCallback = new AsyncUiCallback() {
@Override
public void callbackWithReturnValue(Boolean result) {
Toast.makeText(context, result ? "Succes !" : "Error'ed", Toast.LENGTH_SHORT).show();
}
@Override
public void onProgressUpdate(Boolean... values) {
Toast.makeText(context, values[0] ? "Started writing" : "Cannot start writing", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Exception e) {
Toast.makeText(context, "We encountered "+ e.getMessage(), Toast.LENGTH_SHORT).show();
}
};
new WriteCallbackNfcAsync(uiCallback,operationCallback).executeWriteOperation();-
This action is executed asynchronously, the application will not be blocked when performing the operation.
-
You can see the AsyncUiCallback and AsyncOperationCallback. The UI callbacks are always called on the UI thread, whereas the AsyncOperationCallback is executed on the background thread.
-
Should the operation encounter an exception, then it is cancelled
- The result will be false
- The
onError(Exception e)will be fired, with the encountered (and caught) exception.
-
Blocking call :
public void onNewIntent(Intent paramIntent) {
super.onNewIntent(paramIntent);
try {
new NfcWriteUtilityImpl().writeUriToTagFromIntent("google.be",paramIntent);
// Thrown when the message is malformed
} catch (FormatException e) {
e.printStackTrace();
// Thrown when attempting to write to a read only tag
} catch (ReadOnlyTagException e) {
e.printStackTrace();
// Thrown when the tag does not have enough capacity for your message
} catch (InsufficientCapacityException e) {
e.printStackTrace();
// Thrown when the Tag is not available in the intent
} catch (TagNotPresentException e) {
e.printStackTrace();
}
}- These are all the exceptions possibly being thrown by the library.
Data is written to a tag with a certain payload header, indicating what sort of data is actually being written.
For example : When you write a payload with header NfcPayloadHeader.HTTP_WWW , the data being written is automatically pre-fixed with http://www. when read..
This does mean however, that your actual link stored in the tag, does not need that prefix again, and thus including it again would result in an invalid URI.
Should you encounter a discrepancy between the standards from the NFC Forum and our implementation, be sure to inform us!