蓝牙为什么不显示设置的BLE设置?

时间:2019-05-23 15:37:34

标签: arduino bluetooth-lowenergy arduino-uno arduino-ide hm-10

我正在尝试使用HM-10蓝牙低功耗模块在Arduino Uno上设置基本的GATT服务器。我正在使用Sandeep Mistry的Arduino-BlePeripheral库来配置Arduino。我只是使用该库提供的测试示例来测试设置,但是当我使用nRf Connector / LightBlue时,代码中定义的服务和特征不会出现。

Arduino-BlePeripheral库: https://github.com/arduino-libraries/ArduinoBLE

我正在使用的HM-10模块: https://www.amazon.co.uk/DSD-TECH-Bluetooth-iBeacon-Arduino/dp/B06WGZB2N4

我还尝试编辑模块提供的默认特征uuid(“ 0000-FFE0-0000-1000-8000-0080-5F9B-34FB”),但是特征保持不变。

// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root 
for full license information.

//#define SHOW_FREE_MEMORY

#ifdef SHOW_FREE_MEMORY
#include <MemoryFree.h>
#endif

// Import libraries (BLEPeripheral depends on SPI)
#include <SPI.h>
#include <BLEPeripheral.h>

// define pins (varies per shield/board)
#define BLE_REQ   0
#define BLE_RDY   1
#define BLE_RST   9

// create peripheral instance, see pinouts above
BLEPeripheral blePeripheral = BLEPeripheral(BLE_REQ, BLE_RDY, BLE_RST);

// create service
BLEService testService = BLEService("fff0");
// create counter characteristic
BLEUnsignedShortCharacteristic testCharacteristic = 
BLEUnsignedShortCharacteristic("fff1", BLERead | BLEWrite | 
BLEWriteWithoutResponse | BLENotify /*| BLEIndicate*/);

// create user description descriptor for characteristic
BLEDescriptor testDescriptor = BLEDescriptor("2901", "counter");

// last counter update time
unsigned long long lastSent = 0;

void setup() {
Serial.begin(9600);
#if defined (__AVR_ATmega32U4__)
delay(5000);  //5 seconds delay for enabling to see the start up comments 
on the serial board
#endif

blePeripheral.setLocalName("test");
#if 1
blePeripheral.setAdvertisedServiceUuid(testService.uuid());
#else
const char manufacturerData[4] = {0x12, 0x34, 0x56, 0x78};
blePeripheral.setManufacturerData(manufacturerData, 
sizeof(manufacturerData));
#endif

// set device name and appearance
blePeripheral.setDeviceName("Test");
blePeripheral.setAppearance(0x0080);

// add service, characteristic, and decriptor to peripheral
blePeripheral.addAttribute(testService);
blePeripheral.addAttribute(testCharacteristic);
blePeripheral.addAttribute(testDescriptor);

// assign event handlers for connected, disconnected to peripheral
blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
blePeripheral.setEventHandler(BLEDisconnected, 
blePeripheralDisconnectHandler);

  // assign event handlers for characteristic
  testCharacteristic.setEventHandler(BLEWritten, characteristicWritten);
  testCharacteristic.setEventHandler(BLESubscribed, 
characteristicSubscribed);
  testCharacteristic.setEventHandler(BLEUnsubscribed, 
characteristicUnsubscribed);

  // set initial value for characteristic
  testCharacteristic.setValue(0);

  // begin initialization
  blePeripheral.begin();

  Serial.println(F("BLE Peripheral"));

  #ifdef SHOW_FREE_MEMORY
  Serial.print(F("Free memory = "));
  Serial.println(freeMemory());
  #endif
  }

void loop() {
  BLECentral central = blePeripheral.central();

  if (central) {
    // central connected to peripheral
    Serial.print(F("Connected to central: "));
    Serial.println(central.address());

    // reset counter value
    testCharacteristic.setValue(0);

    while (central.connected()) {
      // central still connected to peripheral
      if (testCharacteristic.written()) {
        // central wrote new value to characteristic
        Serial.println(F("counter written, reset"));

         // reset counter value
        lastSent = 0;
        testCharacteristic.setValue(0);
      }

      if (millis() > 1000 && (millis() - 1000) > lastSent) {
        // atleast one second has passed since last increment
        lastSent = millis();

        // increment characteristic value
        testCharacteristic.setValue(testCharacteristic.value() + 1);

        Serial.print(F("counter = "));
        Serial.println(testCharacteristic.value(), DEC);
      }
    }

    // central disconnected
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

我希望将公布的设备名称更改为test,但该设备仅显示预设的设备名称“ DSD Tech”。

0 个答案:

没有答案