Coder Social home page Coder Social logo

Comments (6)

jack4git avatar jack4git commented on June 24, 2024 1

Tried this again after I learned some lessons about async ... another version of the code (device is a BluetoothDevice). Based on logs from flutter_blue, all the characteristics are getting read, no errors being caught, it looks like one or more Futures just don't return.

Could also be my code or a Dart issue!

Future.wait(_charValMap.keys.map(powerDevice.scanResult.device.readCharacteristic))
  .then((List<List<int>> listList) {
    print('Inside Future.wait, listList has ${listList.length} elements');
  })
  .catchError((e) {
    print('error $e');
  }); 

from flutter_blue.

pauldemarco avatar pauldemarco commented on June 24, 2024

I'll have some time to investigate this next week, thanks for reporting!

from flutter_blue.

pauldemarco avatar pauldemarco commented on June 24, 2024

Future.wait will fire off the functions in quick succession.

Both iOS and Android only support one readCharacteristic attempt at a time, or in other words, you must wait for the readCharacteristic to return before calling it again.

Dart makes this easy with async functions like so:

readAll() async {
  final value1 = await device.readCharacteristic(char1);
  final value2 = await device.readCharacteristic(char2);
  // do something with value1 and value2
}

Here, the await keyword will wait for readCharacteristic to return before moving onto the next line of code.

If you wanted similar functionality to your function you could do something like:

Future<List> readAll() async {
  final value1 = await device.readCharacteristic(char1);
  final value2 = await device.readCharacteristic(char2);
  return [value1, value2];
}

readAll().then((values) => print('${values.length} read.'));

from flutter_blue.

jack4git avatar jack4git commented on June 24, 2024

re: one attempt at a time ... I was not aware of the limitation in iOS / Android, can you point me at a reference? (It might actually be a BLE limitation -- I write embedded BLE firmware also and this might be a spec thing ...)

re: firing off in quick succession, yes indeed. But based on what I'm seeing logged, it looks like flutter_blue is actually executing all the reads successfully ... the log looks just like it does on my successful approach using await in a loop ... I wonder if the right solution here is (assuming you can detect multiple overlapping read attempts) is to error out, or to actually allow it to work (sequentially) by returning futures for all requests when they complete ... right now it almost "succeeds silently" ...

Here's my current approach, given that flutter_blue gives me lists of characteristics: looping through a list (this just does the bluetooth value to internal model mapping on one characteristic at the moment):

for (BluetoothCharacteristic char in _charValMap.keys.toList()) {
    tmpIntList = await powerDevice.scanResult.device.readCharacteristic(char);
    _charValMap[char] = tmpIntList;
    print('processed _charValMap item: ${char.uuid} ${tmpIntList.toString()}');
    // Process/save values here ...
  }

from flutter_blue.

pauldemarco avatar pauldemarco commented on June 24, 2024

I am unable to replicate this issue @jack4git

Closing this for now, please reopen if you are still experiencing this in the latest version.

from flutter_blue.

akshay-kapase avatar akshay-kapase commented on June 24, 2024

There are three services out of which the third one should be used to read and write characteristic.The first two are generic and the third one is custom service using which it will work. Try this :

_scanResult.device.discoverServices().then((services){
services[2].characteristics[0].read();
});

from flutter_blue.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.