Android NFC开发实战


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

相关内容