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

Unable to read data using .value property from a BluetoothCharacteristic object. #13

Open
thefakhreddin opened this issue Nov 13, 2021 · 6 comments

Comments

@thefakhreddin
Copy link

Hi,
After going through a ton of effort, I'm still unable to read data. Please help!
I'm using this simple code to read data from a characteristic but the value stream seems to be empty.


readCharacteristic = await services
                  .firstWhere((service) => service.uuid == SERVICE_UUID)
                  .getCharacteristic(READ_UUID);

print("read characteristic: ${readCharacteristic.uuid}");
await readCharacteristic.startNotifications();
try {
  await readCharacteristic.value
      .timeout(Duration(seconds: 5))
      .firstWhere((element) {
    print("buffer: ${element.buffer}");
    print("elementSizeInBytes: ${element.elementSizeInBytes}");
    print("lengthInBytes: ${element.lengthInBytes}");
    print("offsetInBytes: ${element.offsetInBytes}");
    return false;
  });
} catch (e) {
  print("err: $e");
}

I printed out the readCharacteristic.uuid to make sure that the characteristic is recognized properly. But when I'm expecting to receive data immediately, after 10 seconds the timeout exceeds and I catch a timeout error which I suppose indicates that the stream was empty until that time. If I'm being correct, at least my terminal should have prompted the properties of whatever ByteData is being received.

I have also tried .lastValue.

To make sure that my transmitter device is indeed transmitting, I successfully read data using two other platforms using a similar method.

Regards.

@jeroen1602
Copy link
Owner

jeroen1602 commented Nov 13, 2021

You might be running in to the bug where the event that listens to value change event has a typo in it (I still need to release a new version for that)

Try readValue() to see if it can read a value at all.

Edit: you can also try and import the package from the git directly. https://flutter.dev/docs/development/packages-and-plugins/using-packages

@thefakhreddin
Copy link
Author

thefakhreddin commented Nov 14, 2021

@jeroen1602
Hi, thank you for your response.
I tried readValue() in my code and it threw the error:
Operation not supported for uuid 0000fff1-0000-1000-8000-00805f9b34fb
which is a valid uuid that I've tested before.

ByteData element = await readCharacteristic
            .readValue(timeout: Duration(seconds: 5));

I also removed the package from pubspec.yaml and tried to get the latest version from the repo directly:

flutter_web_bluetooth:
    git:
      url: git://github.com/jeroen1602/flutter_web_bluetooth.git

and I got the same result.

Am I missing anything?

@jeroen1602
Copy link
Owner

Some operations are not supported based on how the characteristic is set up. You should be able to check what is supported by reading the properties of the characteristic.

But did you also try startNotifications() again with the version directly from GitHub? Because that one should work with the GitHub version.

@thefakhreddin
Copy link
Author

@jeroen1602
Hi, Sorry for the delay!
I downloaded the package from the repo again and monitored the characteristic's properties (the ones I thought might be relevant) with the following code:

if (readCharacteristic != null) {
        print(
            "is notifying: ${readCharacteristic.isNotifying}");
        print(
            "notify: ${readCharacteristic.properties.notify}");
        print(
            "reliableWrite: ${readCharacteristic.properties.reliableWrite}");
        print("read: ${readCharacteristic.properties.read}");
      }

and here are the results:

is notifying: true
notify: true
reliableWrite: false
read: false

Couple of notes:

  1. I called startNotifications() right after declaring the characteristic. The print("is notifying: ${readCharacteristic.isNotifying}"); returns true which I thinkg verifies that.
  2. I previously used this exact characteristic via NRF connect application and flutter blue package before reading from my Bluetooth device. So I'm confident about the UUIDs that I chose. (getCharacteristic() method also did not throw NotFoundError and I confirmed the characteristic by printing its UUID print(readCharacteristic.uuid);)

@JooYoo
Copy link

JooYoo commented May 18, 2023

  • ✅ With the following code, I'm able to .readValue() from device on Desktop Chrome. So the uuid is correct and working.
// Setup serviceId
final services = await _device?.discoverServices();
final service = services?.firstWhere(
  (service) => service.uuid == serviceId,
);
// Setup charId
final characteristic = await service?.getCharacteristic(characteristicUuid);
// Read from device
final res = characteristic?.readValue();
  • 💔 However, on mobile phone Android Chrome, search, connect, write are all working, but read value failed. I got the similar error from the android Chrome. Which is very strange, why it works on Desktop Chrome, but not on MobilePhone Android Chrome...
image

@jeroen1602
Copy link
Owner

@JooYoo This seems to be a bug with the chrome version that is running on Android. You can avoid this error by checking it the characteristic supports reading with characteristic.properties.read. Though this doesn't explain why it would work on desktop chrome and not on the mobile version of chrome.

You could try enabling the enable-experimental-web-platform-features flag in chrome://flags, and see if that solves the problem.

You could also try using a BLE app on your phone to see if that is able to read from the characteristic, it may just be the Bluetooth implementation on the phone.

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

3 participants