2

嗨,我正在开发一个应用程序,它将读取和写入 nfc 标签。我已经为 nfc 构建了两个独立的读写器应用程序。现在我想将它们合并在一起。这是我在作家应用程序中所做的:

// here I specify which application is going to listen for my tag.

new NdefMessage(new NdefRecord[] { rtdUriRecord,NdefRecord.createApplicationRecord("com.example.nfc")

并且为了阅读我指定

 <intent-filter>
      <action android:name="android.nfc.action.NDEF_DISCOVERED" />
      <category android:name="android.intent.category.DEFAULT" />
      <data android:mimeType="text/plain" />
 </intent-filter>

我合并双方然后我的清单看起来像

    <activity
        android:name="com.example.nfc.NFCWriter"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity> 

    <activity
        android:name="com.example.nfc.NFCReader"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity> 

现在我的意图是,当我的应用程序打开并且我触摸卡时,它将向卡写入数据,而当应用程序未打开时,如果我触摸卡,它将从卡中读取数据。可能吗。需要帮忙。谢谢

在作家方面...

    // declearation for nfc
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
            mNfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                    getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                    | Intent.FLAG_ACTIVITY_CLEAR_TOP), 0);
        IntentFilter discovery=new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        mWriteTagFilters = new IntentFilter[] { discovery };



    // create ndf message
    private NdefMessage getTagAsNdef() 
        {

           boolean addAAR = true;
           String uniqueId = "data";
           byte[] uriField = uniqueId.getBytes(Charset.forName("US-ASCII"));
           byte[] payload = new byte[uriField.length + 1];              //add 1 for the URI Prefix
           payload[0] = 0x01;                                       //prefixes http://www. to the URI

           System.arraycopy(uriField, 0, payload, 1, uriField.length);  //appends URI to payload
           NdefRecord rtdUriRecord = new NdefRecord(
               NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload);

           if(addAAR) 
           {
              return new NdefMessage(new NdefRecord[] {
                   rtdUriRecord, NdefRecord.createApplicationRecord("com.mobiotics.nfc")
           }); 
           }
           else 
           {
               return new NdefMessage(new NdefRecord[] {rtdUriRecord});
           }
         }

    // listen for new intent for activity
    @Override
        protected void onNewIntent(Intent intent) 
        {
            super.onNewIntent(intent);
            Log.i("*******************************", "inside new intent writer1");
            if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) 
            {
            // validate that this tag can be written....
            detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            if(supportedTechs(detectedTag.getTechList())) {
                // check if tag is writable (to the extent that we can
                if(writableTag(detectedTag)) {
                    ready_to_write = true;
                } else {
                    Toast.makeText(getApplicationContext(),"This tag is not writable",Toast.LENGTH_SHORT).show();
                }               
            } else {
                Toast.makeText(getApplicationContext(),"This tag type is not supported",Toast.LENGTH_SHORT).show();
            }
        }

 // in manifest file 
<activity
    android:name="com.mobiotics.nfc.NFCWriter1"
    android:label="WRITER1">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

在读者方面...

    // get new intent ...
    private void handleIntent(Intent intent) 
        {
            String action = intent.getAction();
            if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {

                String type = intent.getType();
                if (MIME_TEXT_PLAIN.equals(type)) {

                    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                    //new NdefReaderTask().execute(tag);
                    setViewText(ndefmessage(tag));

                } else {
                    Log.d(TAG, "Wrong mime type: " + type);
                }
            } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {

                // In case we would still use the Tech Discovered Intent
                Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                String[] techList = tag.getTechList();
                String searchedTech = Ndef.class.getName();

                for (String tech : techList) {
                    if (searchedTech.equals(tech)) {
                        //new NdefReaderTask().execute(tag);
                        ndefmessage(tag);
                        break;
                    }
                }
            }
        }


    // read tag data ...
    private String ndefmessage(Tag tag)
        {
            Tag tag1 = tag;

            Ndef ndef = Ndef.get(tag);
            if (ndef == null) {
                // NDEF is not supported by this Tag. 
                return null;
            }

            NdefMessage ndefMessage = ndef.getCachedNdefMessage();

            NdefRecord[] records = ndefMessage.getRecords();
            for (NdefRecord ndefRecord : records) {
                if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
                    try {
                        return readText1(ndefRecord);
                    } catch (UnsupportedEncodingException e) {
                        Log.e(TAG, "Unsupported Encoding", e);
                    }
                }
            }

            return null;
        }

        private String readText1(NdefRecord record) throws UnsupportedEncodingException 
        {
            byte[] payload = record.getPayload();
            String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16";
            int languageCodeLength = payload[0] & 0063;

            return new String(payload, 0, payload.length , textEncoding);
        }

//in manifest file for reader side...
<activity
        android:name="com.mobiotics.nfc.NFCReader1"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity>

所以我的问题是我想在同一个应用程序中同时使用读取器和写入器。

4

0 回答 0