0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Android】NFCのUIDを読み込んでTextViewに表示するプログラム

Last updated at Posted at 2019-09-01
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/test001"
        android:layout_width="wrap_content"
        android:layout_height="18dp"
        android:text="@string/test002"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:onClick="clearId"
        android:text="クリア"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/test001" />

</android.support.constraint.ConstraintLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.XXXXX.mynfc">

    <uses-feature android:name="android.hardware.nfc" android:required="true"/>
    <uses-permission android:name="android.permission.NFC"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
MainActivity.java
package com.example.XXXXX.mynfc;

import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

    private NfcAdapter mNfcAdapter; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ボタンを設定
        Button button = findViewById(R.id.button2);

        // インスタンスを取得
        mNfcAdapter = android.nfc.NfcAdapter.getDefaultAdapter(this);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // NFCかざし設定
        Intent intent = new Intent(this,this.getClass());

        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,intent, 0);

        mNfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null );
    }

    @Override
    protected void onPause() {

        super.onPause();

        mNfcAdapter.disableForegroundDispatch(this); //ディスパッチを無効化する
    }

    @Override
    protected void onNewIntent(Intent intent){
        super.onNewIntent(intent);

        byte[] uid = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID); // NFCのUID

        Toast.makeText(this, Arrays.toString(uid), Toast.LENGTH_SHORT).show(); // 表示

        StringBuilder tagId = new StringBuilder("ID(" + uid.length + "bytes):");

        for (int i=0; i<uid.length; i++){
             tagId.append(String.format("%02x",uid[i] & 0xff));
        }

        Toast.makeText(this, tagId, Toast.LENGTH_SHORT).show();  // 表示 その2

        TextView tv = (TextView)findViewById(R.id.test001);

        tv.setText(tagId);
    }

    public void clearId(View view) {
        TextView tv = (TextView) findViewById(R.id.test001);
        tv.setText("");
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?