Write data to BLE device and read its response flutter?
Asked Answered
D

1

4

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.

@override
  void initState() {
    // TODO: implement initState
    widget.flutterBlue.scanResults.listen((List<ScanResult> results) {
      for (ScanResult result in results) {
        _addDeviceTolist(result.device);
      }
    });
    widget.flutterBlue.startScan();
  }

Connect Device:-

void _connectDevice() async {
    widget.flutterBlue.stopScan();
    print(_deviceToConnect);
    try {
      await _deviceToConnect.connect();
    } catch (e) {
      if (e.code != 'already_connected') {
        throw e;
      }
    } finally {
      _services = await _deviceToConnect.discoverServices();
    }
    _discoverDeviceServices();
    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(value);
        // print(AsciiDecoder().convert(value)); /*** TAG-1***/
        // print(utf8.decode(value)); /*** TAG-2***/
        if (characteristic.properties.write){
          if (characteristic.properties.notify){
            _rx_Write_Characteristic = characteristic;
            _sendCommandToDevice();
          }
        }
      }
    }
  }

Writing commands to device:-

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

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?

Thanks!

Dryden answered 19/12, 2020 at 10:20 Comment(5)
If you print the raw value, what do you get? What are you expecting to get back? Bluetooth is normally byte arrays or list of integers. Is the returned values expected to be a string? Or could it be a numerical value?Uranometry
I get a List of integers which is absolutely perfect. The problem lies in decoding it. The response I receive is [13, 13, 69, 76, 77, 51, 50, 55, 32, 118, 49, 46, 53, 13, 13, 62]. Now decoding of this response is not getting done.Dryden
Those don't look like ASCII/UTF-8 values. What are you expecting those values to represent? Often there are a number values in one read. For example uint8 or unit16 etcUranometry
I am expecting ELM327 v1.5 from the response.Dryden
@Dryden can you share the code. I want run AT command in flutter.Arabelle
U
8

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

Uranometry answered 20/12, 2020 at 8:37 Comment(3)
This seems to resolve my issue. Actually I am pretty new to Flutter and was just exploring BLE. Thank you very much. Also, I don't know who downrated the answer.Dryden
They didn't leave a comment so we can only speculate why it got down voted. My best guess is because I've included a sceengrab with code on. I thought it added more context than just the text. The main thing is you are moving forward.Uranometry
@Uranometry it does add more context, but it is more difficult for screenreaders to process and for some of us to read.Incessant

© 2022 - 2024 — McMap. All rights reserved.