使用 CurieBLE 发送 20 字节的特征值

Sending 20-byte characteristic values with CurieBLE

Arduino/Genuino 101's CurieBLE library 的文档在“服务设计模式”部分中说明如下:

A characteristic value can be up to 20 bytes long. This is a key constraint in designing services. [...] You could also combine readings into a single characteristic, when a given sensor or actuator has multiple values associated with it.

[e.g. Accelerometer X, Y, Z => 200,133,150]

This is more efficient, but you need to be careful not to exceed the 20-byte limit. The accelerometer characteristic above, for example, takes 11 bytes as a ASCII-encoded string.

但是,API 中可用的类型 Characteristic constructors 仅限于以下内容:

  • BLEBoolCharacteristic
  • BLECharCharacteristic
  • BLEUnsignedCharCharacteristic
  • BLEShortCharacteristic
  • BLEUnsignedShortCharacteristic
  • BLEIntCharacteristic
  • BLEUnsignedIntCharacteristic
  • BLELongCharacteristic
  • BLEUnsignedLongCharacteristic
  • BLEFloatCharacteristic
  • BLEDoubleCharacteristic

None 这些类型的特征似乎能够容纳 20 字节的字符串。 (我试过 BLECharCharacteristic,它似乎属于单个 char,而不是 char 数组。)

如何使用 CurieBLE,将字符串用作特征值,如文档中所述,这是一种有效的做法?

您的问题在 official arduino 101 example 中有所描述。几串代码如何设置数组:

BLECharacteristic heartRateChar("2A37",  // standard 16-bit characteristic UUID
    BLERead | BLENotify, 2);
...
const unsigned char heartRateCharArray[2] = { 0, (char)heartRate };
heartRateChar.setValue(heartRateCharArray, 2);

如您所见,使用 "setValue" 函数将所需数组作为参数来设置特征值。您可以将字符串作为指向数组的 char* 传递。