use of android.nfc.NdefMessage in project platform_packages_apps_Settings by BlissRoms.
the class WriteWifiConfigToNfcDialog method handleWriteNfcEvent.
private void handleWriteNfcEvent(Tag tag) {
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
if (ndef.isWritable()) {
NdefRecord record = NdefRecord.createMime(NFC_TOKEN_MIME_TYPE, hexStringToByteArray(mWpsNfcConfigurationToken));
try {
ndef.connect();
ndef.writeNdefMessage(new NdefMessage(record));
getOwnerActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mProgressBar.setVisibility(View.GONE);
}
});
setViewText(mLabelView, R.string.status_write_success);
setViewText(mCancelButton, com.android.internal.R.string.done_label);
} catch (IOException e) {
setViewText(mLabelView, R.string.status_failed_to_write);
Log.e(TAG, "Unable to write Wi-Fi config to NFC tag.", e);
return;
} catch (FormatException e) {
setViewText(mLabelView, R.string.status_failed_to_write);
Log.e(TAG, "Unable to write Wi-Fi config to NFC tag.", e);
return;
}
} else {
setViewText(mLabelView, R.string.status_tag_not_writable);
Log.e(TAG, "Tag is not writable");
}
} else {
setViewText(mLabelView, R.string.status_tag_not_writable);
Log.e(TAG, "Tag does not support NDEF");
}
}
use of android.nfc.NdefMessage in project packages_apps_Contacts by AOKP.
the class NfcHandler method createNdefMessage.
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
ContentResolver resolver = mContext.getContentResolver();
if (mContactUri != null) {
final String lookupKey = Uri.encode(mContactUri.getPathSegments().get(2));
final Uri shareUri;
// of determining this.
if (lookupKey.equals(PROFILE_LOOKUP_KEY)) {
shareUri = Profile.CONTENT_VCARD_URI.buildUpon().appendQueryParameter(Contacts.QUERY_PARAMETER_VCARD_NO_PHOTO, "true").build();
} else {
shareUri = Contacts.CONTENT_VCARD_URI.buildUpon().appendPath(lookupKey).appendQueryParameter(Contacts.QUERY_PARAMETER_VCARD_NO_PHOTO, "true").build();
}
ByteArrayOutputStream ndefBytes = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int r;
try {
InputStream vcardInputStream = resolver.openInputStream(shareUri);
while ((r = vcardInputStream.read(buffer)) > 0) {
ndefBytes.write(buffer, 0, r);
}
NdefRecord record = NdefRecord.createMime("text/x-vcard", ndefBytes.toByteArray());
return new NdefMessage(record);
} catch (IOException e) {
Log.e(TAG, "IOException creating vcard.");
return null;
}
} else {
Log.w(TAG, "No contact URI to share.");
return null;
}
}
use of android.nfc.NdefMessage in project i2p.i2p-bote by i2p.
the class EditContactActivity method processIntent.
/**
* Parses the NDEF Message from the intent
*/
private void processIntent(Intent intent) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs == null || rawMsgs.length < 1)
// TODO notify user?
return;
NdefMessage msg = (NdefMessage) rawMsgs[0];
NdefRecord[] records = msg.getRecords();
if (records.length != 2 || records[0].getTnf() != NdefRecord.TNF_EXTERNAL_TYPE || !Arrays.equals(records[0].getType(), Constants.NDEF_LEGACY_TYPE_CONTACT.getBytes()) || records[1].getTnf() != NdefRecord.TNF_EXTERNAL_TYPE || !Arrays.equals(records[1].getType(), Constants.NDEF_LEGACY_TYPE_CONTACT_DESTINATION.getBytes()))
// TODO notify user?
return;
String name = new String(records[0].getPayload());
String destination = new String(records[1].getPayload());
EditContactFragment f = EditContactFragment.newInstance(name, destination);
getSupportFragmentManager().beginTransaction().replace(R.id.container, f).commit();
}
use of android.nfc.NdefMessage in project i2p.i2p-bote by i2p.
the class ViewIdentityActivity method onCreate.
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
String key = null;
Bundle args = getIntent().getExtras();
if (args != null)
key = args.getString(ViewIdentityFragment.ADDRESS);
if (key == null) {
setResult(RESULT_CANCELED);
finish();
return;
}
ViewIdentityFragment f = ViewIdentityFragment.newInstance(key);
getSupportFragmentManager().beginTransaction().add(android.R.id.content, f).commit();
}
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
mNfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() {
@Override
public NdefMessage createNdefMessage(NfcEvent nfcEvent) {
return getNdefMessage();
}
}, this);
}
}
Aggregations