Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struggling with BluetoothCharacteristic.value #11

Open
thefakhreddin opened this issue Nov 10, 2021 · 2 comments
Open

Struggling with BluetoothCharacteristic.value #11

thefakhreddin opened this issue Nov 10, 2021 · 2 comments

Comments

@thefakhreddin
Copy link

thefakhreddin commented Nov 10, 2021

No description provided.

@thefakhreddin
Copy link
Author

thefakhreddin commented Nov 10, 2021

I'm trying to read data from a specific characteristic. I have already managed to read data using flutter_blue package. It has a similar class BluetoothCharacteristic with a similar attribute , value which returns Stream<List<int>>. The flutter_web_bluetooth package however returns Stream<ByteData> which I'm having difficulties reading from. Is there any neat way to convert the data type, Stream<ByteData> to Stream<List<int>> so that I can treat the data the same way I did previously with flutter_blue on Android and IOS?

This is what I already wrote with flutter_blue:

/// blue.Bluetooth is a BluetoothDevice object
blue.Bluetooth.readCharacteristic.value
          .timeout(Duration(seconds: 5))
          .firstWhere((d) {
        if (d.isNotEmpty) {
          if (d[0] == SOME_VAL) {
            if (d[1] <= 100) {
              setState(() => readingVal = d[1] / 100);
              return true;
            }
          }
        }
        return false;
      }, orElse: () => null);

This is what I tried with flutter_web_bluetooth:

blueWeb.Bluetooth.readCharacteristic.value
          .timeout(Duration(seconds: 5))
          .firstWhere((d) {
        /// trying to convert <ByteData> to List<int>
        List<int> dList = new List.from(
            d.buffer.asUint8List(d.offsetInBytes, d.lengthInBytes));
        if (dList.isNotEmpty) {
          if (dList[0] == SOME_VAL) {

            if (dList[1] <= 100) {
              setState(() => readingVal = dList[1] / 100);
              return true;
            }
          }
        }
        return false;
      }, orElse: () => null);

It wasn't successful though.

@jeroen1602
Copy link
Owner

jeroen1602 commented Nov 13, 2021

You will only get a value from the stream if you use readValue or startNotifications​.

The startNotifications doesn't work in the latest version because of a typo. But if you get it from the git instead of from pub.dev it will work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants