2012-04-13 3 views
-1

NFCデバイスからデータを取得するNFCベースのアプリケーションを開発しています。しかし、NFC対応のAndroidデバイスをNFCデバイスの隣に置くと、すべてのNFCベースのアプリケーションがリストされたダイアログボックスが表示され、そのうちの1つも私のものです。リストからアプリケーションアイコンをクリックすると、自分のアプリからのアクティビティ(NfcActivityという名前)が開き、Android Phone にデータ転送を開始します。は不要です。そのアクティビティには、更新とキャンセルの2つのボタンがあります。 私は更新ボタンをクリックしてNFCデバイスを起動し、プログレスバー付きの電話機にデータ転送を開始します。データが完全に転送されると、進行状況バーが自動的に閉じられます。私は同じことをすることができません、あなたは同じことに関する任意の提案があれば私を示唆してください。ボタンをクリックすると、NFCデバイスからデータ転送が開始されます

ありがとうございます。

NfcActivity:

package com.a1technology.impak; 


import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.math.BigInteger; 
import java.nio.ByteBuffer; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.PendingIntent; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.ServiceConnection; 
import android.nfc.NfcAdapter; 
import android.nfc.tech.IsoDep; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.util.Log; 
import android.view.View; 
import android.widget.Toast; 

import com.a1technology.impak.db.DBAdapter; 
import com.cypak.mobile.bridge.android.CypakDevice; 
import com.cypak.mobile.bridge.android.CypakDeviceListener; 
import com.cypak.mobile.bridge.android.NfcTransportService; 
import com.cypak.mobile.bridge.android.TransportBinder; 



public class NfcActivity extends Activity implements CypakDeviceListener { 

    private boolean update = false; 

    private String TAG = "NfcExampleActivity"; 

    List<EventByteGetSet> eventList = new ArrayList<EventByteGetSet>(); 

    // Required for foreground dispatch 
    private String[][] techListsArray = new String[][] { new String[] { IsoDep.class.getName() } }; 

    private NfcAdapter mAdapter; 

    private PendingIntent pendingIntent; 

    // Required when binding to NfcTransportService 
    private NfcTransportService mService; 

