Данные из последовательного порта продолжают буферизоваться (устройство Android не может распознать их как данные)Android

Форум для тех, кто программирует под Android
Ответить Пред. темаСлед. тема
Anonymous
 Данные из последовательного порта продолжают буферизоваться (устройство Android не может распознать их как данные)

Сообщение Anonymous »

Вопрос относительно заголовка выше
Почему мои данные, передаваемые через последовательный порт, продолжают буферизоваться? Андроид не может распознать данные как данные. Он не может записать полученные и переданные данные в тег на устройстве Android.
Это функция sendFileToTag:
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;
}

Это конфигурация последовательного порта:
// 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;
}
}

Это файл 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());
}
}


Подробнее здесь: https://stackoverflow.com/questions/784 ... -it-as-a-d
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Android»