chipweinberger / flutter_blue_plus

Flutter plugin for connecting and communicationg with Bluetooth Low Energy devices, on Android, iOS, macOS
Other
781 stars 471 forks source link

[Help]: Android: writeCharacteristic error GATT_INVALID_ATTRIBUTE_LENGTH but my peripheral device support 4 bytes. #472

Closed NewAge23 closed 1 year ago

NewAge23 commented 1 year ago

Ask your question

I/flutter (17980): Error writing data: FlutterBluePlusException: name:writeCharacteristic errorCode:13, errorString:GATT_INVALID_ATTRIBUTE_LENGTH

if i am sending one value its okay its send it, but if i send [0x00,0xff,0xff,0xff]this value is provide error please provide solution

chipweinberger commented 1 year ago
Screenshot 2023-08-04 at 3 24 36 AM
chipweinberger commented 1 year ago

your peripheral device does not support 4 bytes. It's too long.

Your peripheral device is probably improperly configured.

NewAge23 commented 1 year ago

Android: writeCharacteristic error GATT_INVALID_ATTRIBUTE_LENGTH but my peripheral device support 4 bytes. Also when the bluetooth device wa send the output in hterm also but now in updated plugin it does not work

chipweinberger commented 1 year ago

In updated FlutterBluePlus we check for Android errors. GATT_INVALID_ATTRIBUTE_LENGTH comes from Android.

https://developer.android.com/reference/android/bluetooth/BluetoothGatt#GATT_INVALID_ATTRIBUTE_LENGTH

It is unlikely to be a FlutterBluePlus problem. It is android or your device problem.

If you think your device is making a mistake, you can ignore the error (usually bad idea).

try {
   chr.write(...);
} catch(FlutterBluePlusException e) {
    if(e.errorString == "GATT_INVALID_ATTRIBUTE_LENGTH") {
        //ignore
    } else {
        rethrow;
    }
};
NewAge23 commented 1 year ago
 Future<void> _writeData() async {
    List<int> dataToWrite = [0x51,0x32];
    final int maxChunkSize =
        20; // Replace with your characteristic's maximum allowed length

    try {
      for (int i = 0; i < dataToWrite.length; i += maxChunkSize) {
        final chunk = dataToWrite.sublist(
            i,
            i + maxChunkSize > dataToWrite.length
                ? dataToWrite.length
                : i + maxChunkSize);

        // Add a delay if needed between chunks to avoid overloading the BLE stack.
        // await Future.delayed(Duration(milliseconds: 100));
      }

      setState(() {
        // Update the UI or state if needed after the write operation
      });

      print("Data written successfully: $dataToWrite");
    } catch (e) {
      print("Error writing data: $e");
      // Handle the error, show a snackbar, or display an error message to the user.
    }
  }

this is my code can you please correct my code for 4 bytes

chipweinberger commented 1 year ago
 Future<void> _writeData() async {
    List<int> dataToWrite = [0x51,0x32];
    final int maxChunkSize =
        20; // Replace with your characteristic's maximum allowed length

    try {
      for (int i = 0; i < dataToWrite.length; i += maxChunkSize) {
        final chunk = dataToWrite.sublist(
            i,
            i + maxChunkSize > dataToWrite.length
                ? dataToWrite.length
                : i + maxChunkSize);

        try {
            await widget.characteristic.write(chunk,
                withoutResponse: widget.characteristic.properties.writeWithoutResponse);
        } catch(FlutterBluePlusException e) {
            if(e.errorString == "GATT_INVALID_ATTRIBUTE_LENGTH") {
                 //ignore
            } else {
                rethrow;
            }
        };

        // Add a delay if needed between chunks to avoid overloading the BLE stack.
        // await Future.delayed(Duration(milliseconds: 100));
      }

      setState(() {
        // Update the UI or state if needed after the write operation
      });

      print("Data written successfully: $dataToWrite");
    } catch (e) {
      print("Error writing data: $e");
      // Handle the error, show a snackbar, or display an error message to the user.
    }
  }
NewAge23 commented 1 year ago

Future _writeData() async { List dataToWrite = [0x00, 0xff, 0xff, 0xff]; final int maxChunkSize = 20; // Replace with your characteristic's maximum allowed length

try {
  for (int i = 0; i < dataToWrite.length; i += maxChunkSize) {
    final chunk = dataToWrite.sublist(
        i,
        i + maxChunkSize > dataToWrite.length
            ? dataToWrite.length
            : i + maxChunkSize);

    await widget.characteristic.write(chunk,
        withoutResponse:
            widget.characteristic.properties.writeWithoutResponse);

    try {
      await widget.characteristic.write(chunk,
          withoutResponse:
              widget.characteristic.properties.writeWithoutResponse);
    } on FlutterBluePlusException catch (e) {
      if (e.errorString == "GATT_INVALID_ATTRIBUTE_LENGTH") {
        //ignore
      } else {
        rethrow;
      }
    }
    ;

    // Add a delay if needed between chunks to avoid overloading the BLE stack.
    // await Future.delayed(Duration(milliseconds: 100));
  }

  setState(() {
    // Update the UI or state if needed after the write operation
  });

  print("Data written successfully: $dataToWrite");
} catch (e) {
  print("Error writing data: $e");
  // Handle the error, show a snackbar, or display an error message to the user.
}

} it seems provde this error I/flutter (24537): Error writing data: FlutterBluePlusException: name:writeCharacteristic errorCode:13, errorString:GATT_INVALID_ATTRIBUTE_LENGTH

chipweinberger commented 1 year ago

You have mistake. You have this code 2x. Delete first one.

    await widget.characteristic.write(chunk,
        withoutResponse:
            widget.characteristic.properties.writeWithoutResponse);
NewAge23 commented 1 year ago

D/FBP-Android: [FBP-Android] onCharacteristicWrite: uuid: 0000fff1-0000-1000-8000-00805f........ status: 13 I/flutter (24537): Data written successfully: [0, 255, 255, 255]

it provide status 13 it means it does not send to the bluetooth device that message. please provide correct solution

chipweinberger commented 1 year ago

ignore the error.

NewAge23 commented 1 year ago

is that means my data is succesfully sent to my bluetooth device?

MrCsabaToth commented 1 year ago

is that means my data is succesfully sent to my bluetooth device?

What does the device do? Does it act like the setting was successful?

NewAge23 commented 1 year ago

when i was used that plugin older version . it communicate to the hterm software but now in upgraded version of plugin it does not communicate to the hterm what i can do