    private boolean bound = false; 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.nfc_activity); 

     findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       finish(); 
      } 
     }); 
     findViewById(R.id.update_button).setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       update = true; 
      } 
     }); 
     // Required for foreground dispatch 
     pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), NfcActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 
     mAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext());// Connecting 
               // to 
               // the 
               // adapter 

     resolveIntent(getIntent()); 

     Intent intent = new Intent(this, NfcTransportService.class); 
     // Bind to the service 
     if (!bindService(intent, mConnection, Context.BIND_AUTO_CREATE)) { 
      Log.e(TAG, "Failed to bind service"); 
     } 
    } 


    @Override 
    protected void onPause() { 

     super.onPause(); 
     // finish(); 
     // Required for foreground dispatch 
     mAdapter.disableForegroundDispatch(this); 

    } 


    @Override 
    protected void onDestroy() { 

     super.onDestroy(); 

     if (bound) { 
      unbindService(mConnection); 
      bound = false; 
     } 
    } 


    @Override 
    protected void onResume() { 

     super.onResume(); 

     // Required for foreground dispatch 
     mAdapter.enableForegroundDispatch(this, pendingIntent, null, techListsArray); 
    } 


    @Override 
    protected void onNewIntent(Intent intent) { 

     super.onNewIntent(intent); 
     setIntent(intent); 
     resolveIntent(intent); 
    } 


    private void resolveIntent(Intent intent) { 

     if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())// need 
               // to 
               // find 
               // what 
               // action 
               // is 
               // coming 
      && mService != null) { 
      // Let NfcTransportService handle the Nfc Intent 
      try { 
       mService.handleIntent(intent); 
      } 
      catch (IOException e) { 
       Log.w(TAG, "Failed to detect device", e); 
      } 

     } 
    } 

    /** 
    * Defines callbacks for service binding, passed to bindService(). Only 
    * needed if binding to MyMaxTransportService 
    * */ 
    private ServiceConnection mConnection = new ServiceConnection() { 

     @Override 
     public void onServiceConnected(ComponentName className, IBinder service) { 

      // We've bound to NfcTransportService, cast the IBinder and 
      // get NfcTransportService instance 
      Log.v(TAG, "Service connected"); 
      TransportBinder binder = (TransportBinder) service; 
      mService = (NfcTransportService) binder.getService(); 
      bound = true; 

      mService.registerDeviceListener(NfcActivity.this); 
     } 


     @Override 
     public void onServiceDisconnected(ComponentName name) { 

      Log.v(TAG, "Service disconnected"); 
      bound = false; 
     } 
    }; 


    @Override 
    public void deviceDiscovered(CypakDevice device) { 

     // String hex = "1"; 
     // byte[] byteClear= hex.getBytes(); 
     // 

     if (!update) { 
      return; 
     } 
     else 
      try { 
       // byte[] byteData= device.sendAppCommand((byte) 
       // 0x11, byteClear); 
       NFCDataHandler mNfcDataHandler = new NFCDataHandler(this); 
       byte[] byteStatusData = device.sendAppCommand((byte) 0x00, new byte[0]); 
       String stateHCC = mNfcDataHandler.getStatus(byteStatusData); 
       if (stateHCC.equalsIgnoreCase("04")) { 
        byte[] byteQueLogData = device.sendAppCommand((byte) 0x05, new byte[0]); 
        if (byteQueLogData.length < 6) { 
         ShowMessage("Warning", "Card is Empty"); 
         return; 
        } 
        mNfcDataHandler.getQueLog(byteQueLogData); 
       } 
       else { 
        ShowMessage("Warning", "Card is not Active"); 
        return; 

       } 

       Log.v("Value", ""); 
      } 
      catch (com.cypak.mobile.bridge.android.AppCommandErrorCodeException e) { 
       Log.e(TAG, "AppCommandErrorCodeException, error code " + e.getErrorCode(), e); 
      } 
      catch (com.cypak.mobile.bridge.android.AppCommandException e) { 
       Log.e(TAG, "AppCommandException", e); 
      } 
      catch (IllegalArgumentException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     // Toast.makeText(getApplicationContext(), "Device found! " + 
     // device, Toast.LENGTH_LONG).show(); 
    } 


    @Override 
    public void invalidDeviceDiscovered(CypakDevice device) { 

     Toast.makeText(getApplicationContext(), "Invalid device!", Toast.LENGTH_SHORT).show(); 
    } 


    @Override 
    public void deviceLost() { 

     Toast.makeText(getApplicationContext(), "Device lost!", Toast.LENGTH_SHORT).show(); 
    } 


    protected Date dateFromlongBuffer(long bb) { 

     return new Date((long) 1000 * (long) bb); 
    } 


    protected Date dateFromByteBuffer(ByteBuffer bb) { 

     return new Date((long) 1000 * (long) bb.getInt()); 
    } 

    static final byte[] HEX_CHAR_TABLE = { (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) 'a', (byte) 'b', 
      (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f' }; 


    public static String getHexString(byte[] raw) throws UnsupportedEncodingException { 

     byte[] hex = new byte[2 * raw.length]; 
     int index = 0; 

     for (byte b : raw) { 
      int v = b & 0xFF; 
      hex[index++] = HEX_CHAR_TABLE[v >>> 4]; 
      hex[index++] = HEX_CHAR_TABLE[v & 0xF]; 
     } 
     return new String(hex, "ASCII"); 
    } 


    public static int hex2decimal(String s) { 

     String digits = "ABCDEF"; 
     s = s.toUpperCase(); 
     int val = 0; 
     for (int i = 0; i < s.length(); i++) { 
      char c = s.charAt(i); 
      int d = digits.indexOf(c); 
      val = 16 * val + d; 
     } 
     return val; 
    } 


    private void ShowMessage(String title, String message) { 

     AlertDialog.Builder b = new AlertDialog.Builder(this); 
     AlertDialog a = b.create(); 
     a.setTitle(title); 
     a.setMessage(message); 
     a.setButton("Ok", new DialogInterface.OnClickListener() { 

      public void onClick(DialogInterface arg0, int arg1) { 

       finish(); 
      } 
     }); 

     a.show(); 
    } 
} 

答えて

0

あなたはonCreate()resolveIntent()を呼んでいるように見えます。アプリ選択ツールからアプリを選択すると、NFC発見IntentonCreate()を経由してActivityに渡されており、mService.handleIntent(intent);に電話するとすぐに処理していると思います。アプリがNFCディスカバリから起動するたびに、すぐにインテントをServiceに渡します。あなたの質問から、あなたがそれを受け取ったときにIntentのNFCデータを取得したいと思うように思えます。それを保存し、更新ボタンが押されたときにあなたのサービスに電話してください。

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.nfc_activity); 

    findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      finish(); 
     } 
    }); 
    findViewById(R.id.update_button).setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      update = true; 
     } 
    }); 
    // Required for foreground dispatch 
    pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), NfcActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 
    mAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext());// Connecting 
              // to 
              // the 
              // adapter 
    // HERE, this could be the Intent from the NFC Discovery 
    **resolveIntent(getIntent());** 

    Intent intent = new Intent(this, NfcTransportService.class); 
    // Bind to the service 
    if (!bindService(intent, mConnection, Context.BIND_AUTO_CREATE)) { 
     Log.e(TAG, "Failed to bind service"); 
    } 
} 

ので、あなたは最終的にはすぐにサービスをオフに意図NFCを渡している。

private void resolveIntent(Intent intent) { 

    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())// need 
              // to 
              // find 
              // what 
              // action 
              // is 
              // coming 
     && mService != null) { 
     // Let NfcTransportService handle the Nfc Intent 
     try { 
      // HERE: you are handling the NFC Intent via onCreate() 
      mService.handleIntent(intent); 
     } 
     catch (IOException e) { 
      Log.w(TAG, "Failed to detect device", e); 
     } 

    } 
} 

は、ユーザーが自分のコールバックメソッド、deviceDiscovered()前ボタンをクリックするまで待ちたくないと呼ばれています?

+0

あなたの答えを具体化できますか? –

+0

サービスコードを含める必要がありますか? – sethro

+0

どのサービスコードですか? –