当前位置: 代码迷 >> Android >> Android手机调用NFC效能
  详细解决方案

Android手机调用NFC效能

热度:209   发布时间:2016-04-24 11:10:52.0
Android手机调用NFC功能
前段时间因为项目需要使用Android实现了nfc芯片的读取工作,现跟大家分享一下:
nfc_reader.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"	android:orientation="vertical" android:layout_width="match_parent"	android:layout_height="match_parent" android:background="#fff">	<RelativeLayout android:layout_width="fill_parent"		android:layout_height="40dip" android:background="@drawable/title_bk">		<ImageButton android:id="@+id/btn_back"			android:layout_width="wrap_content" android:layout_height="wrap_content"			android:background="@drawable/btn_back_selector" android:src="@drawable/btn_back" />		<View android:id="@+id/line0" android:layout_width="1px"			android:layout_height="fill_parent" android:layout_toRightOf="@id/btn_back"			android:background="#444444" />		<View android:layout_width="1px" android:layout_height="fill_parent"			android:layout_toRightOf="@id/line0" android:background="#444444" />		<TextView android:layout_width="wrap_content"			android:layout_height="wrap_content" android:layout_centerInParent="true"			android:text="NFC卡扫描" android:textColor="#FFFFFF" android:textSize="20sp" />	</RelativeLayout><!-- 	<com.ant.liao.GifView android:id="@+id/nfcGif" --><!-- 		android:paddingTop="20dp" android:layout_height="wrap_content" --><!-- 		android:layout_width="wrap_content" android:layout_gravity="center" --><!-- 		android:enabled="false" /> -->	<TextView android:layout_height="match_parent"		android:gravity="center" android:layout_gravity="center"		android:layout_width="match_parent" android:paddingTop="50dp"		android:text="请将手机背面贴近标签进行识别" /></LinearLayout>

NfcActivity.java
package com.yzlt.wxaj;import android.app.Activity;import android.app.PendingIntent;import android.content.Intent;import android.nfc.NfcAdapter;import android.os.Bundle;import android.view.View;import android.widget.ImageButton;import com.ant.liao.GifView;public class NfcActivity extends Activity{	public static NfcActivity instance = null;	private PendingIntent mPendingIntent;	NfcAdapter nfcAdapter;	private ImageButton imageButton;	@Override	public void onCreate(Bundle paramBundle)	{		super.onCreate(paramBundle);		setContentView(R.layout.nfc_reader);		imageButton = (ImageButton) this.findViewById(R.id.btn_back);		imageButton.setOnClickListener(new View.OnClickListener()		{			@Override			public void onClick(View arg0)			{				finish();			}		});		// 从xml中得到GifView的句柄//		GifView gf1 = (GifView) this.findViewById(R.id.nfcGif);		// 设置Gif图片源//		gf1.setGifImage(R.drawable.nfc);		instance = this;		initNfcAdapter();	}	@Override	protected void onNewIntent(Intent paramIntent)	{		String str = getRFID(paramIntent);		Intent localIntent = new Intent(this, MainActivity.class);		localIntent.putExtra("nfcCode", str);		setResult(RESULT_OK, localIntent);		finish();	}	@Override	public void onPause()	{		super.onPause();		if (this.nfcAdapter != null)		{			this.nfcAdapter.disableForegroundDispatch(this);		}	}	@Override	public void onResume()	{		super.onResume();		if (this.nfcAdapter != null)		{			this.nfcAdapter.enableForegroundDispatch(this, this.mPendingIntent, null, null);		}	}	private long getDec(byte[] paramArrayOfByte)	{		long l1 = 0L;		long l2 = 1L;		for (int i = 0;; i++)		{			if (i >= paramArrayOfByte.length)				return l1;			l1 += l2 * (0xFF & paramArrayOfByte[i]);			l2 *= 256L;		}	}	public String getRFID(Intent paramIntent)	{		paramIntent.getAction();		String str = String.valueOf(getDec(paramIntent.getByteArrayExtra("android.nfc.extra.ID")));		return str;	}	public boolean initNfcAdapter()	{		try		{			this.nfcAdapter = NfcAdapter.getDefaultAdapter(this);			this.mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(536870912), 0);			return false;		}		catch (Exception e)		{			e.printStackTrace();		}		return true;	}}

MainActivity.java
@Override	protected void onActivityResult(int requestCode, int resultCode, Intent data)	{		super.onActivityResult(requestCode, resultCode, data);		// 直接调finish()方法,resultCode就是0		if (resultCode != RESULT_OK)		{			return;		}		switch (requestCode)		{			case NFC:				JavaScriptObject.nfcCode = data.getStringExtra("nfcCode");				h.post(new Runnable()				{					public void run()					{						// 调用js中的setBarCode方法						browser.loadUrl("javascript:setNfcCode('" + JavaScriptObject.nfcCode + "')");					}				});				break;			case CAMERA:				new PhotoTask().execute(WEB_URL, this.imageFilePath.getPath());				String filePath = this.imageFilePath.getPath();				JavaScriptObject.fileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length());				h.post(new Runnable()				{					public void run()					{						// 调用js中的setBarCode方法						browser.loadUrl("javascript:setCameraFileName('" + JavaScriptObject.fileName + "','" + JavaScriptObject.camera_type + "')");					}				});				break;			case BAR_CODE:				Bundle bundle = data.getExtras();				// 显示扫描到的内容				JavaScriptObject.barCode = bundle.getString("barCode");				h.post(new Runnable()				{					public void run()					{						// 调用js中的setBarCode方法						browser.loadUrl("javascript:setBarCode('" + JavaScriptObject.barCode + "')");					}				});				break;		}	}
  相关解决方案