本示例使用的发卡器:https://item.taobao.com/item.htm?id=615391857885&spm=a1z10.5-c.w4002-21818769070.11.1f60789ey1EsPH
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="3dp"
tools:context=".NdefURLActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="?attr/colorPrimary"
app:navigationIcon="@drawable/baseline_arrow_back_ios_24"
app:titleTextColor="@color/white"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteY="0dp">
<TextView
android:id="@+id/TextViewlabelDispleft"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="返回"
android:textColor="@color/white"
android:textSize="16sp"
android:gravity="center"
android:onClick="retmain" />
<TextView
android:id="@+id/TextViewlabelDisp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="NDEF_URL "
android:textColor="@color/white"
android:textSize="16sp"
android:gravity="center_horizontal|right|center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/TextViewlabelDispleft"
app:layout_constraintTop_toTopOf="parent" />
</androidx.appcompat.widget.Toolbar>
<TextView
android:id="@+id/sample_text"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:padding="3dp"
android:text="操作结果"
android:textSize="12sp"
android:background="@drawable/shape4border"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<ScrollView
android:id="@+id/scrollViewIC"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintBottom_toTopOf="@+id/sample_text"
android:scrollbars="horizontal"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp" >
<TextView
android:id="@+id/textlabletitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="标题:"
android:textSize="16sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/Edittexttitle"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:textSize="16sp"
android:gravity="left"
android:hint="百度"
app:layout_constraintBottom_toBottomOf="@+id/textlabletitle"
app:layout_constraintLeft_toRightOf="@+id/textlabletitle"
app:layout_constraintTop_toTopOf="@+id/textlabletitle" />
<TextView
android:id="@+id/textlableprefix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="前缀:"
android:textSize="16sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textlabletitle" />
<Spinner
android:id="@+id/spin_Selurlprefix"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:entries="@array/NdefUrlPrefix"
android:theme="@style/my_spinner_style"
app:layout_constraintBottom_toBottomOf="@+id/textlableprefix"
app:layout_constraintLeft_toRightOf="@+id/textlableprefix"
app:layout_constraintTop_toTopOf="@+id/textlableprefix" />
<EditText
android:id="@+id/edittexturl"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginLeft="38dp"
android:layout_marginTop="10dp"
android:background="@drawable/shape4border"
android:gravity="left"
android:inputType="textCapCharacters"
android:maxLines="8"
android:hint="baidu.com"
android:textSize="12sp"
app:layout_constraintLeft_toRightOf="@+id/textlableprefix"
app:layout_constraintTop_toBottomOf="@+id/spin_Selurlprefix" />
<Button
android:id="@+id/butt_writeurltag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:text="将以上URL写入标签"
android:textSize="14sp"
android:onClick="writeurltag"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edittexturl" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.usbreadertest;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.reader.ourmifare;
public class NdefURLActivity extends AppCompatActivity {
private TextView tv;
private Spinner ctrselefix;
private EditText ctrtitle;
private EditText ctrurlinf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ndef_url);
androidx.appcompat.widget.Toolbar toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tv = findViewById(R.id.sample_text);
tv.setText("操作结果");
ctrselefix=findViewById(R.id.spin_Selurlprefix);
ctrselefix.setSelection(2);
ctrurlinf=findViewById(R.id.edittexturl);
ctrurlinf.requestFocus();
ctrtitle=findViewById(R.id.Edittexttitle);
}
@Override
public void onBackPressed(){
super.onBackPressed();
finish();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==android.R.id.home){
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public void retmain(View view)
{
finish();
}
public void writeurltag(View view) throws Exception {
String languagecodestr="en";
byte[] languagecodebyte= languagecodestr.getBytes("gb2312");
int languagecodestrlen=languagecodebyte.length;
String urltstr=ctrurlinf.getText().toString().trim();
if(urltstr.length()<1){
tv.setText("请输入要写入的网址!");
ctrurlinf.requestFocus();
return;
}
byte[] urlbyte=urltstr.getBytes();
int urllen=urlbyte.length;
String titletstr=ctrtitle.getText().toString().trim();
byte[] titlebyte=titletstr.getBytes();
int titlelen=titlebyte.length;
byte urlheaderindex = (byte)(ctrselefix.getSelectedItemId());
ourmifare.tagbufclear(); //先清空NDEF数据缓冲
byte status=ourmifare.tagbufadduri(languagecodebyte,languagecodestrlen,titlebyte,titlelen,urlheaderindex,urlbyte,urllen); //生成NDEF数据缓冲
if (status==0){
int tagtype=NdefTextActivity.CheckCardType(); //检测发卡器上标签类型
if(tagtype==-1){
tv.setText("发卡器感应区未发现有效的NDEF标签!");
return;
}
byte[] mypiccserial=new byte[8];
status=NdefTextActivity.WriteTag(tagtype,mypiccserial); //将已生成的NDEF数据写入不同的标签内
String dispinfo=NdefTextActivity.dispwritetag(status ,mypiccserial,tagtype);
tv.setText(dispinfo);
}
}
}
源码下载:AndroidstudioRFIDNFC读写源码资源-CSDN文库