amboji alur
amboji alur

Reputation: 429

Not able to write or send data to BLE hardware using react-native ble-manager[ write error status-3]

I am able to make a BLE connection with the hardware. By using service UUID and characteristics UUID I am able to receive data from the hardware through start notification function. But when I try to send data to the hardware its showing error [Write error status-3] as shown in the below code.



BleManager.retrieveServices(peripheral.id).then((peripheralInfo) => {
              console.log(peripheralInfo);

              var service = '6e400001-b5a3-f393-e0a9-e50e24dcca9e';
              var WriteCharacteristic = '6e400002-b5a3-f393-e0a9-e50e24dcca9e';
              var ReadCharacteristic = '6e400003-b5a3-f393-e0a9-e50e24dcca9e';

              setTimeout(() => {

                // receiving data from hardware 
                BleManager.startNotification(peripheral.id, service, ReadCharacteristic).then(() => {
                  console.log('Started notification on ' + peripheral.id);
                  setTimeout(() => {
                      // sending data to the hardware
                    BleManager.write(peripheral.id, service, WriteCharacteristic, [1,95]).then(() => {
                      console.log('Writed NORMAL crust');
                    });

                  }, 500);
                }).catch((error) => {
                  console.log('Notification error', error);
                });

First faced some problems with characteristics not found. After some changes, I am getting error as

Write error Status - 3

I am not able to find any solution for this error. Thank you in advance

Upvotes: 1

Views: 3207

Answers (1)

Rvind Jitta
Rvind Jitta

Reputation: 11

While using BleManager.write, you should first prepare your data

Data preparation:

If your data is not in byte array format you should convert it first. For strings you can use convert-string or other npm package in order to achieve that. Install the package first:

npm install convert-string

Then use it in your application:

// Import/require in the beginning of the file
import { stringToBytes } from "convert-string";
// Convert data to byte array before write/writeWithoutResponse
const data = stringToBytes(yourStringData);

In my case I converted my data to byte format, this way and you can do the same.

const dataByte = convertString.UTF8.stringToBytes(data);

Then used in my code

BleManager.retrieveServices(currentDevice.PID).then((peripheralInfo) => {
      console.log(peripheralInfo);
      BleManager.write(
        currentDevice.PID,
        '0000ffe0-0000-1000-8000-00805f9b34fb',
        '0000ffe1-0000-1000-8000-00805f9b34fb',
        dataByte,
      )
        .then(() => {
          console.log(`Sent ${dataByte}`);
        })
        .catch((error) => {
          console.log(error);
        });
    });
  };

Upvotes: 1

Related Questions