Sending Commands

We can now send commands to Dash and Dot by setting the value of the command characteristic. Since we’re sending data over Bluetooth LE, we’re going to send as few bytes as possible. No room for objects or strings, we’ll use a simple array of 8-bit unsigned integers.

var data = new Uint8Array(2);

The first byte will contain the command, and any following bytes will contain the required parameters. In this example, the first byte contains 8, the command to set eye brightnesses. The second byte contains a brightness level between 0 and 255.

// Turn eye off
data[0] = 8;
data[1] = 0;
command.setValue(data);

Let’s take a look at another example. 6 is the command to set the left ear color. It requires us to include values for red, green and blue. This is 4 bytes in total.

// Change left ear color to purple
var data = new Uint8Array(4);
data[0] = 6;
data[1] = 255;
data[2] = 0;
data[3] = 255;
command.setValue(data);

Make Some Noise

The command to play a sound is 24. Each sound has an identifier. To make Dash or Dot say Happy Birthday, we use the identifier “SYSTBIRTHDAY”. Of course we need to convert it to a byte array.

var sound = 'SYSTBIRTHDAY'.
var data = new Uint8Array(1 + sound.length);
data[0] = 24;
for (var i = 0; i < sound.length; i++) {
  data[i + 1] = sound.charCodeAt(i)
}
command.setValue(data);

Take a look at the final example for a full list of possible sounds.

Keywords

  • Await 1
  • B-Splines 1
  • Bezier 1
  • Binary 1
  • Bluetooth 1
  • Canvas 1 2
  • Curves 1
  • Drupal 1
  • Gulp 1
  • JavaScript 1 2 3
  • PHP 1
  • Promises 1
  • Xdebug 1