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

Helper functions to read values #126

Closed
g-ortuno opened this issue May 29, 2015 · 12 comments
Closed

Helper functions to read values #126

g-ortuno opened this issue May 29, 2015 · 12 comments

Comments

@g-ortuno
Copy link
Contributor

In Android we have helper functions to more easily read and write values to and from characteristics:

[getFloatValue()](https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic.html#getFloatValue%28int, int%29)
[getIntValue()](https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic.html#getIntValue%28int, int%29)
getStringValue()

They are really useful since developers don't have to worry about bytes if all they are doing is reading/writing integers. Could we add similar helper functions?

@jyasskin
Copy link
Member

I think these features should be prototyped in a helper library before we put them in the spec, so they have a chance to evolve faster than they would in Blink. Shall we start a sibling repository to hold that helper library?

@g-ortuno
Copy link
Contributor Author

Eventually we should, for now let's focus on the spec and implementation.

@beaufortfrancois
Copy link
Member

I'd be happy to start this helper library.
Which other helper functions would you add?

@beaufortfrancois
Copy link
Member

@jyasskin
Copy link
Member

jyasskin commented Jan 7, 2016

Yes, that's great. I think I'd make 2 changes: I'd either default the byteOffset to 0 or omit it entirely, since if you're reading multiple items from a single characteristic, you probably want to only call readValue once. And I'd default littleEndian to true, since nearly all Bluetooth values are little-endian, contrary to DataView's default.

@beaufortfrancois
Copy link
Member

Will do that tomorrow then. Thanks for the feedback.

On Thu, Jan 7, 2016, 7:15 PM Jeffrey Yasskin notifications@github.com
wrote:

Yes, that's great. I think I'd make 2 changes: I'd either default the
byteOffset to 0 or omit it entirely, since if you're reading multiple
items from a single characteristic, you probably want to only call
readValue once. And I'd default littleEndian to true, since nearly all
Bluetooth values are little-endian, contrary to DataView's default.


Reply to this email directly or view it on GitHub
#126 (comment)
.

@beaufortfrancois
Copy link
Member

I've changed default littleEndian to true as advised. Thank you!

Regarding the default/removal of byteOffset, that made me realized we might want to implement BluetoothGattCharacteristic.prototype.getFloat32Value instead of BluetoothGattCharacteristic.prototype.readFloat32Value so that it returns a Float32 value of the stored cached read value.
What do you think?

@beaufortfrancois
Copy link
Member

@g-ortuno
Copy link
Contributor Author

I wonder how useful these function will be when characteristic returns a
DataView instead of an ArrayBuffer.

On Fri, Jan 8, 2016 at 1:48 AM François Beaufort notifications@github.com
wrote:

Here's what it would look like:
https://github.com/beaufortfrancois/sandbox/blob/gh-pages/web-bluetooth/utils/utils.js

[image: screenshot 2016-01-08 at 10 47 07 am]
https://cloud.githubusercontent.com/assets/634478/12195118/3896530a-b5f5-11e5-97e1-98e0a4da98f8.png


Reply to this email directly or view it on GitHub
#126 (comment)
.

@beaufortfrancois
Copy link
Member

Hopefully it won't be needed anymore.

@beaufortfrancois
Copy link
Member

Here's what it looks like at https://github.com/beaufortfrancois/sandbox/blob/gh-pages/web-bluetooth/utils/utils.js below. What do you think?

BluetoothGATTCharacteristic.prototype.getFloat32Value = function(byteOffset, littleEndian = true) {
  return this.value.getFloat32(byteOffset, littleEndian);
};

BluetoothGATTCharacteristic.prototype.getFloat64Value = function(byteOffset, littleEndian = true) {
  return this.value.getFloat64(byteOffset, littleEndian);
};

BluetoothGATTCharacteristic.prototype.getInt16Value = function(byteOffset, littleEndian = true) {
  return this.value.getInt16(byteOffset, littleEndian);
};

BluetoothGATTCharacteristic.prototype.getInt32Value = function(byteOffset, littleEndian = true) {
  return this.value.getInt32(byteOffset, littleEndian);
};

BluetoothGATTCharacteristic.prototype.getInt8Value = function(byteOffset) {
  return this.value.getInt8(byteOffset);
};

BluetoothGATTCharacteristic.prototype.getStringValue = function(utfLabel = 'utf8') {
  var decoder = new TextDecoder(utfLabel);
  return decoder.decode(this.value);
};

BluetoothGATTCharacteristic.prototype.getUint16Value = function(byteOffset, littleEndian = true) {
  return this.value.getUint16(byteOffset, littleEndian);
};

BluetoothGATTCharacteristic.prototype.getUint32Value = function(byteOffset, littleEndian = true) {
  return this.value.getUint32(byteOffset, littleEndian);
};

BluetoothGATTCharacteristic.prototype.getUint8Value = function(byteOffset) {
  return this.value.getUint8(byteOffset);
};

@jyasskin
Copy link
Member

jyasskin commented May 4, 2016

I'd be happy to have this as a simple npm module, but I'd like to wait to change the spec until we see it being widely used. I'll close this issue in this repository, but we can reopen later if it turns out people want this.

@jyasskin jyasskin closed this as completed May 4, 2016
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