2012-11-04 5 views
2

나는 이것이 간단하다고 확신하지만 그것을 이해할 수는 없습니다. 제가하려는 것은 NFC를 통해 메시지를 보내는 것입니다. 코드를 주 활동으로 보내고 있다면 완벽하게 작동하지만 다른 활동으로 보내는 방법을 모르겠습니다. 나는 Android 개발자 페이지에서 NFC와 Intent Filter 기사를 살펴 보았지만 정확하게 수행하는 방법을 잘 모르겠습니다. 나는 그것을 NFC 활동에 보내려고 노력 중이며, 아래에 내 매니페스트와 NFC 클래스를 게시 할 것입니다.NFC 인 텐트 필터 - 메시지가 아닌 활동 보내기

매니페스트 :

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.justbaumdev.tagsense" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="16" 
     android:targetSdkVersion="15" /> 

    <uses-permission android:name="android.permission.NFC" /> 

    <uses-feature android:name="android.hardware.nfc" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@android:style/Theme.Holo" > 
     <activity 
      android:name=".TagSense" 
      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.justbaumdev.tagsense.NFC" android:exported="false"> 
      <intent-filter> 
       <action android:name="android.nfc.action.NDEF_DISCOVERED" /> 

       <category android:name="android.intent.category.DEFAULT" /> 

       <data android:mimeType="application/com.justbaumdev.tagsense" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

NFC 클래스 : 당신의 도움에 대한

package com.justbaumdev.tagsense; 

import org.json.JSONArray; 
import org.json.JSONException; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.net.wifi.WifiManager; 
import android.nfc.NdefMessage; 
import android.nfc.NdefRecord; 
import android.nfc.NfcAdapter; 
import android.nfc.NfcAdapter.CreateNdefMessageCallback; 
import android.nfc.NfcAdapter.OnNdefPushCompleteCallback; 
import android.nfc.NfcEvent; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.os.Parcelable; 
import android.widget.Toast; 

public class NFC extends Activity implements CreateNdefMessageCallback, OnNdefPushCompleteCallback { 
    private NfcAdapter mNfcAdapter; 
    private static final int MESSAGE_SENT = 1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.nfc); 
     mNfcAdapter = NfcAdapter.getDefaultAdapter(this); // Check for available NFC Adapter 
     if (mNfcAdapter == null) 
     { 
      Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show(); 
      finish(); 
      return; 
     } 
     else 
     { 
      mNfcAdapter.setNdefPushMessageCallback(this, this); // Register callback to set NDEF message 
      mNfcAdapter.setOnNdefPushCompleteCallback(this, this); // Register callback to listen for message-sent success 
     } 
    } 


    @Override 
    public void onResume() { 
     super.onResume(); 
     // Check to see that the Activity started due to an Android Beam 
     if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) { 
      processIntent(getIntent()); 
     } 
    } 


    @Override 
    public void onNewIntent(Intent intent) { 
     // onResume gets called after this to handle the intent 
     setIntent(intent); 
    } 


    @Override 
    public NdefMessage createNdefMessage(NfcEvent event) { 
     WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
     String mac = wm.getConnectionInfo().getMacAddress(); 
     String newMac = mac.substring(0, 2); 
     mac = mac.substring(2); 
     int hex = Integer.parseInt(newMac, 16) + 0x2; 
     newMac = Integer.toHexString(hex); 
     String text = newMac + mac; 

     NdefMessage msg = new NdefMessage(NdefRecord.createMime(
       "application/com.justbaumdev.tagsense", text.getBytes())); 
     return msg; 
    } 


    /** 
    * Implementation for the OnNdefPushCompleteCallback interface 
    */ 
    @Override 
    public void onNdefPushComplete(NfcEvent arg0) { 
     // A handler is needed to send messages to the activity when this 
     // callback occurs, because it happens from a binder thread 
     mHandler.obtainMessage(MESSAGE_SENT).sendToTarget(); 
    } 


    /** This handler receives a message from onNdefPushComplete */ 
    private final Handler mHandler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
      switch (msg.what) { 
      case MESSAGE_SENT: 
       Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show(); 
       break; 
      } 
     } 
    }; 


    /** 
    * Parses the NDEF Message from the intent and prints to the TextView 
    */ 
    //TODO Currently overwrites any previously saved mac addresses. Get FB ID as value. Auto end activity. 
    void processIntent(Intent intent) { 
     Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); 
     // only one message sent during the beam 
     NdefMessage msg = (NdefMessage) rawMsgs[0]; 
     // record 0 contains the MIME type, record 1 is the AAR, if present 
     //textView.setText(new String(msg.getRecords()[0].getPayload())); 
     String payload = new String(msg.getRecords()[0].getPayload()); 
     Toast.makeText(this, new String(msg.getRecords()[0].getPayload()), Toast.LENGTH_LONG).show(); 

     SharedPreferences appData = getSharedPreferences("appData", Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = appData.edit(); 
     String addresses = appData.getString("mac_address", null); 
     if(addresses==null) 
     { 
      JSONArray addressArray = new JSONArray(); 
      addressArray.put(payload); 
      addresses = addressArray.toString(); 
     } 
     else 
     { 
      try { 
       if(!addresses.contains(payload)) 
       { 
        JSONArray addressArray = new JSONArray(addresses); 
        addressArray.put(payload); 
        addresses = addressArray.toString(); 
       } 
      } catch (JSONException e) { 
       Toast.makeText(this, "Error adding new friend. Please try again.", Toast.LENGTH_SHORT).show(); 
      } 
     } 
     editor.putString("mac_address", addresses); 
     editor.commit(); 
    } 
} 

감사합니다.

+0

귀하의 목표 SDK 버전이 최소 SDK 버전보다 낮습니다. –

답변

관련 문제