Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
skupekA committed Dec 9, 2022
0 parents commit cd8fb57
Show file tree
Hide file tree
Showing 38 changed files with 17,611 additions and 0 deletions.
21 changes: 21 additions & 0 deletions BoardConfigNfc.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2018 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

LOCAL_STEM := nfc/BoardConfigNfcPartial.mk

#BOARD_KERNEL_CMDLINE += androidboot.selinux=permissive

DEVICE_MANIFEST_FILE += vendor/nxp/nfc/manifest.xml

-include vendor/nxp/$(LOCAL_STEM)
16 changes: 16 additions & 0 deletions T4TDemo/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

# Only compile source java files in this apk.
LOCAL_SRC_FILES := $(call all-java-files-under, src)

LOCAL_PACKAGE_NAME := T4TDemo
LOCAL_PRIVATE_PLATFORM_APIS := true
LOCAL_CERTIFICATE := platform
LOCAL_USES_LIBRARIES := com.nxp.nfc
LOCAL_JAVA_LIBRARIES := com.nxp.nfc
LOCAL_STATIC_JAVA_LIBRARIES += android-support-v7-appcompat

include $(BUILD_PACKAGE)
28 changes: 28 additions & 0 deletions T4TDemo/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nxp.t4tdemo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="29"
android:targetSdkVersion="29" />
<uses-permission android:name="android.permission.NFC"></uses-permission>

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name="com.nxp.t4tdemo.T4tActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.nxp.nfc"
android:required="true"/>
</application>

</manifest>
Binary file added T4TDemo/res/drawable-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added T4TDemo/res/drawable-hdpi/nxp_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added T4TDemo/res/drawable-ldpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added T4TDemo/res/drawable-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 99 additions & 0 deletions T4TDemo/res/layout/main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<LinearLayout
android:layout_width="500dip"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|clip_horizontal"
android:orientation="vertical" >

<TextView
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Status:"
android:textSize="16sp"
android:layout_gravity="left" />

<TextView
android:id="@+id/txt_status"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_gravity="left" />


<TextView
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Note: This demo app only supports RTD_TXT type"
android:textSize="12sp"
android:layout_gravity="left" />

<EditText
android:id="@+id/edt_content"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:textColor="@android:color/black"
android:inputType="textMultiLine"
android:lines="8"
android:minLines="6"
android:gravity="top|left"
android:maxLines="10"
android:hint="Please enter text to write" />


<LinearLayout
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:layout_width="300dip"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
android:id="@+id/btn_write"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="T4T Write" />

<Button
android:id="@+id/btn_read"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="T4T Read" />

<Button
android:id="@+id/btn_reset"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="reset" />

</LinearLayout>

</LinearLayout>


<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:src="@drawable/nxp_logo"
android:layout_gravity="bottom|right"
/>

</FrameLayout>
5 changes: 5 additions & 0 deletions T4TDemo/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">T4T Demo</string>

</resources>
148 changes: 148 additions & 0 deletions T4TDemo/src/com/nxp/t4tdemo/T4TFromNfccReaderWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright 2021 NXP
*
* All rights are reserved. Reproduction in whole or in part is
* prohibited without the written consent of the copyright owner.
* NXP reserves the right to make changes without notice at any time.
* NXP makes no warranty, expressed, implied or statutory, including but
* not limited to any implied warranty of merchantability or fitness for any
* particular purpose, or that the use will not infringe any third party patent,
* copyright or trademark. NXP must not be liable for any loss or damage
* arising from its use.
*
*/

package com.nxp.t4tdemo;

import android.app.Activity;
import android.content.Context;
import android.nfc.NfcAdapter;
import android.nfc.FormatException;
import com.nxp.nfc.NxpNfcAdapter;
import com.nxp.t4tdemo.Utils;
import android.nfc.NdefRecord;
import android.util.Log;
import java.util.Arrays;
import java.io.UnsupportedEncodingException;

