无法通过NFC发送NDEF消息

时间:2018-10-24 07:32:51

标签: android nfc

我想通过NFC从2个android设备发送/接收消息。但这对我不起作用。

当我接近两部手机时,似乎没有呼叫 createNdefMessage ,并且消息也没有传输。 当发送/接收两个设备屏幕都打开时。 应用程序正在运行,甚至我也可以触摸设备的屏幕!

我尝试了多种方法来解决此问题,但对我来说没有结果! 我在哪里犯错了?


<uses-permission android:name="android.permission.NFC" />

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

 <activity android:name=".NFCActivity"
            android:label="@string/app_name"
            android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>

public class NFCActivity extends AppCompatActivity
        implements NfcAdapter.OnNdefPushCompleteCallback,
        NfcAdapter.CreateNdefMessageCallback {


    private NfcAdapter mNfcAdapter;

    private ArrayList<String> messagesToSendArray = new ArrayList<>();
    private ArrayList<String> messagesReceivedArray = new ArrayList<>();


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

        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter != null && mNfcAdapter.isEnabled()) {
            mNfcAdapter.setNdefPushMessageCallback(this, this);

            mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
        } else {
            Toast.makeText(this, "NFC not available on this device",
                    Toast.LENGTH_SHORT).show();
        }
    }


    public void addMessage(View view) {
        String newMessage = txtBoxAddMessage.getText().toString();
        messagesToSendArray.add(newMessage);
        txtBoxAddMessage.setText(null);
        updateTextViews();
    }


    @Override
    public NdefMessage createNdefMessage(NfcEvent event) {
        if (messagesToSendArray.size() == 0) 
            return null;

        NdefRecord[] recordsToAttach = createRecords();
        return new NdefMessage(recordsToAttach);
    }


    public NdefRecord[] createRecords() {
        NdefRecord[] records = new NdefRecord[messagesToSendArray.size() + 1];
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            for (int i = 0; i < messagesToSendArray.size(); i++) {
                byte[] payload = messagesToSendArray.get(i).getBytes(Charset.forName("UTF-8"));
                NdefRecord record = new NdefRecord(
                        NdefRecord.TNF_WELL_KNOWN,      
                        "http://developer.android.com/index.html".getBytes(Charset.forName("UTF-8")),            
                        new byte[0],                    
                        payload);                    

                records[i] = record;
            }
        } else {
            for (int i = 0; i < messagesToSendArray.size(); i++) {
                byte[] payload = messagesToSendArray.get(i).
                        getBytes(Charset.forName("UTF-8"));

                NdefRecord record = NdefRecord.createMime("application/vnd.com.example.android.beam", payload);
                records[i] = record;
            }
        }
        records[messagesToSendArray.size()] = NdefRecord.createApplicationRecord(getPackageName());

        return records;
    }


    @Override
    public void onNdefPushComplete(NfcEvent event) {
        messagesToSendArray.clear();
    }

    private void handleNfcIntent(Intent intent) {

        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
            Parcelable[] receivedArray = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

            if (receivedArray != null) {

                updateTextViews();
            } else {
                Log.i(TAG, "Received Blank Parcel");
            }
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        setIntent(intent);
        handleNfcIntent(intent);
    }

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

        updateTextViews();
        handleNfcIntent(getIntent());
    }
}

0 个答案:

没有答案