Android安卓写入蓝牙设备自动连接NDEF标签

77 篇文章 5 订阅
2 篇文章 0 订阅

 本示例所使用的发卡器: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.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.reader.ourmifare;

public class NdefbluetoothActivity extends AppCompatActivity {
    private TextView tv;
    private EditText ctrname;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ndef_bluetooth);

        androidx.appcompat.widget.Toolbar toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        tv = findViewById(R.id.sample_text);
        tv.setText("操作结果");

        ctrname=findViewById(R.id.Edittextname);
        ctrname.requestFocus();

    }

    @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 writebluetoothtag(View view)
    {
        String namestr=ctrname.getText().toString().trim();
        if(namestr.length()<1){
            tv.setText("请输入蓝牙设备名称!");
            ctrname.requestFocus();
            return;
        }
        byte[] btnamebyte=namestr.getBytes();
        int  btnamelen=btnamebyte.length;

        EditText ctrmac=findViewById(R.id.Edittextmacadd);
        String macstr=ctrmac.getText().toString().trim();
        String[] strArr=macstr.split(":");
        if (strArr.length !=6) {
            tv.setText("请输入正确的设备MAC地址!");
            ctrmac.requestFocus();
            return;
        }
        byte[] btmacbyte=new byte[6];
        for (int i=0;i<6;i++){
            btmacbyte[i]=(byte)(Integer.parseInt(strArr[i],16));
        }

        ourmifare.tagbufclear();  //清空数据缓冲
        byte status=ourmifare.tagbufaddbluetooth(btnamebyte,btnamelen,btmacbyte);  //将要写的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=".NdefbluetoothActivity">

    <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_蓝牙  "
            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/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" />

            <EditText
                android:id="@+id/Edittextname"
                android:layout_width="350dp"
                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" />

            <TextView
                android:id="@+id/textlablemacadd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="MAC地址:"
                android:textSize="16sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textlablename" />

            <EditText
                android:id="@+id/Edittextmacadd"
                android:layout_width="350dp"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:gravity="left"
                android:digits="0123456789ABCDEFabcdef:"
                android:maxLength="17"
                android:text=""
                android:hint="11:22:33:AA:BB:CC"
                app:layout_constraintBottom_toBottomOf="@+id/textlablemacadd"
                app:layout_constraintLeft_toRightOf="@+id/textlablemacadd"
                app:layout_constraintTop_toTopOf="@+id/textlablemacadd" />

            <Button
                android:id="@+id/butt_Writebluetoothtag"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="10dp"
                android:onClick="writebluetoothtag"
                android:text="将以上蓝牙连接信息写入标签"
                android:textSize="14sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textlablemacadd" />

        </androidx.constraintlayout.widget.ConstraintLayout>


    </ScrollView>


</androidx.constraintlayout.widget.ConstraintLayout>

源码下载:AndroidstudioRFIDNFC读写源码_安卓读写15693格式的nfc标签资源-CSDN文库

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android设备中,可以使用NFC Adapter进行NFC标签设备自动识别。以下是可能的代码实现: 1. 获取NFC Adapter: ``` NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); ``` 2. 在Activity中,重写onNewIntent()方法,以便在检测到新的NFC标签设备时进行处理: ``` @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { // 处理NFC标签被发现的情况 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); // 对标签进行读取或写入等操作 } else if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) { // 处理NFC设备被发现的情况 // 对设备进行读取或写入等操作 } } ``` 3. 在Activity中,重写onResume()方法和onPause()方法,以便在Activity处于前台时监听NFC事件: ``` @Override protected void onResume() { super.onResume(); IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); IntentFilter[] filters = new IntentFilter[] { tagDetected }; PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); nfcAdapter.enableForegroundDispatch(this, pendingIntent, filters, null); } @Override protected void onPause() { super.onPause(); nfcAdapter.disableForegroundDispatch(this); } ``` 以上代码实现了NFC标签设备自动识别,并提供了相应的操作。需要注意的是,NFC操作需要相应的权限和硬件支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vx_13822155058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值