public class T4TFromNfccReaderWriter {
private static String TAG = "T4TFromNfccReaderWriter";

private Context mContext;
private NfcAdapter mNfcAdapter;
private NxpNfcAdapter mNxpNfcAdapter;
private OnActionCompleteListener mOnActionCompleteListener;

public T4TFromNfccReaderWriter(Context cntx) {
this.mContext = cntx;
this.mNfcAdapter = NfcAdapter.getDefaultAdapter(cntx);
this.mNxpNfcAdapter = NxpNfcAdapter.getNxpNfcAdapter(mNfcAdapter);
Log.e(TAG, "adapter loaded");
}

public void setActionListener(OnActionCompleteListener onActionCompleteListener){
this.mOnActionCompleteListener = onActionCompleteListener;
}

static byte[] hexStringToBytes(String s) {
if (s == null || s.length() == 0)
return null;
int len = s.length();
if (len % 2 != 0) {
s = '0' + s;
len++;
}
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] =
(byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
}
return data;
}

public static void printLongAPDU(String str) {
if (str.length() > 4000) {
Log.i(TAG, "APDU + : " + str.substring(0, 4000));
printLongAPDU(str.substring(4000));
} else
Log.i(TAG, "APDU : " + str);
}

public void writeT4T(String content){

try{
byte[] fileId = { (byte)0xE1, (byte)0x04};
final NdefRecord ndefRecord = NdefRecord.createTextRecord("en-us",content);
byte[] tbytes = ndefRecord.toByteArray();

Log.d(TAG, "NdefRecord getByte :" + Utils.toHexString(tbytes, 0, tbytes.length));
Thread thrd = new Thread(new Runnable() {
@Override
public void run() {
byte[] bytes = ndefRecord.toByteArray();
Log.d(TAG, "NdefRecord getByte :" + Utils.toHexString(bytes, 0, bytes.length));
int retValue = mNxpNfcAdapter.doWriteT4tData(fileId, bytes, bytes.length);
Log.d(TAG, "T4T NDEF Write API Return Value :" + retValue);
String status;
if(retValue>=0){
status = "T4T Write is success, doWriteT4tData API retunes : "+retValue;
}else{
status = "T4T Write is failed, doWriteT4tData API retunes : "+retValue;
}
mOnActionCompleteListener.OnT4tWriteComplete(status);
}
});
thrd.start();
}catch(IllegalArgumentException exception){
mOnActionCompleteListener.OnT4tWriteComplete("UnsupportedEncodingException");
}
}

public void readT4T(){

byte[] fileId = { (byte)0xE1, (byte)0x04};

Thread thrd = new Thread(new Runnable() {
@Override
public void run() {
byte[] rData = mNxpNfcAdapter.doReadT4tData(fileId);
if (rData != null) {
try{
NdefRecord record = new NdefRecord(rData);
byte[] payloadData = record.getPayload();
if(payloadData.length>0 && record.getTnf() == NdefRecord. TNF_WELL_KNOWN && Arrays.equals(NdefRecord.RTD_TEXT,record.getType())){
int languageCodeLenth = payloadData[0];
byte[] languageCode = new byte[languageCodeLenth];
System.arraycopy(payloadData, 1, languageCode, 0, languageCodeLenth);
int contentLength = payloadData.length-languageCodeLenth-1;
byte[] contentBytes = new byte[contentLength];
System.arraycopy(payloadData, languageCodeLenth+1, contentBytes, 0, contentLength);
String content = new String(contentBytes,"UTF-8");

mOnActionCompleteListener.OnT4TReadComplete(content,"Read success");
Log.d(TAG, "T4T NDEF Read API Return Value : " + content);
}else{
mOnActionCompleteListener.OnT4TReadComplete("","Unable to read content");
Log.d(TAG, "T4T NDEF Read API unable to read");
}
} catch(FormatException exception){
mOnActionCompleteListener.OnT4tWriteComplete("Failed due to FormatException");
} catch(UnsupportedEncodingException exception){
mOnActionCompleteListener.OnT4tWriteComplete("Failed due to UnsupportedEncodingException");
}
}else{
byte[] errorCode = {(byte) 0x64, (byte) 0xFF};
Log.d(TAG, "T4T NDEF Read API Return Value : " + Utils.toHexString(errorCode, 0, errorCode.length));
mOnActionCompleteListener.OnT4TReadComplete("","Read failed");
}
}
});
thrd.start();
}

interface OnActionCompleteListener{
public void OnT4TReadComplete(String content,String status);
public void OnT4tWriteComplete(String status);
}

}
Loading

0 comments on commit cd8fb57

Please sign in to comment.