Code example for NfcAdapter

Methods: disableForegroundDispatch, disableForegroundNdefPush, enableForegroundDispatch

0
	    mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, 
	        mNdefExchangeFilters, null);
	} 
	 
    private void disableNdefExchangeMode() { 
        mNfcAdapter.disableForegroundNdefPush(this);
        mNfcAdapter.disableForegroundDispatch(this);
    } 
	 
 
     
	public void onResume() { 
		super.onResume(); 
		 
		Intent intent = getIntent();
 
		 
		if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
 
			Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
			if (rawMsgs != null) {
				msgs = new NdefMessage[rawMsgs.length];
				for (int i = 0; i< rawMsgs.length; i++) {
					msgs[i] = (NdefMessage) rawMsgs[i];
				} 
			} 
			byte[] payload = msgs[0].getRecords()[0].getPayload();
			System.out.println("Payload :" + new String(payload));
	        Intent NFC_transmission = getIntent();
	        NFC_transmission.putExtra("nfc_value", new String(payload));
	        setResult(RESULT_OK, NFC_transmission);
	         
			SharedPreferences settings = getSharedPreferences("container_prefs", 0);
			SharedPreferences.Editor editor = settings.edit();
			editor.putString("nfc_retrieved_message", new String(payload));
			editor.commit();
	         
			finish();
			 
		} 
		 
//		enableNdefExchangeMode(); 
		 
		//process NDEF MESSAGE here 
	} 
	 
    protected void onPause() 
    { 
    	super.onPause(); 
    	disableNdefExchangeMode(); 
    } 
	 
	public void onBackPressed() { 
		finish();
	} 
     
	@Override 
	protected void onNewIntent(Intent intent) {
	    // NDEF exchange mode for peer to peer 
	    if (!mWriteMode &&  NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
	        NdefMessage[] msgs = getNdefMessages(intent);
	        Toast.makeText(this, "Item received", Toast.LENGTH_LONG).show();
	        System.out.println("msgs = " + msgs.toString());
	        Intent NFC_transmission = getIntent();
	        NFC_transmission.putExtra("nfc_value", msgs.toString());
	        setResult(RESULT_OK, NFC_transmission);
			finish();
	    } 
	     
	 // Tag writing mode 
        if (mWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
            Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            writeTag(createNFCMessage(), detectedTag);
        } 
	     
	} 
	 
	// when he select the button to write to it gives a dialog box to write to a tag, and will remain unless back button is hit or tag is touched 
	 private View.OnClickListener mTagWriter = new View.OnClickListener() {
	        public void onClick(View arg0) { 
	            // Write to a tag for as long as the dialog is shown. 
	            disableNdefExchangeMode(); 
	            enableTagWriteMode(); 
 
	            new AlertDialog.Builder(NFCOperation.this).setTitle("Touch tag to write") 
	                    .setOnCancelListener(new DialogInterface.OnCancelListener() { 
	                        public void onCancel(DialogInterface dialog) { 
	                            disableTagWriteMode(); 
	                            enableNdefExchangeMode(); 
	                        } 
	                    }).create().show(); 
	        } 
	    }; 
	 
	private void enableTagWriteMode() { 
	    mWriteMode = true;
	    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
	    mWriteTagFilters = new IntentFilter[] { tagDetected };
	    mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, mWriteTagFilters, null);
	} 
	 
    private void disableTagWriteMode() { 
        mWriteMode = false;
        mNfcAdapter.disableForegroundDispatch(this);