我无法在React Native BLE管理器中写入数据,而相同的功能适用于Android

问题描述 投票:0回答:1
 **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);
        });
};

这是日志 这是我的函数的输出,其中包括我正在编写的数据以及

    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

我尝试在另一个应用程序上设置 mtu 大小,但没有成功,而且这个应用程序的代码足够小,可以编写,当我看到 Hercules 中的日志时,在写入时什么也没有给出 我已经验证了可能导致此问题的所有可能性,但是,我的扫描、连接、读取、通知功能在 IOS 中工作正常,只有我遇到问题的写入功能也同样的功能在 Android 中工作完美

react-native bluetooth-lowenergy react-native-ble-manager
1个回答
0
投票

有一些可能的原因可能导致您无法在 React Native BLE Manager 中写入数据,而相同的功能适用于 Android。 一种可能是您没有请求正确的权限。为了将数据写入BLE设备,您需要具有ACCESS_COARSE_LOCATION权限。您可以使用 PermissionsAndroid.request() 方法在 React Native 应用程序中请求此权限。 另一种可能是您没有正确连接到 BLE 设备。确保您正在调用 BleManager.connect() 方法并且传入正确的设备 UUID。 最后,React Native BLE Manager 库也可能存在错误。如果您已尝试上述所有方法,但仍然无法将数据写入 BLE 设备,您可以尝试向库的维护人员报告该错误。

您可以尝试以下一些其他操作:-

  1. 确保您的蓝牙已打开。
  2. 确保您的位置已开启。
  3. 尝试重新启动您的 React Native 应用程序。
  4. 尝试重新启动您的 BLE 设备。
  5. 尝试从不同的 React Native 应用程序连接到 BLE 设备。
© www.soinside.com 2019 - 2024. All rights reserved.