2

I'm actually confused how I can receive data using flutter_blue package, I can send data but I can not receive and this is the example I've been using:

 startScan() { setState(() { connectionText = "Start Scanning"; }); scanSubScription = flutterBlue.scan().listen((scanResult) { if (scanResult.device.name == TARGET_DEVICE_NAME) { print('DEVICE found'); stopScan(); setState(() { connectionText = "Found Target Device"; }); targetDevice = scanResult.device; connectToDevice(); } }, onDone: () => stopScan()); } stopScan() { scanSubScription?.cancel(); scanSubScription = null; } connectToDevice() async { if (targetDevice == null) return; setState(() { connectionText = "Device Connecting"; }); await targetDevice.connect(); print('DEVICE CONNECTED'); setState(() { connectionText = "Device Connected"; }); discoverServices(); } disconnectFromDevice() { if (targetDevice == null) {} targetDevice.disconnect(); setState(() { connectionText = "Device Disconnected"; }); } discoverServices() async { if (targetDevice == null) return; List<BluetoothService> services = await targetDevice.discoverServices(); services.forEach((service) { // do something with service if (service.uuid.toString() == SERVICE_UUID) { service.characteristics.forEach((characteristic) { if (characteristic.uuid.toString() == CHARACTERISTIC_UUID) { targetCharacteristic = characteristic; // writeData("A 300 300 300"); setState(() { connectionText = "All Ready with ${targetDevice.name}"; }); } }); } }); } writeDataAndWaitForRespond() async { writeData("A 300 300 300"); List<BluetoothService> services = await targetDevice.discoverServices(); print("////////////////We're here, listening to Hive..."); // isDeviceTurnedOn = true; services.forEach((service) async { Future.delayed(const Duration(milliseconds: 500), () async { print("Entered the loop..."); var characteristics = service.characteristics; for (BluetoothCharacteristic c in characteristics) { List<int> value = await c.read(); String stringValue = new String.fromCharCodes(value); print("The recieved Characteristic Value is $stringValue and $value"); print("Entered the second loop..."); var descriptors = c.descriptors; print("The descriptors value is equal to: $descriptors"); for (BluetoothDescriptor d in descriptors) { List<int> value = await d.read(); print("Entered the third loop..."); String stringValue = new String.fromCharCodes(value); print("The recieved Value is $stringValue and $value"); } } }); }); } writeData(String data) { if (targetCharacteristic == null) return; List<int> bytes = utf8.encode(data); targetCharacteristic.write(bytes); } 

I'm totally confused, how can I use flutter_blue to receive data from my ESP32 device, the ESP32 is working properly and I've tested it using "BLE Scan", as far as I understood the writeDataAndWaitForRespond function should do the work but it doesn't and it won't even enter the following loop:

 for (BluetoothDescriptor d in descriptors) { List<int> value = await d.read(); print("Entered the third loop..."); String stringValue = new String.fromCharCodes(value); print("The recieved Value is $stringValue and $value"); } 

Please help, Thanks in advance.

2
  • are you sure you wanted to use descriptors? I think it does not enter the loop as there are no descriptors on the device. when you write you're writing to a characteristic and expecting / reading a value from the descriptor? let me know if I don't understand it correctly. Commented Nov 19, 2020 at 19:52
  • Thanks for the fast respond, I don't know if I was doing it right or wrong and I just want to receive the data from ESP32 device because this is an important test app. How can I receive data using flutter_blue package? this is the main question :) thanks! Commented Nov 19, 2020 at 20:03

2 Answers 2

5

Since you're writing the data to the targetCharacteristic using

 writeData(String data) { if (targetCharacteristic == null) return; List<int> bytes = utf8.encode(data); targetCharacteristic.write(bytes); } 

You read the data from the same using

 Future<List<int>> readData() async { if (targetCharacteristic == null) return; return await targetCharacteristic.read(); } 
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, I received the value but how can I convert 'Future<List<int>>' to string, so I can understand what's in it?
you could use, String.fromCharCodes([...]) :)
sorry it didn't work, the message is: " The argument type 'Future<List<int>>' can't be assigned to the parameter type 'Iterable<int>' ", Thanks
yes, so you have to wait for the Future to complete. String data = String.fromCharCodes(await readData());
Sorry I don't know much about flutter, Thanks a lot!
|
1

This works for me Sharing Code for All.

(widget.readValues[characteristic.uuid]==null) ? Text('Value: widget.readValues[characteristic.uuid].toString()): Text('Value: ' + String.fromCharCodes(widget.readValues[characteristic.uuid])) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.