1

我正在尝试学习 NFC 并阅读 Google 文档。我复制了他们的代码来创建一个 Activty,将数据从一台 Android 设备传输到另一台设备。

我无法让它在线运行 new NdefRecord[] { createMime(... 我从 Google 的示例代码中收到“未定义类型 beamActivity”错误消息。

完整代码如下

public class BeamActivity extends Activity
                          implements CreateNdefMessageCallback, 
                                     OnNdefPushCompleteCallback {

    NfcAdapter mNfcAdapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // see if there is a NFC interface
        mNfcAdapter=NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter==null)
            Toast.makeText(this,"no adapter",
                    Toast.LENGTH_SHORT).show();

        mNfcAdapter.setNdefPushMessageCallback(this,this);  
            mNfcAdapter.setOnNdefPushCompleteCallback(this,this);
    }

    public void onNdefPushComplete( NfcEvent arg0) {
        mHandler.obtainMessage(1).sendToTarget();
    }

    private final Handler mHandler = new Handler() {
        @Override
        public void  handleMessage(Message msg) {
            switch(msg.what) {
            case 1:
                Toast.makeText( getApplicationContext(),"Mesg Sent",
                        Toast.LENGTH_SHORT).show();
                 break;
            } // end switch
        } // end handle mesg
    }; // end new

    // creat call back code
    // gets called to star a beam, return messag to beam  
    @Override
    public NdefMessage createNdefMessage(NfcEvent event) {
        String text = ("Beam me up, Android!\n\n" +
                "Beam Time: " + System.currentTimeMillis());

        // error message hear
        NdefMessage msg = new NdefMessage(
                new NdefRecord[] { createMime(
                        "application/vnd.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")
        });
        return msg;
    }


    @Override
    public void onNewIntent(Intent intent)
    {
        setIntent(intent);
    }

    @Override
    public void onResume()
    {
        super.onRestart();

        if (mNfcAdapter.ACTION_NDEF_DISCOVERED.equals(  getIntent().getAction()))
        {
            processIntent( getIntent() );
        }
    }

    void processIntent( Intent intet)
    {
        Parcelable[] rawMsgs= intet.getParcelableArrayExtra( mNfcAdapter.EXTRA_NDEF_MESSAGES );

        NdefMessage msg = (  NdefMessage) rawMsgs[0];
        String s= new String(  msg.getRecords()[0].getPayload());

        Toast.makeText( getApplicationContext(), s,
                Toast.LENGTH_SHORT).show();
    }

}
4

1 回答 1

2

createMime(...)是一个静态方法NdefRecord

NdefMessage msg = new NdefMessage(
    new NdefRecord[] {
        NdefRecord.createMime(
                "application/vnd.com.example.android.beam",
                text.getBytes())
    });
于 2013-10-08T06:50:52.663 回答