Write data to BLE device and read its response flutter?


I am facing issues with writing the commands to BLE device and receiving the data. I am writing ASCII encoded string to the Characteristic. The problem arises when its time to decode the data back. Exact data is received as it’s received in iOS but when I try to decode it just gets blank. I tried decoding via UTF8 decoder and ASCII decoder but no result was obtained.

This is how I discover the devices.

  void initState() {
    // TODO: implement initState
    widget.flutterBlue.scanResults.listen((List<ScanResult> results) {
      for (ScanResult result in results) {

Connect Device:-

void _connectDevice() async {
    try {
      await _deviceToConnect.connect();
    } catch (e) {
      if (e.code != 'already_connected') {
        throw e;
    } finally {
      _services = await _deviceToConnect.discoverServices();
    setState(() {
      _connectedDevice = _deviceToConnect;

Discovering the services:-

When commenting TAG-1 & TAG-2 lines the values get printed. I tried both ASCII and UTF8 decoding.

While scrolling through various issues I found out to read values from all the characteristics.

void _discoverDeviceServices() async {
    for (BluetoothService service in _services){
      for (BluetoothCharacteristic characteristic in service.characteristics){
        var value = await characteristic.read();
        // print(AsciiDecoder().convert(value)); /*** TAG-1***/
        // print(utf8.decode(value)); /*** TAG-2***/
        if (characteristic.properties.write){
          if (characteristic.properties.notify){
            _rx_Write_Characteristic = characteristic;

Writing commands to device:-

void _sendCommandToDevice() async {
    final command = "AT Z\r";
    final convertedCommand = AsciiEncoder().convert(command);
    await _rx_Write_Characteristic.write(convertedCommand);

Please help me out pointing where I got wrong in reading the response of the data written to BLE device and how exactly writing and reading of values is to be done?



It is probably String.fromCharCode you are looking for.

It seemed to give the right answer when I tested it with your values on https://dartpad.dev/

enter image description here

The 13s are \r escape characters that represent carriage return

Answered By – ukBaz

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.