Data types

Data types are very important for writing and reading operations. In case of writing, they specify, how the received value should be interpreted and displayed in the application. In case of reading, they specify, how the value you enter will be interpreted and advertised to other devices. There are a bunch of different options.

Bluetooth operates with byte arrays. For each data type I will provide a calculation example.

Hex

The provided value will be displayed as a hex string. Hex strings can be converted to byte arrays pretty easily. Every pair of characters is exactly one byte:

-> AE C8 FF

-> 174, 200, 255

Binary

The provided value will be displayed as a binary number. Every pair of 8 digits will be converted to one byte:

-> 10101110 11001000 11111111

-> 178, 200, 255

Signed Integer

The application will interpret byte arrays as signed integer values (positive and negative whole numbers). Negative values will use the two’s complement. In case of integer data types, you will have to specify which endianness should be used to convert the value.

  • Little Endian: The first byte will be the least significant byte
  • Big Endian: The first byte will be the most significant byte

Furthermore, you need to specify, how long the value can be:

  • 4 byte: from -2,147,483,648 to +2,147,483,647
  • 8 byte: from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Example:

The byte array

64 00 00 will be interpreted as:

  • Little Endian: 100
  • Big Endian: 6.553.600

9C FF FF FF will be interpreted as:

  • Little Endian: -100
  • Big Endian: -1.660.944.385

Unsigned Integer

Is the same as signed integer, but will only display positive numbers. The range of the value can be:

  • 4 byte: from 0 to 4,294,967,295
  • 8 byte: from 0 to 18,446,744,073,709,551,615

Example:

The byte array

64 00 00 will be interpreted as:

  • Little Endian: 100
  • Big Endian: 6.553.600

9C FF FF FF will be interpreted as:

  • Little Endian: 4.294.967.196
  • Big Endian: 2.634.022.911

Signed floating point

Can represent negative and positive floating point numbers. This data type also needs to specify an endianness.

The range of the value can be:

  • 4 byte: from ±1.5 x 10−45 to ±3.4 x 1038
  • 8 byte: from 0 to ±5.0 × 10−324 to ±1.7 × 10308

UTF-8

Every single character will be represented by one byte.

-> Hello

-> 48 65 6C 6C 6F

where 48 = ‘H’, 65 = ‘e’, 6C = ‘l’ and 6F = ‘o’

BluetoothDate

BluetoothDate is a way to represent date and time information in byte format. The message will always be 10 bytes long:

  • 2 bytes for the year (little endian unsigned integer)
  • 1 byte for the month
  • 1 byte for the day
  • 1 byte for the hour
  • 1 byte for the minute
  • 1 byte for the second
  • 1 byte for the day of the week
  • 1 byte for milliseconds
  • 1 byte for adjust reason (not used)

For example:

-> 2016-01-03T04:36:20

-> E0 07 01 03 04 36 24 00 00 00 -> 224, 7, 1, 3, 4, 36, 20, 0, 0, 0