本示例使用的发卡器:Android Linux RFID读写器NFC发卡器WEB可编程NDEF文本/网址/海报-淘宝网 (taobao.com)
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 NdefwifiActivity extends AppCompatActivity {private TextView tv;private EditText ctrwifiname;private Spinner ctrauth;private Spinner ctrkeyalgorithm;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_ndef_wifi);androidx.appcompat.widget.Toolbar toolbar=findViewById(R.id.toolbar);setSupportActionBar(toolbar);tv = findViewById(R.id.sample_text);tv.setText("操作结果");ctrauth=findViewById(R.id.spin_Selauthtype);ctrauth.setSelection(7);ctrkeyalgorithm=findViewById(R.id.spin_Selkeytype);ctrkeyalgorithm.setSelection(4);ctrwifiname=findViewById(R.id.Edittextname);ctrwifiname.requestFocus();}@Overridepublic void onBackPressed(){super.onBackPressed();finish();}@Overridepublic 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 writewifitag(View view) {String wifinamestr=ctrwifiname.getText().toString().trim();if (wifinamestr.length()<1){tv.setText("请输入网络名称!");ctrwifiname.requestFocus();return;}byte[] wifinamebyte=wifinamestr.getBytes();int wifinamelen=wifinamebyte.length;int authtype=(int)(ctrauth.getSelectedItemId());int keytype=(int)(ctrkeyalgorithm.getSelectedItemId());EditText ctrwifikey=findViewById(R.id.Edittextkey);String wifikeystr=ctrwifikey.getText().toString().trim();byte[] wifikeybyte=wifikeystr.getBytes();int keylen=wifikeybyte.length;ourmifare.tagbufclear(); //清空数据缓冲byte status=ourmifare.tagbufaddwifi(wifinamebyte,wifinamelen,authtype,keytype,wifikeybyte,keylen); //将要写的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);}}
}
<?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=".NdefwifiActivity"><androidx.appcompat.widget.Toolbarandroid: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"><TextViewandroid: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" /><TextViewandroid:id="@+id/TextViewlabelDisp"android:layout_width="match_parent"android:layout_height="match_parent"android:text="NDEF_WIFI "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><TextViewandroid: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"/><ScrollViewandroid: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.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="3dp" ><TextViewandroid:id="@+id/textlablename"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" /><EditTextandroid:id="@+id/Edittextname"android:layout_width="330dp"android:layout_height="wrap_content"android:textSize="16sp"android:gravity="left"android:text=""app:layout_constraintBottom_toBottomOf="@+id/textlablename"app:layout_constraintLeft_toRightOf="@+id/textlablename"app:layout_constraintTop_toTopOf="@+id/textlablename" /><TextViewandroid:id="@+id/textlableauthtype"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/textlablename" /><Spinnerandroid:id="@+id/spin_Selauthtype"android:layout_width="330dp"android:layout_height="wrap_content"android:entries="@array/wifiauthtype"android:theme="@style/my_spinner_style"app:layout_constraintBottom_toBottomOf="@+id/textlableauthtype"app:layout_constraintLeft_toRightOf="@+id/textlableauthtype"app:layout_constraintTop_toTopOf="@+id/textlableauthtype" /><TextViewandroid:id="@+id/textlablekeytype"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/textlableauthtype" /><Spinnerandroid:id="@+id/spin_Selkeytype"android:layout_width="330dp"android:layout_height="wrap_content"android:entries="@array/wifikeytype"android:theme="@style/my_spinner_style"app:layout_constraintBottom_toBottomOf="@+id/textlablekeytype"app:layout_constraintLeft_toRightOf="@+id/textlablekeytype"app:layout_constraintTop_toTopOf="@+id/textlablekeytype" /><TextViewandroid:id="@+id/textlableahostkey"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/textlablekeytype" /><EditTextandroid:id="@+id/Edittextkey"android:layout_width="330dp"android:layout_height="wrap_content"android:textSize="16sp"android:gravity="left"android:text=""android:inputType="textPassword"app:layout_constraintBottom_toBottomOf="@+id/textlableahostkey"app:layout_constraintLeft_toRightOf="@+id/textlableahostkey"app:layout_constraintTop_toTopOf="@+id/textlableahostkey" /><Buttonandroid:id="@+id/butt_Writewifitag"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginTop="10dp"android:onClick="writewifitag"android:text="将以上WIFI连接信息写入标签"android:textSize="14sp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/textlableahostkey" /></androidx.constraintlayout.widget.ConstraintLayout></ScrollView></androidx.constraintlayout.widget.ConstraintLayout>