jeroen1602 / flutter_web_bluetooth

A dart library to use the web bluetooth api.
https://jeroen1602.github.io/flutter_web_bluetooth/
MIT License
19 stars 15 forks source link

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

Open thefakhreddin opened 2 years ago

thefakhreddin commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

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 commented 2 years ago

@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 commented 1 year ago
// 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();
image
jeroen1602 commented 1 year ago

@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.