0

在以下 BLE 示例中:我想将浮点数分成可以分配给 CUSTOM_UUID 的元素。

例如:取 12345.67 并以某种方式将其分配给 CUSTOM_UUID[] 作为

    const uint8_t CUSTOM_UUID[] =
    {
        0X00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x01, 0x32, 0x54, 0x67
    };

    BLEUuid uuid = BLEUuid(CUSTOM_UUID);

当前格式在 BLE 广告中显示为: 00000000-0000-0000-0000-000000000000 所以我想得到类似: 00000000-0000-0000-0000-000001234567 作为第一步。下一步将处理小数。

 /*********************************************************************
 This is an example for our nRF52 based Bluefruit LE modules

 Pick one up today in the adafruit shop!

 Adafruit invests time and resources providing this open source code,
 please support Adafruit and open-source hardware by purchasing
 products from Adafruit!

 MIT license, check LICENSE for more information
 All text above, and the splash screen below must be included in
 any redistribution

 Author: KTOWN (Kevin Townsend)
 Copyright (C) Adafruit Industries 2017
*********************************************************************/

/*  This example constantly advertises a custom 128-bit UUID, and is
 *  intended to be used in combination with a Central sketch that scans
 *  for this UUID, and then displays an alert message, sorting matching
 *  devices by their RSSI level which is an approximate indication of
 *  distance (although highly subject to environmental obstacles).
 *  
 *  By including a custom UUID in the advertising packet, we can easily
 *  filter the scan results on the Central device, rather than manually
 *  parsing the advertising packet(s) of every device in range.
 *  
 *  This example is intended to be run with the *_central.ino version
 *  of this application.
 */

#include <bluefruit.h>
#include <ble_gap.h>

//int test_hex = 0x55;

// Software Timer for blinking RED LED
SoftwareTimer blinkTimer;

// Custom UUID used to differentiate this device.
// Use any online UUID generator to generate a valid UUID.
// Note that the byte order is reversed ... CUSTOM_UUID
// below corresponds to the follow value:
// df67ff1a-718f-11e7-8cf7-a6006ad3dba0
const uint8_t CUSTOM_UUID[] =
{
    0X00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

BLEUuid uuid = BLEUuid(CUSTOM_UUID);




void setup() 
{
  Serial.begin(115200);
  while ( !Serial ) delay(10);   // for nrf52840 with native usb

  Serial.println("Bluefruit52 Peripheral Proximity Example");
  Serial.println("----------------------------------------\n");


  // Initialize blinkTimer for 1000 ms and start it
  blinkTimer.begin(1000, blink_timer_callback);
  blinkTimer.start();

  err_t err = Bluefruit.begin();
  if (err)
  {
    Serial.print("Unable to init Bluefruit (ERROR CODE: ");
    Serial.print(err);
    Serial.println(")");
    while(1)
    {
      digitalToggle(LED_RED);
      delay(100);
    }
  }
  else
  {
    Serial.println("Bluefruit initialized (peripheral mode)");
  }

  // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
  Bluefruit.setTxPower(4);
  Bluefruit.setName("Bluefruit52");

  // Set up and start advertising
  startAdv();

  Serial.println("Advertising started"); 

}

void startAdv(void)
{   
  // Note: The entire advertising packet is limited to 31 bytes!

  // Advertising packet
  Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  Bluefruit.Advertising.addTxPower();

  // Preferred Solution: Add a custom UUID to the advertising payload, which
  // we will look for on the Central side via Bluefruit.Scanner.filterUuid(uuid);
  // A valid 128-bit UUID can be generated online with almost no chance of conflict
  // with another device or etup
  Bluefruit.Advertising.addUuid(uuid);


  // Alternative Solution: Manufacturer Specific Data (MSD)
  // You could also send a custom MSD payload and filter for the 'Company ID'
  // via 'Bluefruit.Scanner.filterMSD(CID);', although this does require a
  // valid CID, which is why the UUID method above is more appropriate in
  // most situations. For a complete list of valid company IDs see:
  // https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers
  // For test purposes, 0xFFFF CAN be used, but according to the Bluetooth SIG:
  // > "This value may be used in the internal and interoperability tests before a
  // >  Company ID has been assigned. This value shall not be used in shipping end
  // >  products."
  uint8_t msd_payload[4]; // Two bytes are required for the CID, so we have 2 bytes user data, expand as needed
  uint16_t msd_cid = 0xFFFF;
  memset(msd_payload, 0, sizeof(msd_payload));
  memcpy(msd_payload, (uint8_t*)&msd_cid, sizeof(msd_cid));
  msd_payload[2] = 0x11;
  msd_payload[3] = 0x22;

  Bluefruit.Advertising.addData(BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA, msd_payload, sizeof(msd_payload));


  // Not enough room in the advertising packet for name
  // so store it in the Scan Response instead
  Bluefruit.ScanResponse.addName();

  /* Start Advertising
   * - Enable auto advertising if disconnected
   * - Interval:  fast mode = 20 ms, slow mode = 152.5 ms
   * - Timeout for fast mode is 30 seconds
   * - Start(timeout) with timeout = 0 will advertise forever (until connected)
   * 
   * For recommended advertising interval
   * https://developer.apple.com/library/content/qa/qa1931/_index.html
   */
  Bluefruit.Advertising.restartOnDisconnect(true);
  Bluefruit.Advertising.setInterval(32, 244);    // in units of 0.625 ms
  Bluefruit.Advertising.setFastTimeout(30);      // number of seconds in fast mode
  Bluefruit.Advertising.start();
}

void loop() 
{
}

/**
 * Software Timer callback is invoked via a built-in FreeRTOS thread with
 * minimal stack size. Therefore it should be as simple as possible. If
 * a periodically heavy task is needed, please use Scheduler.startLoop() to
 * create a dedicated task for it.
 * 
 * More information http://www.freertos.org/RTOS-software-timer.html
 */
void blink_timer_callback(TimerHandle_t xTimerID)
{
  (void) xTimerID;
  digitalToggle(LED_RED);
}
4

3 回答 3

0

我使用这个标准例程将 2 个字节组合为 1。浮点数中的点变成了一个“E”,如果你不希望这样,你可以将浮点数更改为长整数。

uint8_t CUSTOM_UUID[16];

void setup() {
  Serial.begin(115200);
}

void loop() {
  /* example with standard UUID: */
  stringToByte("E2C56DB5-DFFB-48D2-B060-D0F5A71096E0");
  for (int i = 0; i < 16; i++) {
    Serial.println(CUSTOM_UUID[i], HEX);
  }
  Serial.println();

  /* example with float: */
  float number = 12345.67;
  char floatToString[32];
  dtostrf(number, 32, 2, floatToString);
  stringToByte(floatToString);
  for (int i = 0; i < 16; i++) {
    Serial.println(CUSTOM_UUID[i], HEX);
  }
  while(1);
}

void stringToByte(char * data)
{
  // Create two pointers that point to the start of the data
  char *leader = data;
  char *follower = leader;
  int counter = 0;

  // Iterate till at the end of the string
  while (*leader) {
      // Check to see if the current character is a -
      if (*leader != '-') {

          // Grab the next two characters and move leader forwards
          char high = *leader;
          leader++;
          char low = *leader;

          // Convert ASCII 0-9A-F to a value 0-15
          if (high > 0x39) high -= 7;
          high &= 0x0f;

          // Same again for the low byte:
          if (low > 0x39) low -= 7;
          low &= 0x0f;

          // Combine the two into a single byte and store in follower:
          *follower = (high << 4) | low;
          CUSTOM_UUID[counter] = *follower;

      } else {
          counter--;
      }

      // Move both pointers to the next character:
      leader++;
      follower++;
      // increase counter:
      counter++;
  }
}
于 2019-02-01T20:33:16.913 回答
0

This is an interesting way to get a little more bandwidth out of an advertising packet, but it goes against the intended use of the UUIDs, which are to identify a peripheral's service profiles and available characteristics. (I'm assuming you intend to dynamically generate UUIDs based on some floating point sensor value.)

