I'm unable to write data in react native ble manager while same function is working for android

136 views Asked by At

here is my code in BLE Helper that i am calling into my settings Screen

    write(data, serviceId, characteristicId) {
            return new Promise((resolve, reject) => {
                BleManager.retrieveServices(this.peripheralId)
                    .then(() => {
                        console.log("writing data --->: ", data);
                        // data = this.addProtocol(data);
                        // console.log("writing data  toString: ", data.toString());
    
                        BleManager.writeWithoutResponse(this.peripheralId, serviceId, characteristicId, data, data.length)
                            .then(() => {
                                // console.log('--stringToByte--------------------', this.stringToByte(data))
                                console.log('Write success in helper: ', data.toString());
                                resolve();
                            })
                            .catch((error) => {
                                console.log('Write  failed: ', error);
                                reject(error);
                            });
                    })
                    .catch((error) => {
                        console.log('Write  failed: ', error);
                        reject(error);
                    })
            });
        }

Here is my code where in my settings screen where i am calling function

const writecharacteristics = async (serviceUUID1, characteristicUUID1) => {
    // Assuming you have minimum1 and maximum1 defined as follows
    // const minimum1 = 16.2;
    // const maximum1 = 42.9;
    setWritingData(true);
    setTimeout(() => {
        setWritingData(false);
    }, 2000);
    // Convert minimum1 and maximum1 to their respective integer values
    // const minimum1Integer = Math.round(minimum1 * 10);
    // console.log(minimum1Integer);
    // const maximum1Integer = Math.round(maximum1 * 10);

    // Convert the integer values to hexadecimal strings
    const minimum1Hex = minimum1.toString(16);
    const maximum1Hex = maximum1.toString(16);
    // console.log(minimum1Hex);
    // Combine the two hexadecimal strings
    const editedData = minimum1Hex + maximum1Hex;

    // Add 36 at the beginning and 35 at the end
    const finalData = '24' + editedData + '23';

    // console.log('editedData-------------->', finalData);

    // Convert the hexadecimal string to bytes
    const editedDataBytes = [];
    for (let i = 0; i < finalData.length; i += 2) {
        const byteValue = parseInt(finalData.substr(i, 2), 16);
        editedDataBytes.push(byteValue);
    }

    console.log('editedDataBytes for Auto  height-------------->', editedDataBytes);

    await BluetoothManager.write(editedDataBytes, serviceUUID1, characteristicUUID1)
        .then((written) => {
            // console.log('written ------------------->', written);
        })
        .catch((error) => {
            console.log('Error writing data:', error);
        });
};

Here are the logs

this is the output of my functions which includes the data that i am writing and also the Logs or Output for the functions

LOG  editedDataBytes for Auto  height--------------> [36, 22, 44, 35]
LOG < ------- This is what i am writing------>
LOG < ------- this.peripheralId------> 9a **************
LOG < ------- serviceId------> c ****************
LOG < ------- characteristicId------> c1a ***************
LOG < ------- data------> [36, 22, 44, 35]
LOG < ------- data.length, ------> 4
LOG < ------- This is what i am writing------>
LOG < ------- This is what i am writing------>
LOG  Write success in helper: 36, 22, 44, 35

I tried setting the mtu size to on another app but did not worked and it this app the code is small enough to be written also when i see logs in Hercules in gives nothing while writing I have verified all the possibilities that can be the cause of this problem but , my scan, connect, read, notify function is working fine in IOS just the write function where I am facing the problem also the same function is working perfect in android

0

There are 0 answers