2013-07-11 29 views
6

Próbuję utworzyć aplikację przy użyciu NFC i chcę tylko spróbować odczytać tag NFC i pobrać wiadomość tekstową z tagu i umieścić go w widoku TextView. Mam już kod, ale nic się nie dzieje, gdy próbuję sparować telefon z tagiem NFC.Odczytywanie wiadomości NDEF z tagu NFC z aplikacji Android

Oto mój kod i może ktoś proszę spojrzeć na niego i zobaczyć, co robię źle i co należy zrobić, aby rozwiązać ten problem, proszę:

Button measurementsDataButton; 
NfcAdapter myNfcAdapter; 
PendingIntent myPendingIntent; 
IntentFilter ndef; 
IntentFilter[] filters; 
String[][] techLists; 
int mCount; 
TextView mText; 
String payload; 
byte payloadHeader; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_nfc_scanner); 

    final ActionBar actionBar = getActionBar(); 
    actionBar.setDisplayHomeAsUpEnabled(true); 

    mText = (TextView) findViewById(R.id.flowTextView1); 

    measurementsDataButton = (Button) findViewById(R.id.measurementsButton1); 
    measurementsDataButton.setOnClickListener(this); 

    myNfcAdapter = NfcAdapter.getDefaultAdapter(this); 

    myPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

    ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); 
    filters = new IntentFilter[] {ndef, }; 
    techLists = new String[][] {new String[] {Ndef.class.getName()}, new String[] {NdefFormatable.class.getName()}}; 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    myNfcAdapter.disableForegroundDispatch(this); 
} 

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

    if(myNfcAdapter != null) { 

     myNfcAdapter.enableForegroundDispatch(this, myPendingIntent, filters, techLists); 

    } 

} 

@Override 
public void onNewIntent(Intent intent) { 

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent())) 
    { 
     NdefMessage [] messages = getNdefMessages(getIntent()); 
     for(int i = 0; i<messages.length; i++) 
     { 
      for(int j = 0; j<messages[0].getRecords().length; j++) 
      { 
       NdefRecord record = messages[i].getRecords()[j]; 
       payload = new String(record.getPayload(), 1, record.getPayload().length-1, Charset.forName("UTF-8")); 
       mText.setText(payload); 
       payloadHeader = record.getPayload()[0]; 
      } 
     } 
    } 
} 

NdefMessage[] getNdefMessages(Intent intent) { 
    // TODO Auto-generated method stub 

    NdefMessage[] message = null; 
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) { 

     Parcelable[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); 

     if(rawMessages != null) { 

      message = new NdefMessage[rawMessages.length]; 
      for(int i = 0; i < rawMessages.length; i++) { 

       message[i] = (NdefMessage) rawMessages[i]; 

      } 
     } 
      else { 
       byte[] empty = new byte[] {}; 
       NdefRecord record = new NdefRecord (NdefRecord.TNF_UNKNOWN, empty, empty, empty); 
       NdefMessage msg = new NdefMessage (new NdefRecord[] {record}); 
       message = new NdefMessage[] {msg}; 
      } 

     } 
      else { 
       Log.d("", "Unknown intent."); 
       finish(); 
       } 


    return message; 
} 
+0

ja już [poradnik] (http://mobile.tutsplus.com/tutorials/android/reading-nfc-tags-with-android/) na przykładzie applicaton. To powinno ci pomóc. – vRallev

+0

Dziękuję bardzo! Poszedłem za twoim tutorialem i wszystko działało dobrze. –

Odpowiedz

3

przykład kodu do wysyłania otrzymywać NDEF TAG :

public class MainActivity extends AppCompatActivity implements NfcAdapter.CreateNdefMessageCallback { 

private NfcAdapter mNfcAdapter; 
TextView tv; 
private PendingIntent mPendingIntent; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    tv = (TextView) findViewById(R.id.textViewReceivedMessage); 

    NfcManager nfcManager = (NfcManager) getSystemService(NFC_SERVICE); 
    mNfcAdapter = nfcManager.getDefaultAdapter(); 

    mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()) 
      .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 
} 

private static NdefMessage getTestMessage() { 
    byte[] mimeBytes = "application/com.android.cts.verifier.nfc" 
      .getBytes(Charset.forName("US-ASCII")); 
    byte[] id = new byte[] {1, 3, 3, 7}; 
    byte[] payload = "CTS Verifier NDEF Push Tag".getBytes(Charset.forName("US-ASCII")); 
    return new NdefMessage(new NdefRecord[] { 
      new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, id, payload) 
    }); 
} 


@Override 
protected void onResume() { 
    super.onResume(); 

    mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null); 
    mNfcAdapter.setNdefPushMessageCallback(this, this); 
} 

// sending message 
@Override 
public NdefMessage createNdefMessage(NfcEvent event) { 
    return getTestMessage(); 
} 


private NdefMessage[] getNdefMessages(Intent intent) { 
    Parcelable[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); 
    if (rawMessages != null) { 
     NdefMessage[] messages = new NdefMessage[rawMessages.length]; 
     for (int i = 0; i < messages.length; i++) { 
      messages[i] = (NdefMessage) rawMessages[i]; 
     } 
     return messages; 
    } else { 
     return null; 
    } 
} 

static String displayByteArray(byte[] bytes) { 
    String res=""; 
    StringBuilder builder = new StringBuilder().append("["); 
    for (int i = 0; i < bytes.length; i++) { 
     res+=(char)bytes[i]; 
    } 
    return res; 
} 

// displaying message 
@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 

    NdefMessage[] messages = getNdefMessages(intent); 
    tv.setText(displayByteArray(messages[0].toByteArray())); 
} 

}