Android Studi安卓读写NDEF智能海报源码

本示例使用的发卡器:https://item.taobao.com/item.htm?id=615391857885&spm=a1z10.5-c.w4002-21818769070.11.1f60789ey1EsPH

XML 复制代码
<?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>
java 复制代码
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);
        }
    }
}
相关推荐
The_Ticker3 分钟前
CFD平台如何接入实时行情源
java·大数据·数据库·人工智能·算法·区块链·软件工程
大数据编程之光25 分钟前
Flink Standalone集群模式安装部署全攻略
java·大数据·开发语言·面试·flink
诸神黄昏EX29 分钟前
Android 分区相关介绍
android
爪哇学长39 分钟前
双指针算法详解:原理、应用场景及代码示例
java·数据结构·算法
ExiFengs42 分钟前
实际项目Java1.8流处理, Optional常见用法
java·开发语言·spring
paj12345678944 分钟前
JDK1.8新增特性
java·开发语言
繁依Fanyi1 小时前
简易安卓句分器实现
java·服务器·开发语言·算法·eclipse
慧都小妮子1 小时前
Spire.PDF for .NET【页面设置】演示:打开 PDF 时自动显示书签或缩略图
java·pdf·.net
m51271 小时前
LinuxC语言
java·服务器·前端
IU宝1 小时前
C/C++内存管理
java·c语言·c++