Вопрос

I'm trying to learn NFC and read through Google documentation. I copied their code to create an Activty to beam data from one Android device to another.

I cannot get it to run on line new NdefRecord[] { createMime(... I get a "undefined for type beamActivity" error message from Google's sample code.

The complete code is below

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

}
Это было полезно?

Решение

createMime(...) is a static method of NdefRecord:

NdefMessage msg = new NdefMessage(
    new NdefRecord[] {
        NdefRecord.createMime(
                "application/vnd.com.example.android.beam",
                text.getBytes())
    });
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top