Data From Serial Port keeps Buffering (Android Device cannot recognize it as a Data)

A question regarding the above title
Why does my data transferred via serial port keeps on buffering? The android cannot recognize the data as a data. It cannot write the data received and transferred to the tag on the android device

This is the sendFileToTag function:

 public boolean sendFileToTag(Tag detectedTag, int sectorOrPage) {
        try {
            // Give the exact filePath
            String filePath = "/storage/self/primary/Upload_in_NFC.txt";

            // Read the file data
            String fileData = readFileAsString(filePath);

            // Encode the file data into an NDEF message
            byte[] ndefData = encodeNdefData(fileData);

            // Construct the NFC_Write command
            byte[] nfcWriteCommand = new byte[]{
                    (byte) 0xAA,  // Start byte
                    0x00,         // Length (variable, depends on the data being sent)
                    0x2C,         // Command byte for NFC_Write (0x94)
                    (byte) sectorOrPage, // Sector/Page (replace this with the appropriate sector or page number)
                    (byte) (ndefData.length + 1), // Length of NDEF data
                    0x01          // Start of NDEF data
                    // NDEF data goes here
                    // You may need to convert the fileData to bytes and place it here
            };

            // Write the NFC_Write command to the tag
            byte[] responseData = sendNFCCommand(detectedTag, nfcWriteCommand);

            // Check the response for errors
            if (responseData != null && responseData.length > 0 && responseData[0] == (byte) 0x00) {
                // Write successful
                byte[] cardNumber = Arrays.copyOfRange(responseData, 1, responseData.length);
                System.out.println("File data written to NFC tag");
                System.out.println("Card Number: " + asciiToHex(new String(cardNumber, StandardCharsets.US_ASCII)));
                return true;
            } else {
                // Handle the case where there was an error in the response
                showToast("NFC Write Error");
            }
        } catch (IOException e) {
            // Handle file reading error
            e.printStackTrace();
        }
        return false;
    }

This is the serial port configuration:

   // Find and open the USB serial port
        UsbSerialPort serialPort = findSerialPort(); // Implement the method to find the appropriate serial port

        try {
            // Open the serial port and configure parameters
            serialPort.open(usbManager.openDevice(serialPort.getDriver().getDevice()));
            // Configure parameters
            serialPort.setParameters(9600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);

            // Create a buffer to read the data
            byte[] buffer = new byte[1024];
            int totalBytesRead = 0;
            int MAX_DATA_SIZE = 1024; // Define your maximum data size here


            // Read data from the serial port
            while (true) {
                int bytesRead = serialPort.read(buffer, 1000); // Adjust the timeout value as needed

                // Convert the received bytes to a string
                if (bytesRead > 0) {
                    // Append the received bytes to the data
                    dataFromSerial += new String(buffer, 0, bytesRead);
                    totalBytesRead += bytesRead;


                    // Check for termination condition or end of transmission
                    if (dataFromSerial.contains("terminator")) {
                        break; // Exit the loop if the terminator is found
                    }

                    // Check for maximum data size or other conditions
                    if (totalBytesRead >= MAX_DATA_SIZE) {
                        break; // Exit the loop if maximum data size is reached
                    }
                } else {
                    // No more data to read
                    break;
                }
            }

This is the writeDataToFile:

   public void writeDataToFile(File directory, NdefMessage ndefMessage) {
        try {
            if (directory != null) {
                File file = new File(directory, "nfc_data.txt");
                try (FileOutputStream fos = new FileOutputStream(file)) {
                    // Process each NDEF record
                    if (ndefMessage != null) {
                        NdefRecord[] records = ndefMessage.getRecords();
                        for (NdefRecord record : records) {
                            // Assuming the record contains text data
                            if (record.getTnf() == NdefRecord.TNF_WELL_KNOWN &&
                                    Arrays.equals(record.getType(), NdefRecord.RTD_TEXT)) {
                                // Decode the payload data
                                String dataFromNdef = new String(record.getPayload(), StandardCharsets.UTF_8);
                                // Write the NDEF data to the file
                                fos.write(("NDEF Data: " + dataFromNdef + "n").getBytes(StandardCharsets.UTF_8));
                            }
                        }
                    }
                    Log.d("FileIO", "NFC Data written to file: " + file.getAbsolutePath());
                } catch (IOException e) {
                    Log.e("FileIO", "Error writing to file: " + e.getMessage());
                }
            } else {
                Log.e("FileIO", "Directory is null, cannot write to file.");
            }
        } catch (Exception e) {
            Log.e("FileIO", "Error: " + e.getMessage());
        }
    }

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật