2012-07-20 2 views
1

그 코드를 사용하고 있지만 아무런 문제가 없습니다.이 코드의 문제점은 무엇입니까? 실행 중이며 오류가 표시되지 않습니다. 한 장치에서 다른 장치로 태그를 공유해야합니다.두 개의 nfc 장치 사이에서 에뮬레이션을 시작합니다. 해당 코드를 사용하고 있지만 아무 일도 일어나지 않습니다. 어떻게해야합니까?

한 장치에서 다른 장치로 태그를 공유하는 코드를 알려주십시오.

package com.app.app.nfctag; 

    import java.nio.charset.Charset; 

    import android.app.Activity; 
    import android.app.PendingIntent; 
    import android.content.Intent; 
    import android.content.IntentFilter; 
    import android.nfc.NdefMessage; 
    import android.nfc.NdefRecord; 
    import android.nfc.NfcAdapter; 
    import android.os.Bundle; 
    import android.os.Parcelable; 
    import android.widget.Button; 
    import android.widget.TextView; 
    import android.widget.Toast; 

    public class DemoNFCtagActivity extends Activity { 

      NdefMessage msg; 

     @Override 
     protected void onResume() { 
      // TODO Auto-generated method stub 
      super.onResume(); 
      if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) { 
        processIntent(getIntent()); 
       } 
       mNfcAdapter.enableForegroundNdefPush(this,msg); 

     } 
     @Override 
      public void onNewIntent(Intent intent) { 
       // onResume gets called after this to handle the intent 
       setIntent(intent); 
      } 

     NfcAdapter mNfcAdapter; 
     TextView textView; 

     Button btnEmulation; 
     PendingIntent mNfcPendingIntent; 
     IntentFilter[] mNdefExchangeFilters; 
     NdefMessage message; 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

      // TextView textView = (TextView) findViewById(R.id.textView); 
      // Check for available NFC Adapter 
      mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 
      if (mNfcAdapter == null) { 
       Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show(); 
       finish(); 
       return; 
      } 
      // Register callback 
     // mNfcAdapter.setNdefPushMessageCallback(this, this); 


      String text = ("Beam me up, Android!\n\n" + 
        "Beam Time: " + System.currentTimeMillis()); 
      msg = new NdefMessage(
        new NdefRecord[] { createMimeRecord(
          "application/com.example.android.beam", text.getBytes()) 
      /** 
       * The Android Application Record (AAR) is commented out. When a device 
       * receives a push with an AAR in it, the application specified in the AAR 
       * is guaranteed to run. The AAR overrides the tag dispatch system. 
       * You can add it back in to guarantee that this 
       * activity starts when receiving a beamed message. For now, this code 
       * uses the tag dispatch system. 
       */ 
       //,NdefRecord.createApplicationRecord("com.example.android.beam") 
      }); 






     } 

     public NdefRecord createMimeRecord(String mimeType, byte[] payload) { 
      byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII")); 
      NdefRecord mimeRecord = new NdefRecord(
        NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload); 
      return mimeRecord; 
     } 

     void processIntent(Intent intent) { 
      textView = (TextView) findViewById(R.id.textView); 
      Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
        NfcAdapter.EXTRA_NDEF_MESSAGES); 
      // only one message sent during the beam 
      NdefMessage msg = (NdefMessage) rawMsgs[0]; 
      // record 0 contains the MIME type, record 1 is the AAR, if present 
      textView.setText(new String(msg.getRecords()[0].getPayload())); 
     } 
    } 

답변

관련 문제