NFC - 初学者实施

时间:2018-06-01 15:43:19

标签: android nfc

也许你可以帮助我。我需要开发一个发送"简单值的应用程序" (可以是你好世界,我不介意)只需按一下按钮即可在Android设备上通过NFC。 我正在使用android studio开发。我阅读了NFC指南并实施了我认为必要的方法。现在,在我的Android设备上进行测试时,按下按钮时应用程序崩溃了。

我无法弄清问题是什么。

这是两个类的代码:

主要课程:

package com.example.bernd.myapplicationsecurityapplication;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.nio.charset.Charset;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.TextView;
import android.widget.Toast;
import java.nio.charset.Charset;

public class MainActivity extends AppCompatActivity {

    NFCMessager MainMessager = new NFCMessager();

    AlertDialog.Builder alert;

    Button button1;

    public void onCreate(Bundle savedInstanceState) {
        alert = new AlertDialog.Builder(MainActivity.this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(myhandler2);
    }

    View.OnClickListener myhandler2 = new View.OnClickListener() {
        public void onClick(View v) {
            NdefMessage helloworld = new NdefMessage(MainMessager.createTextRecord("helloworld", Locale.US, true));
            MainMessager.sendRecord(helloworld);
            AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
            alertDialog.setTitle("Info");
            alertDialog.setMessage("The NFC message was sent successfully");
            alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
            alertDialog.show();
        }
    };
}

Class" NFCMessager":

package com.example.bernd.myapplicationsecurityapplication;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.nio.charset.Charset;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.TextView;
import android.widget.Toast;
import java.nio.charset.Charset;

public class NFCMessager extends AppCompatActivity implements CreateNdefMessageCallback {

    NfcAdapter mNfcAdapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Check for available NFC Adapter
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter == null) {
            Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
        // Register callback
        mNfcAdapter.setNdefPushMessageCallback(this, this);

    }

    public NdefRecord createTextRecord(String payload, Locale locale, boolean encodeInUtf8) {
        byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));
        Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
        byte[] textBytes = payload.getBytes(utfEncoding);
        int utfBit = encodeInUtf8 ? 0 : (1 << 7);
        char status = (char) (utfBit + langBytes.length);
        byte[] data = new byte[1 + langBytes.length + textBytes.length];
        data[0] = (byte) status;
        System.arraycopy(langBytes, 0, data, 1, langBytes.length);
        System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);
        NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
                NdefRecord.RTD_TEXT, new byte[0], data);
        return record;
    }

    public void sendRecord(NdefMessage helloworld) {
        mNfcAdapter.setNdefPushMessage(helloworld,this, this);
    }

    //This override must be done otherwise the compiler wants to have the class abstract, which means I can't instantiate it anymore

    @Override
    public NdefMessage createNdefMessage(NfcEvent nfcEvent) {
    try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

}

关于&#34;覆盖&#34;的简短故事 - 为什么有必要:

在初始完成时(这是一个类中的所有方法),编译器给了我这个错误:

  

错误:MainActivity不是抽象的,不会覆盖抽象   CreateNdefMessageCallback中的方法createNdefMessage(NfcEvent)

我无法以令人满意的方式解决它。我已将受影响的方法移动到另一个类&#34; NFCMessager&#34;,但它希望为createNdefMessage创建一个@override。

我不知道这是不是导致坠机的原因。

任何输入都表示赞赏...如果你觉得我的编程很糟糕,我也可以接受。

问候

0 个答案:

没有答案