Jump to content
jcwhit

TBluetoothGattCharacteristic.SetValueAs will not reduce the size of the value array.

Recommended Posts

Posted (edited)

This issue shows up when writing a Characteristic from the Andorid app to the BT device. Here is the sequence using these TBluetoothGattCharactertistics methods

SetValueAsUint32, then 4 bytes are transmitted. 

SetValueAsUint64, then 8 bytes are transmitted

SetValueAsUint32, then 8 bytes are transmitted, the first four bytes contain the new 32 bits and the remaining 4 bytes contain the previous 64 bit value

*************************************************************************************************************

In System.Bluetooth we find this method @line 2717(10.4.2), which all of the above methods call

 

procedure TBluetoothGattCharacteristic.SetValueAs<T>(AValue: T; Offset: Integer);
var
  LBytes: TBytes;
begin
  LBytes := Value;
  if (Length(LBytes) < Offset + SizeOf(AValue)) then
      SetLength(LBytes, Offset + SizeOf(AValue));
  Move(AValue, LBytes[Offset], SizeOf(AValue));
  SetValue(LBytes);
end;

 

LBytes is always the last value sent (64 bits or 8bytes in the above example).  As long as offset is zero (0), following a 64bit value with a 32bit or less value,  will always result in SetLength not being called. And as long as offset is zero, this procedure can never reduce the size of LBytes, it can only increase the size of LBytes.

*****************************************************************************************************************

my work around was to create a method in my BT wrapper that

  1. creates a variable ClrValue of type TBytes
  2. set length to 1
  3. call the method SetValue(ClrValue)

This resets the TBluetoothGattCharactertistics property Value to be length 1 and thus the SetLength in the above code will always be called and the correct number of bytes will be transmitted.

*****************************************************************************************************************

I went back and checked 10.1 Update2 and the same code as above is there.  This may be expected behavior, I dont know,  Other than my little work around, I found no intrinsic way to change the array length and  thus the bytes transmitted.  If you call SetValueAsUint32, I would expect 4 bytes to be transmitted, regardless of what was transmitted before.

 

Edited by jcwhit

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×