2012-06-05 3 views
0

Моя деятельность осуществляетNFC посылая неправильные бирки/записи

CreateNdefMessageCallback, OnNdefPushCompleteCallback 

Я звоню

mNfcAdapter.setNdefPushMessageCallback(this, this); 
    // Register callback to listen for message-sent success 
    mNfcAdapter.setOnNdefPushCompleteCallback(this, this); 

Я перекрываться

@Override 
public NdefMessage createNdefMessage(NfcEvent event) { 
    Log.d(TAG, "Creating massage"); 
    String text = TAG + DATA_SPLITTER + ADDRESS + DATA_SPLITTER 
      + USER_NAME; 

    // Nachricht vorbereiten. 
    // String text = "Hello NFC World!"; 
    NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, 
      MIME_TYPE.getBytes(), new byte[0], text.getBytes()); 
    NdefRecord[] records = { record }; 
    NdefMessage msg = new NdefMessage(records); 
    return msg; 
} 

Но когда я получаю, я получить разные записи, два из них, один с рынком uri для поиска приложения, с одним только именем пакета java (я думаю). До этого я просто использовал enableForegroundNdefPush ... и т. Д., Поэтому без обратного вызова, но так как мне нужно, чтобы обработать завершение моего нажатия на обоих устройствах. Я изменил его таким образом, и теперь он не правильно передавать мои данные.

EDIT:

Для того, чтобы лучше понять:

public class NfcActivity extends Activity implements CreateNdefMessageCallback, 
    OnNdefPushCompleteCallback { 
// ================================ Member ================================= 
// Speichert den NFC Adapter. 
private NfcAdapter mNfcAdapter = null; 
private boolean retryChannel = false; 

public static final String TAG = "NfcActivity"; 
private static final String DATA_SPLITTER = "__:DATA:__"; 
private static final String MIME_TYPE = "application/my.applications.mimetype"; 

private String ADDRESS = null; 
private String USER_NAME = null; 

// Speichert das Intent, welches von Android mit erkannten Tags gefüllt 
// und an die Activity weitergeleitet wird. 
private PendingIntent mNfcPendingIntent = null; 

private static final String LOG_TAG = "NfcActivity"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ADDRESS = "Something"; 

    USER_NAME = "Someone"; 

    // Zugriff auf den NFC Adapter erhalten. 
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 

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

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

@Override 
protected void onNewIntent(Intent intent) { 
    if (!NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) { 
     return; 
    } 

    // Nutzdaten des Intent auslesen. 
    Parcelable[] rawMsgs = intent 
      .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); 
    if (rawMsgs == null) { 
     return; 
    } 

    for (int i = 0; i < rawMsgs.length; ++i) { 
     NdefMessage msg = (NdefMessage) rawMsgs[i]; 
     NdefRecord[] records = msg.getRecords(); 
     for (NdefRecord record : records) { 
      String text = new String(record.getPayload()); 
          // this one has the wrong content. I receive two records, one for the market, one with java packet name (or something) 
     } 
    } 
} 

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

      // activate receiving of messages 
    mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, null, 
      null); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    // deactivate receiving 
    mNfcAdapter.disableForegroundDispatch(this); 
} 


private void logError(String msg) { 
    Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); 
    Log.v(LOG_TAG, msg); 
} 

@Override 
public void onNdefPushComplete(NfcEvent arg0) { 
    Log.d(TAG, "Push complete"); 
} 

@Override 
public NdefMessage createNdefMessage(NfcEvent event) { 
    Log.d(TAG, "Creating massage"); 

      String text = "asdfgh"; 

    // Nachricht vorbereiten. 
    // String text = "Hello NFC World!"; 
    NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, 
      MIME_TYPE.getBytes(), new byte[0], text.getBytes()); 
    NdefRecord[] records = { record }; 
    NdefMessage msg = new NdefMessage(records); 
    return msg; 
} 

} 
+0

, что вы определили для своей деятельности внутри вашего манифеста? –

+0

<намеренных фильтр> \t \t \t <действие андроид: имя = "android.nfc.action.NDEF_DISCOVERED" /> \t \t \t <категория андроид: имя = "android.intent.category.DEFAULT" /> \t \t \t \t \t damian

+0

Хм, я вижу. текст/равнину. Так может быть, поэтому я не получаю свои NdefMessages? – damian

ответ

1

Я отправляю этот ответ для вас, что вы можете принять его.

Решение было неправильным Mime-Type внутри Android-манифеста. Он должен быть таким же, как NdefRecord определяет

Смежные вопросы