歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android NFC開發實戰

Android NFC開發實戰

日期:2017/3/1 10:55:24   编辑:Linux編程

對於Android 4.0 SDK中提供的Beam例子,對於NFC開發來說的確是一個不錯的模板。對於了解NFC的NDEF消息處理過程不妨看下面的代碼。

  1. public class Beam extends Activity implements CreateNdefMessageCallback,
  2. OnNdefPushCompleteCallback {
  3. NfcAdapter mNfcAdapter;
  4. TextView mInfoText;
  5. private static final int MESSAGE_SENT = 1;
  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main);
  10. mInfoText = (TextView) findViewById(R.id.textView);
  11. mNfcAdapter = NfcAdapter.getDefaultAdapter(this); //實例化NFC設備
  12. if (mNfcAdapter == null) {
  13. mInfoText = (TextView) findViewById(R.id.textView);
  14. mInfoText.setText("NFC is not available on this device.");
  15. }
  16. mNfcAdapter.setNdefPushMessageCallback(this, this); //注冊NDEF回調消息
  17. mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
  18. }
  19. @Override
  20. public NdefMessage createNdefMessage(NfcEvent event) {
  21. Time time = new Time();
  22. time.setToNow();
  23. String text = ("Beam me up!\n\n" +
  24. "Beam Time: " + time.format("%H:%M:%S"));
  25. NdefMessage msg = new NdefMessage(
  26. new NdefRecord[] { createMimeRecord(
  27. "application/com.example.android.beam", text.getBytes())
  28. });
  29. return msg;
  30. }
  31. @Override
  32. public void onNdefPushComplete(NfcEvent arg0) {
  33. // A handler is needed to send messages to the activity when this
  34. // callback occurs, because it happens from a binder thread
  35. mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();
  36. }
  37. private final Handler mHandler = new Handler() {
  38. @Override
  39. public void handleMessage(Message msg) {
  40. switch (msg.what) {
  41. case MESSAGE_SENT:
  42. Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show();
  43. break;
  44. }
  45. }
  46. };
  47. @Override
  48. public void onResume() {
  49. super.onResume();
  50. if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
  51. processIntent(getIntent());
  52. }
  53. }
  54. @Override
  55. public void onNewIntent(Intent intent) {
  56. // onResume gets called after this to handle the intent
  57. setIntent(intent);
  58. }
  59. /**
  60. * Parses the NDEF Message from the intent and prints to the TextView
  61. */
  62. void processIntent(Intent intent) {
  63. Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
  64. NfcAdapter.EXTRA_NDEF_MESSAGES);
  65. // only one message sent during the beam
  66. NdefMessage msg = (NdefMessage) rawMsgs[0];
  67. // record 0 contains the MIME type, record 1 is the AAR, if present
  68. mInfoText.setText(new String(msg.getRecords()[0].getPayload()));
  69. }
  70. /**
  71. * Creates a custom MIME type encapsulated in an NDEF record
  72. *
  73. * @param mimeType
  74. */
  75. public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
  76. byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
  77. NdefRecord mimeRecord = new NdefRecord(
  78. NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
  79. return mimeRecord;
  80. }
  81. @Override
  82. public boolean onCreateOptionsMenu(Menu menu) {
  83. // If NFC is not available, we won't be needing this menu
  84. if (mNfcAdapter == null) {
  85. return super.onCreateOptionsMenu(menu);
  86. }
  87. MenuInflater inflater = getMenuInflater();
  88. inflater.inflate(R.menu.options, menu);
  89. return true;
  90. }
  91. @Override
  92. public boolean onOptionsItemSelected(MenuItem item) {
  93. switch (item.getItemId()) {
  94. case R.id.menu_settings:
  95. Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
  96. startActivity(intent);
  97. return true;
  98. default:
  99. return super.onOptionsItemSelected(item);
  100. }
  101. }
  102. }
Copyright © Linux教程網 All Rights Reserved