0

Sometimes, if a GATT notification occurs, and I try to get the characteristic value through characteristic.getValue() multiple times, the call suddenly starts to return an empty array.

That mostly happens on the first notification in that connection, and seems to be quite random.

How can I get characteristic.getValue() to be consistent?

I have a Pixel 3; maybe it's an implementation issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel D.
  • 178
  • 2
  • 15

1 Answers1

0

You should only call characteristic.getValue() once per notification, and store it in some variable. You should not call that every time you need to reference that data. If you are calling that multiple times after a single notification, it will not work.

Ideally you are using the service example provided by Google, and pushing the notification data to a variable in the desired activity. This will happen automatically after using setCharacteristicNotification on the desired characteristic, and the update is performed in the broadcastUpdate whenever a notification broadcast is received. (Assuming you connect the 'plumbing' in you application)

axjjienn
  • 56
  • 4
  • Thanks for the answer, although that would be new for me. It seems like it is really a bug in my stack. Do you have any official sources confirming your statement? – Daniel D. Nov 16 '19 at 20:07