Having a changing UUID will cause a great deal of trouble when you try to connect to your device using the BLE communication stack of the Central (master) device, e.g. an Android or IOS phone. BLE Central devices expect to gather a static hierarchy of characteristic UUIDs from a given peripheral device, then use those UUIDs to identify the corresponding characteristic values. For example, the IOS BLE stack has constants to identify these well-known advertisement UUIDs, all available before connecting: CBAdvertisementDataLocalNameKey, CBAdvertisementDataManufacturerDataKey, CBAdvertisementDataServiceUUIDsKey, CBAdvertisementDataSolicitedServiceUUIDsKey.

Instead, note that Generic Attribute Profiles include data typing, allowing you to directly set and recover float and double float IEEE-754 floating-point characteristic values, as well as fixed point characteristic types which might better serve your purpose.

Search the docs on your host BLE stack for "Characteristic Presentation Format Descriptor" to find the constants used to identify the type.

I'd suggest that you spend some time studying iOS or Android code samples for BLE communications, and the various Bluetooth/GATT tutorials which describe the hierarchical structure of a BLE device's profiles. Find a cheap BLE device which conforms to one of the GATT profiles here: https://www.bluetooth.com/specifications/gatt (You may need to create a free account.) and experiment with contacting it from your phone or PC. Alternately, see if you can find sample Arduino code which makes the device conform to a well-known GATT profile, and use that as your peripheral.

于 2019-02-01T23:09:51.643 回答
0

As others have said, changing UUIDs at run-time is not really an expected behavior, especially as central devices (like smartphones) can save the profile of a peripheral device in cache. Changing it dynamically can therefore cause some troubles.

The expected way to send custom data through BLE advertisement packets would be to use the Manufacturer Specific Data field. It starts with 2 bytes of company ID and then can be followed by custom data. As long as you control both central and peripheral application, you can store your floats as you wish.

Also, be careful as sending both custom UUIDs and manufacturer specific data in the advertisement packet can take some space and advertisements packets are limited. Have a look at Scan response if you need to send more data (like several floats)

于 2019-03-20T10:23:25.743 回答