2012-07-20 60 views
1

我必須從一個設備共享標籤到其他,我有android 2.3.3 galaxy。 請分享一些代碼,用於將標籤從一臺設備共享到另一臺設備。如何在Android的兩個nfc設備之間啓動模擬

我採取這種代碼的開發者網站,但它顯示的錯誤在該行

mNfcAdapter.setNdefPushMessageCallback(this, this); 


    // 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); 

現在即時通訊使用使forground消息,但它工作正常,但怎麼看仿真..

public class DemoNFCtagActivity extends Activity { 

    NdefMessage msg; 

    @Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 

      mNfcAdapter.enableForegroundNdefPush(this,msg); 

    } 

    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; 
    } 
} 

回答

0

NfcAdapter.setNdefPushMessageCallback()僅適用於Android 4.0 ICS及更高版本。改爲使用enableForegroundNdefPush()

+0

我有另一個問題,我使用上面描述的代碼.. 但如何看到仿真意味着代碼不顯示任何錯誤,但沒有發生..請幫助 – user626130 2012-07-20 11:43:16