0

谢谢你来看这个问题。

我正在使用 Arduino 制作带有 nRF52832 模块的气体检测器。

我想在检测到气体时更改蓝牙设备名称。这是因为如果发生这种情况,您可以在蓝牙设备搜索阶段检查气体检测仪的状态。

我知道没有办法重新启动nRF52832模块本身,所以我想知道如何从Bluefruit库中重新启动蓝牙本身。

下面是我应用的代码。谢谢你。

    #include <bluefruit.h>

// BLE Service
BLEDfu  bledfu;  // OTA DFU service
BLEDis  bledis;  // device information
BLEUart bleuart; // uart over ble
BLEBas  blebas;  // battery

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

#if CFG_DEBUG
  // Blocking wait for connection when debug mode is enabled via IDE
  while ( !Serial ) yield();
#endif

  bleStart(); //ble setup
}

void loop() {
  // Forward from BLEUART to HW Serial
  while ( bleuart.available() ) {
    uint8_t ch;
    ch = (uint8_t) bleuart.read();
    //    Serial.write(ch);
    if (ch == '0') {
      //change ble name [gas 0 detected]
    } else if (ch == '1') {
      s//change ble name [gas 1 detected]
    } else if (ch == '2') {
      //change ble name [gas 2 detected]
    }
  }
}

void bleStart() {
  //Ble set
  Bluefruit.autoConnLed(true);
  Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
  Bluefruit.begin();
  Bluefruit.setTxPower(4);    // Check bluefruit.h for supported values


  Bluefruit.setName(nameBuf); // change device name

 
 Bluefruit.Periph.setConnectCallback(connect_callback);
  Bluefruit.Periph.setDisconnectCallback(disconnect_callback);

  // To be consistent OTA DFU should be added first if it exists
  bledfu.begin();

  // Configure and Start Device Information Service
  bledis.setManufacturer("Adafruit Industries");
  bledis.setModel("Bluefruit Feather52");
  bledis.begin();

  // Configure and Start BLE Uart Service
  bleuart.begin();

  // Start BLE Battery Service
  blebas.begin();
  blebas.write(100);

  // Set up and start advertising
  startAdv();
}

void startAdv(void)
{
  // Advertising packet
  Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  Bluefruit.Advertising.addTxPower();

  // Include bleuart 128-bit uuid
  Bluefruit.Advertising.addService(bleuart);

  // Secondary Scan Response packet (optional)
  // Since there is no room for 'Name' in Advertising packet
  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 unit of 0.625 ms
  Bluefruit.Advertising.setFastTimeout(30);      // number of seconds in fast mode
  Bluefruit.Advertising.start(0);                // 0 = Don't stop advertising after n seconds
}
4

1 回答 1

0

基本上,您正在寻找的是在检测到气体时更改广告名称。

您可以按照此程序进行操作。

  1. 检测气体。
  2. 停止 BLE 广告。Bluefruit.Advertising.stop()
  3. 更改 BLE/GAP 设备名称 void setName (const char* str);
  4. 再次启动 BLE 广告。Bluefruit.Advertising.start(0);
于 2021-06-01T17:05:35.170 回答