Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter_Blue Read response from write

I do not have the Rep to create a tag for Flutter_Blue. But this uses Flutter_Blue in Android Studio.

I am trying to send commands to a device that my company creates devices and I am developing and Android/IOS application that connects to the devices and sends commands and reads what the device returns.

I have transplanted the example code to find devices and then write to them. I have the following code.

    _writeCharacteristic(BluetoothCharacteristic c) async {

await device.writeCharacteristic(c, [0x076, 0x0d, 0x0a],
    type: CharacteristicWriteType.withResponse);


setState(() {});
}

This sends a v and crlf to a device. How do I read the device response?

Let me know if there is more I need to accomplish this question properly.

I figured it out, See Below

like image 728
Darnold Avatar asked May 29 '26 12:05

Darnold


1 Answers

I could be wrong but I found the spot that gets the response i'm looking for.

I Added this

String x = "";
        d.forEach((f) => x+=String.fromCharCode(f));

to

_setNotification(BluetoothCharacteristic c) async {
    if (c.isNotifying) {
      await device.setNotifyValue(c, false);
      // Cancel subscription
      valueChangedSubscriptions[c.uuid]?.cancel();
      valueChangedSubscriptions.remove(c.uuid);
    } else {
      await device.setNotifyValue(c, true);
      // ignore: cancel_subscriptions
      final sub = device.onValueChanged(c).listen((d) {
        String x = "";
        d.forEach((f) => x+=String.fromCharCode(f));
        Response = x;
        setState(() {

          print('onValueChanged $x');
        });
      });
      // Add to map
      valueChangedSubscriptions[c.uuid] = sub;
    }
    setState(() {});
  }

and outputs

    onValueChanged DT8610CI-7125
like image 199
Darnold Avatar answered Jun 01 '26 09:06

Darnold



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!