- 博客(0)
- 资源 (1)
空空如也
NFCDemo 近距离传输
// THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
// WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
// TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
// DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS
// ARISING FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS
// OF THE CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
package com.nfc.apps;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class NFCappsActivity extends Activity
{
private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;
private IntentFilter[] mFilters;
private String[][] mTechLists;
Button btnwww;
private ImageView imgScan;
private int drawableImageID;
private Timer rollImage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAdapter = NfcAdapter.getDefaultAdapter(this);
mPendingIntent = PendingIntent.getActivity(this, 0,new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
mFilters = new IntentFilter[] {ndef,};
mTechLists = new String[][] { new String[] { android.nfc.tech.NfcV.class.getName() } };
imgScan = (ImageView) findViewById(R.id.imgViewScan);
btnwww= (Button) this.findViewById(R.id.btnwww);
drawableImageID = R.drawable.wait1;
rollImage = new Timer();
rollImage.schedule(new TimerTask()
{
@Override
public void run()
{
TimerMethod();
}
}, 0, 500);
this.btnwww.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String url = "http://www.st.com/memories";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}
@Override
protected void onNewIntent(Intent intent)
{
// TODO Auto-generated method stub
super.onNewIntent(intent);
String action = intent.getAction();
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action))
{
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
DataDevice dataDevice = (DataDevice)getApplication();
dataDevice.setCurrentTag(tagFromIntent);
byte[] GetSystemInfoAnswer = NFCCommand.SendGetSystemInfoCommandCustom(tagFromIntent,(DataDevice)getApplication());
if(DecodeGetSystemInfoResponse(GetSystemInfoAnswer))
{
Intent intentScan = new Intent(this, Scan.class);
startActivity(intentScan);
}
else
{
return;
}
}
}
@Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
Log.v("NFCappsActivity.java", "ON RESUME NFC APPS ACTIVITY");
mPendingIntent = PendingIntent.getActivity(this, 0,new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);
}
@Override
protected void onPause()
{
// TODO Auto-generated method stub
Log.v("NFCappsActivity.java", "ON PAUSE NFC APPS ACTIVITY");
super.onPause();
mAdapter.disableForegroundDispatch(this);
return;
}
private void TimerMethod()
{
//This method is called directly by the timer
//and runs in the same thread as the timer.
//We call the method that will work with the UI
//through the runOnUiThread method.
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
@Override
public void run() {
if (drawableImageID==R.drawable.wait1)
{
drawableImageID = R.drawable.wait2;
} else if (drawableImageID==R.drawable.wait2)
{
drawableImageID = R.drawable.wait3;
} else if (drawableImageID==R.drawable.wait3)
{
drawableImageID = R.drawable.wait4;
} else if (drawableImageID==R.drawable.wait4)
{
drawableImageID = R.drawable.wait1;
}
imgScan.setImageDrawable(getResources().getDrawable(drawableImageID));
}
};
//***********************************************************************/
//* the function Decode the tag answer for the inventory command sent
//* the function fills the values (uid / manuf / techno / name /etc..)
//* in the myApplication class. return true if everything is ok.
//***********************************************************************/
public boolean DecodeInventoryResponse (byte[] InventoryResponse)
{
//if the tag has returned a god response
if(InventoryResponse[0] == (byte) 0x00)
{
DataDevice ma = (DataDevice)getApplication();
String uidToString = "";
byte[] uid = new byte[8];
// change uid format from byteArray to a String
for (int i = 1; i <= 8; i++)
{
uid[i - 1] = InventoryResponse[10 - i];
uidToString += Helper.ConvertHexByteToString(uid[i - 1]);
}
//***** TECHNO ******
ma.setUid(uidToString);
if(uid[0] == (byte) 0xE0)
ma.setTechno("ISO 15693");
else if (uid[0] == (byte) 0xE0)
ma.setTechno("ISO 14443");
else
ma.setTechno("unknow");
//***** MANUFACTURER ****
if(uid[1]== (byte) 0x02)
ma.setManufacturer("STMicroelectronics");
else if(uid[1]== (byte) 0x04)
ma.setManufacturer("NXP");
else if(uid[1]== (byte) 0x07)
ma.setManufacturer("Texas Instrument");
else
ma.setManufacturer("unknow");
//**** PRODUCT NAME *****
if(uid[2] >= (byte) 0x20 && uid[2] <= (byte) 0x23)
ma.setProductName("LRI2K");
else if(uid[2] >= (byte) 0x28 && uid[2] <= (byte) 0x2B)
ma.setProductName("LRIS2K");
else if(uid[2] >= (byte) 0x2C && uid[2] <= (byte) 0x2F)
ma.setProductName("M24LR64-R");
else if(uid[2] >= (byte) 0x40 && uid[2] <= (byte) 0x43)
ma.setProductName("LRI1K");
else if(uid[2] >= (byte) 0x44 && uid[2] <= (byte) 0x47)
ma.setProductName("LRIS64K");
else if(uid[2] == (byte) 0x4C)
ma.setProductName(" M24LR16E-R");
else
ma.setProductName("unknow");
return true;
}
//if the tag has returned an error code
else
{
return true;
}
}
//***********************************************************************/
//* the function Decode the tag answer for the GetSystemInfo command
//* the function fills the values (dsfid / afi / memory size / icRef /..)
//* in the myApplication class. return true if everything is ok.
//***********************************************************************/
public boolean DecodeGetSystemInfoResponse (byte[] GetSystemInfoResponse)
{
//if the tag has returned a god response
if(GetSystemInfoResponse[0] == (byte) 0x00 && GetSystemInfoResponse.length >= 12)
{
DataDevice ma = (DataDevice)getApplication();
String uidToString = "";
byte[] uid = new byte[8];
// change uid format from byteArray to a String
for (int i = 1; i <= 8; i++)
{
uid[i - 1] = GetSystemInfoResponse[10 - i];
uidToString += Helper.ConvertHexByteToString(uid[i - 1]);
}
//***** TECHNO ******
ma.setUid(uidToString);
if(uid[0] == (byte) 0xE0)
ma.setTechno("ISO 15693");
else if (uid[0] == (byte) 0xE0)
ma.setTechno("ISO 14443");
else
ma.setTechno("unknow");
//***** MANUFACTURER ****
if(uid[1]== (byte) 0x02)
ma.setManufacturer("STMicroelectronics");
else if(uid[1]== (byte) 0x04)
ma.setManufacturer("NXP");
else if(uid[1]== (byte) 0x07)
ma.setManufacturer("Texas Instrument");
else
ma.setManufacturer("unknow");
//**** PRODUCT NAME *****
if(uid[2] >= (byte) 0x20 && uid[2] <= (byte) 0x23)
ma.setProductName("LRI2K");
else if(uid[2] >= (byte) 0x28 && uid[2] <= (byte) 0x2B)
ma.setProductName("LRIS2K");
else if(uid[2] >= (byte) 0x2C && uid[2] <= (byte) 0x2F)
ma.setProductName("M24LR64-R");
else if(uid[2] >= (byte) 0x40 && uid[2] <= (byte) 0x43)
ma.setProductName("LRI1K");
else if(uid[2] >= (byte) 0x44 && uid[2] <= (byte) 0x47)
ma.setProductName("LRIS64K");
else if(uid[2] == (byte) 0x4C)
ma.setProductName("M24LR16E-R");
else
ma.setProductName("unknow");
//*** DSFID ***
ma.setDsfid(Helper.ConvertHexByteToString(GetSystemInfoResponse[10]));
//*** AFI ***
ma.setAfi(Helper.ConvertHexByteToString(GetSystemInfoResponse[11]));
//*** MEMORY SIZE ***
if(ma.isBasedOnTwoBytesAddress())
{
String temp = new String();
temp += Helper.ConvertHexByteToString(GetSystemInfoResponse[13]);
temp += Helper.ConvertHexByteToString(GetSystemInfoResponse[12]);
ma.setMemorySize(temp);
}
else
ma.setMemorySize(Helper.ConvertHexByteToString(GetSystemInfoResponse[12]));
//*** BLOCK SIZE ***
if(ma.isBasedOnTwoBytesAddress())
ma.setBlockSize(Helper.ConvertHexByteToString(GetSystemInfoResponse[14]));
else
ma.setBlockSize(Helper.ConvertHexByteToString(GetSystemInfoResponse[13]));
//*** IC REFERENCE ***
if(ma.isBasedOnTwoBytesAddress())
{
ma.setIcReference(Helper.ConvertHexByteToString(GetSystemInfoResponse[15]));
if(GetSystemInfoResponse[15] == (byte)0x5A)
ma.setProductName("M24LR04E-R");
}
else
{
ma.setIcReference(Helper.ConvertHexByteToString(GetSystemInfoResponse[14]));
if(GetSystemInfoResponse[14] == (byte)0x5A)
ma.setProductName("M24LR04E-R");
}
return true;
}
//if the tag has returned an error code
else
return true;
}
}
2012-03-06
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人