在android中添加RFID功能

问题描述 投票:0回答:1

在android studio中是否需要使用任何特定协议才能使用RFID阅读器读取NFC标签?读写器和NFC标签都具有高频(13.5 MHz) 我已经使用了检测 NFC 的正常协议。但是,当扫描标签时,RFID 读取器不会检测到 NFC 标签。 Android应用程序是否有任何特定协议可以在Android应用程序中使用RFID阅读器?

android nfc rfid
1个回答
0
投票

按要求。这是我的代码:

@Override
    protected void onResume() {
        super.onResume();
        // Enable NFC when the activity is in the foreground
        nfcManager.enableNFC(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // Disable NFC when the activity goes into the background
        nfcManager.disableNFC(this);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopSerialIoManager();
        if (serialPort != null) {
            try {
                serialPort.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            serialPort = null;
        }
    }

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

        String action = intent.getAction();

        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
            Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

            // Retrieve the list of technologies supported by the tag
            String[] techList = tag.getTechList();

            // Check if app supports any of the desired technologies
            if (supportsDesiredTechnology(techList)) {
                // App supports at least one of the desired technologies
                // Implement logic here to handle the supported technology

                // Create or obtain the directory
                File directory = createFile();

                if (directory != null) {
                    // Read data from the NFC tag using the Ndef technology
                    String dataFromNFC = getDataFromNFC(tag);

                    if (!dataFromNFC.isEmpty()) {
                        // Calculate the MD5 hash of the data
                        String md5Hash = calculateMD5(dataFromNFC);

                        // Write data and MD5 hash to the file
                        writeDataToFile(directory, md5Hash);

                        // Display a message or perform actions based on the data read
                        Toast.makeText(this, "NFC Data: " + dataFromNFC, Toast.LENGTH_SHORT).show();
                        Toast.makeText(this, "MD5 Hash: " + md5Hash, Toast.LENGTH_SHORT).show();
                    } else {
                        // Handle the case where no NFC data was read
                        Toast.makeText(this, "No NFC data read.", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    // Handle the case where directory is null
                    Log.e("FileIO", "Directory is null, cannot write to file.");
                }
            } else {
                // Your app does not support any of the desired technologies
                // Handle this situation (e.g., display a message)
                handleUnsupportedTechnology();
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.