Android phone calls NFC function

Some time ago, because the project needs to use Android to realize the reading of nfc chips, I will share with you:
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="Please put the back of the phone close to the label for identification" />
</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();
			}
		});
		// Get the handle of GifView from xml
//		GifView gf1 = (GifView) this.findViewById(R.id.nfcGif);
		// Set the Gif image source
//		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);

		// Call the finish() method directly, the resultCode is 0
		if (resultCode != RESULT_OK)
		{
			return;
		}

		switch (requestCode)
		{
			case NFC:
				JavaScriptObject.nfcCode = data.getStringExtra("nfcCode");

				h.post(new Runnable()
				{
					public void run()
					{
						// Call the setBarCode method in js
						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()
					{
						// Call the setBarCode method in js
						browser.loadUrl("javascript:setCameraFileName('" + JavaScriptObject.fileName + "','" + JavaScriptObject.camera_type + "')");
					}
				});

				break;

			case BAR_CODE:
				Bundle bundle = data.getExtras();
				// display the scanned content
				JavaScriptObject.barCode = bundle.getString("barCode");

				h.post(new Runnable()
				{
					public void run()
					{
						// Call the setBarCode method in js
						browser.loadUrl("javascript:setBarCode('" + JavaScriptObject.barCode + "')");
					}
				});
				break;
		}
	}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326871514&siteId=291194637