use of android.nfc.NdefMessage in project android_frameworks_base by DirtyUnicorns.
the class Ndef method getNdefMessage.
/**
* Read the current {@link android.nfc.NdefMessage} on this tag.
*
* <p>This always reads the current NDEF Message stored on the tag.
*
* <p>Note that this method may return null if the tag was in the
* INITIALIZED state as defined by NFC Forum, as in that state the
* tag is formatted to support NDEF but does not contain a message yet.
*
* <p>This is an I/O operation and will block until complete. It must
* not be called from the main application thread. A blocked call will be canceled with
* {@link IOException} if {@link #close} is called from another thread.
*
* <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
*
* @return the NDEF Message, can be null
* @throws TagLostException if the tag leaves the field
* @throws IOException if there is an I/O failure, or the operation is canceled
* @throws FormatException if the NDEF Message on the tag is malformed
*/
public NdefMessage getNdefMessage() throws IOException, FormatException {
checkConnected();
try {
INfcTag tagService = mTag.getTagService();
if (tagService == null) {
throw new IOException("Mock tags don't support this operation.");
}
int serviceHandle = mTag.getServiceHandle();
if (tagService.isNdef(serviceHandle)) {
NdefMessage msg = tagService.ndefRead(serviceHandle);
if (msg == null && !tagService.isPresent(serviceHandle)) {
throw new TagLostException();
}
return msg;
} else if (!tagService.isPresent(serviceHandle)) {
throw new TagLostException();
} else {
return null;
}
} catch (RemoteException e) {
Log.e(TAG, "NFC service dead", e);
return null;
}
}
use of android.nfc.NdefMessage in project bitcoin-wallet by bitcoin-wallet.
the class SendCoinsFragment method onCreate.
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
backgroundThread = new HandlerThread("backgroundThread", Process.THREAD_PRIORITY_BACKGROUND);
backgroundThread.start();
backgroundHandler = new Handler(backgroundThread.getLooper());
if (savedInstanceState != null) {
restoreInstanceState(savedInstanceState);
} else {
final Intent intent = activity.getIntent();
final String action = intent.getAction();
final Uri intentUri = intent.getData();
final String scheme = intentUri != null ? intentUri.getScheme() : null;
final String mimeType = intent.getType();
if ((Intent.ACTION_VIEW.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) && intentUri != null && "bitcoin".equals(scheme)) {
initStateFromBitcoinUri(intentUri);
} else if ((NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) && PaymentProtocol.MIMETYPE_PAYMENTREQUEST.equals(mimeType)) {
final NdefMessage ndefMessage = (NdefMessage) intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)[0];
final byte[] ndefMessagePayload = Nfc.extractMimePayload(PaymentProtocol.MIMETYPE_PAYMENTREQUEST, ndefMessage);
initStateFromPaymentRequest(mimeType, ndefMessagePayload);
} else if ((Intent.ACTION_VIEW.equals(action)) && PaymentProtocol.MIMETYPE_PAYMENTREQUEST.equals(mimeType)) {
final byte[] paymentRequest = BitcoinIntegration.paymentRequestFromIntent(intent);
if (intentUri != null)
initStateFromIntentUri(mimeType, intentUri);
else if (paymentRequest != null)
initStateFromPaymentRequest(mimeType, paymentRequest);
else
throw new IllegalArgumentException();
} else if (intent.hasExtra(SendCoinsActivity.INTENT_EXTRA_PAYMENT_INTENT)) {
initStateFromIntentExtras(intent.getExtras());
} else {
updateStateFrom(PaymentIntent.blank());
}
}
}
use of android.nfc.NdefMessage in project iosched by google.
the class BeamUtils method tryUpdateIntentFromBeam.
/**
* 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);
}
}
}
use of android.nfc.NdefMessage in project iosched by google.
the class BeamUtils method setBeamSessionUri.
/**
* Sets this activity's Android Beam message to one representing the given session.
*/
public static void setBeamSessionUri(Activity activity, Uri sessionUri) {
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
if (nfcAdapter == null) {
// No NFC :-(
return;
}
nfcAdapter.setNdefPushMessage(new NdefMessage(new NdefRecord[] { new NdefRecord(NdefRecord.TNF_MIME_MEDIA, ScheduleContract.makeContentItemType(ScheduleContract.Sessions.CONTENT_TYPE_ID).getBytes(), new byte[0], sessionUri.toString().getBytes()) }), activity);
}
use of android.nfc.NdefMessage in project weiciyuan by qii.
the class UserInfoActivity method processIntent.
private void processIntent(Intent intent) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
// only one message sent during the beam
NdefMessage msg = (NdefMessage) rawMsgs[0];
Toast.makeText(this, new String(msg.getRecords()[0].getPayload()), Toast.LENGTH_SHORT).show();
bean = new UserBean();
bean.setScreen_name(new String(msg.getRecords()[0].getPayload()));
}
Aggregations