0

I am trying to read the larger text file from BLE device using react-native-ble-plx library of react native, I am able to read/fetch only end of the file and few line in between the file. I am copying the code that I am trying.PLease suggest me, how to read the entire file content.

--Output file-

Reading file

const executeCommandTest = (device: Device, command: any) => {

    console.log('entered executeCommandTest')
    return new Promise((resolve, reject) => {
        bleManager.discoverAllServicesAndCharacteristicsForDevice(device.id)
            .then(() => {
                const writeCharacteristicUUID = 'writeCharacteristicID';
                //let commandbtoa = btoa(command);
                let commandbtoa = encode(command);

                bleManager.writeCharacteristicWithResponseForDevice(device.id, '00001234-0000-1000-8000-00805f9b34fb', writeCharacteristicUUID, commandbtoa)
                    .then(() => {
                        const readCharacteristicUUID = 'read characteristicsID';

                        let responseAccumulator = '';

                        const readResponse = () => {


                            bleManager.requestMTUForDevice(device.id, 512)
                                .then((mtu) => {
                                    console.log('MTU size updated:');
                                    // MTU size successfully updated, proceed with other operations

                                    bleManager.readCharacteristicForDevice(device.id, 'serviceID', readCharacteristicUUID)
                                        .then(async (response) => {
                                            if (response.value != null) {
                                                // const receivedData = atob(response.value);
                                                const receivedData = decode(response.value);


                                                responseAccumulator += receivedData;
                                                console.log('response : ', receivedData);

                                                // Check if the end of file is reached
                                                if (receivedData.includes('End of Files')) {
                                                    console.log('END OF FILE')
                                                    resolve(responseAccumulator);
                                                } else {
                                                    // Continue rea ding the response
                                                    console.log('Continue reading the response')
                                                    // await sleep(0.01);
                                                    // readResponse();
                                                    setTimeout(readResponse, 0.1);
                                                }
                                            } else {
                                                reject(new Error('No response data'));
                                            }
                                        })
                                        .catch((error) => {
                                            console.error('Failed to read response:', error);
                                            reject(error);
                                        });
                                })
                                .catch((error) => {
                                    console.error('Failed to update MTU size:', error);
                                    // Handle the error accordingly
                                });
                        };
                        console.log('Start reading the response')
                        // Start reading the response
                        readResponse();
                    })
                    .catch((error) => {
                        console.error('Failed to write command:', error);
                        reject(error);
                    });
            })
            .catch((error) => {
                console.error('Failed to discover services and characteristics:', error);
                reject(error);
            });
    });
};

I am calling the readResonse() method after some delay to read continuously.

I am attaching the text file that I am trying to read and the output I am getting after reading the file.

I tried adding the delay and looing the readResponse() method, but it still not working.

0 Answers0