2012-07-13 5 views
0

다시 한 번 물어봐서 안타깝게도 태그에 데이터를 쓰려면 Android 앱을 만들고 태그를 인식하고 AAR을 사용하여 자동으로 열 수 있지만 데이터를 쓸 수는 없습니다 파일에 EdiText 열을 채우고 버튼을 클릭 한 후 layout.xml 파일에서 onclick 액션을 선언하거나 onclicklistener를 사용하여 여러 가지 방법을 시도했지만 아무도 작동하지 않으므로 아무에게도 문제가있는 곳을 보시오. 다음내 NFC 작성 앱이 태그에 쓸 수 없음

public class Writer extends Activity{ 

NfcAdapter mAdapter; 
PendingIntent mPendingIntent; 
IntentFilter mWriteTagFilters[]; 
boolean mWriteMode; 
Tag detectedTag; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_writer); 
    Button btn = (Button) findViewById(R.id.button); 
    btn.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       SetTag(); 
      } 
     }); 
    mAdapter = NfcAdapter.getDefaultAdapter(this); 
    mPendingIntent = PendingIntent.getActivity(this, 0, 
      new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); 
    mWriteTagFilters = new IntentFilter[] { tagDetected }; 
    //Intent intent = getIntent(); 

} 

private void enableTagWriteMode(){ 
    mWriteMode = true; 
    mAdapter.enableForegroundDispatch(this, mPendingIntent, mWriteTagFilters, null); 
} 

private void disableTagWriteMode(){ 
    mWriteMode = false; 
    mAdapter.disableForegroundDispatch(this); 
} 

public void SetTag(){ 
    EditText editText1 = (EditText) findViewById(R.id.edit_message1); 
    EditText editText2 = (EditText) findViewById(R.id.edit_message2); 
    String message1 = editText1.getText().toString(); 
    String message2 = editText2.getText().toString(); 
    byte[] textBytes1 = message1.getBytes(); 
    byte[] textBytes2 = message2.getBytes(); 
    NdefRecord textRecord1 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, 
      NdefRecord.RTD_TEXT, new byte[]{}, textBytes1); 
    NdefRecord textRecord2 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, 
      NdefRecord.RTD_TEXT, new byte[]{}, textBytes2); 
    NdefMessage mNdefMessage = new NdefMessage(
     new NdefRecord[]{ 
       textRecord1, 
       textRecord2, 
       NdefRecord.createApplicationRecord("android.reader") 
     } 
    ); 
    writeTag(mNdefMessage, detectedTag);  
} 

public static void writeTag(NdefMessage message, Tag tag){ 
    int size = message.toByteArray().length; 
    try { 
     Ndef ndef = Ndef.get(tag); 
     if (ndef != null){ 
      ndef.connect(); 
      if (ndef.isWritable() && ndef.getMaxSize() > size) 
       ndef.writeNdefMessage(message); 
      ndef.close(); 
     }else{ 
      NdefFormatable format = NdefFormatable.get(tag); 
      if (format != null) { 
       try { 
        format.connect(); 
        format.format(message); 
       }catch(IOException e){ 

       } 
      } 
     } 
    }catch(Exception e){ 

    } 
} 

@Override 
protected void onNewIntent(Intent intent){ 
    if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) 
     detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);   
} 

@Override 
public void onPause(){ 
    super.onPause(); 
    disableTagWriteMode(); 
} 

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

/*@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    EditText editText1 = (EditText) findViewById(R.id.edit_message1); 
    EditText editText2 = (EditText) findViewById(R.id.edit_message2); 
    String message1 = editText1.getText().toString(); 
    String message2 = editText2.getText().toString(); 
    byte[] textBytes1 = message1.getBytes(); 
    byte[] textBytes2 = message2.getBytes(); 
    NdefRecord textRecord1 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, 
      message1.getBytes(), new byte[]{}, textBytes1); 
    NdefRecord textRecord2 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, 
      message2.getBytes(), new byte[]{}, textBytes2); 
    NdefMessage mNdefMessage = new NdefMessage(
     new NdefRecord[]{ 
       textRecord1, 
       textRecord2, 
       NdefRecord.createApplicationRecord("android.reader") 
     } 
    ); 
    writeTag(mNdefMessage, detectedTag); 
}*/ 


} 

layot ​​파일입니다

<EditText android:id="@+id/edit_message1" 
    android:layout_weight="2" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:hint="@string/edit_message1" /> 

<EditText android:id="@+id/edit_message2" 
    android:layout_weight="2" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:hint="@string/edit_message2" /> 

<Button android:id="@+id/button" 
    android:layout_weight="1" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:text="@string/button_send" /> 

</LinearLayout> 

최근에 내 코드 만 실행할 수 있지만, 기기 의미를 다시 태그, 그것은이 열립니다 때 따라서, 태그에 데이터를 쓸 수 없습니다 내가 작성한 또 다른 읽기 태그 앱 대신 앱을 다시 사용하십시오.

답변

0

IntentFilter가 명확하게 일치하지 않으므로 ForegroundDispatch가 작동하지 않습니다. onCreate()tagDetected.addCategory(Intent.CATEGORY_DEFAULT);을 추가하십시오. 나는 그것이 트릭을 할 것이라고 생각한다.

+0

하지만 작동하지 않는 경우 태그가 감지 된 후 시작할 수있는 이유는 무엇입니까? 아니면이 단계는 전경 파견과 관련이 없습니까? 그래서 전혀 다시 시작하지 않기 때문에 쓰기 모드가 활성화되어 있지 않으므로 아무 것도 쓸 수 없습니까? 내 개념을 먼저 지우고 싶습니다. – Conrad

+0

좋은 질문입니다. 이것은 매니페스트 파일의 인 텐트 필터 때문에 시작되었습니다. Activity의 launchMode (예 : singleTop)에 따라, 결과 콜은'onCreate()','onNewIntent()'또는 아마도'onResume()'이 될 것입니다 (정확하게 모르겠습니다. 다양한 기능의 토스트 메시지 또는 logcat에 대한 로깅.) –

+0

ok, thx guy, 먼저 작업 흐름을 알아 내기 위해 몇 가지 메시지를 출력하려고합니다. – Conrad

관련 문제