Send identity destinations to other devices via NFC

This commit is contained in:
str4d
2014-07-03 12:32:03 +00:00
parent f700132a9e
commit 9a148dde7b
3 changed files with 77 additions and 1 deletions

View File

@@ -1,11 +1,19 @@
package i2p.bote.android.config;
import android.annotation.SuppressLint;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.nfc.NfcEvent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import i2p.bote.android.InitActivities;
public class ViewIdentityActivity extends ActionBarActivity {
NfcAdapter mNfcAdapter;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -26,5 +34,35 @@ public class ViewIdentityActivity extends ActionBarActivity {
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, f).commit();
}
// NFC send only works on API 10+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) {
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);
}
}
@SuppressLint("NewApi")
@Override
public void onResume() {
super.onResume();
if (mNfcAdapter != null &&
Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
mNfcAdapter.enableForegroundNdefPush(this, getNdefMessage());
}
}
private NdefMessage getNdefMessage() {
ViewIdentityFragment f = (ViewIdentityFragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);
return f.createNdefMessage();
}
}

View File

@@ -13,6 +13,9 @@ import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
@@ -153,4 +156,40 @@ public class ViewIdentityFragment extends Fragment {
return super.onOptionsItemSelected(item);
}
}
public NdefMessage createNdefMessage() {
NdefMessage msg = new NdefMessage(new NdefRecord[]{
createNameRecord(),
createDestinationRecord()
});
return msg;
}
private NdefRecord createNameRecord() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
return new NdefRecord(
NdefRecord.TNF_EXTERNAL_TYPE,
"i2p.bote:contact".getBytes(),
new byte[0],
mIdentity.getPublicName().getBytes()
);
else
return NdefRecord.createExternal(
"i2p.bote", "contact", mIdentity.getPublicName().getBytes()
);
}
private NdefRecord createDestinationRecord() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
return new NdefRecord(
NdefRecord.TNF_EXTERNAL_TYPE,
"i2p.bote:contactDestination".getBytes(),
new byte[0],
mIdentity.getKey().getBytes()
);
else
return NdefRecord.createExternal(
"i2p.bote", "contactDestination", mIdentity.getKey().getBytes()
);
}
}