sandeepmistry / arduino-BLEPeripheral

An Arduino library for creating custom BLE peripherals with Nordic Semiconductor's nRF8001 or nR51822.
MIT License
462 stars 179 forks source link

BLEFixedLengthCharacteristic sends wrong data #266

Open zeroby0 opened 5 years ago

zeroby0 commented 5 years ago

BLEFixedLengthCharacteristic sends wrong data over read/notifications/indications. The data that is sent is sometimes random and persists over different payloads and even on reprogramming the board with a different sketch.

For example, try this sketch

#include <BLEPeripheral.h>
#include <SPI.h>

char data[20];
char buffer[20];

BLEPeripheral blePeripheral = BLEPeripheral();
BLEService s_primary = BLEService("fff0");
BLEFixedLengthCharacteristic c_data =
    BLEFixedLengthCharacteristic("fff1", BLERead | BLENotify, 20);

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

    blePeripheral.setLocalName("Nrf52");
    blePeripheral.setAdvertisedServiceUuid(s_primary.uuid());

    blePeripheral.addAttribute(s_primary);
    blePeripheral.addAttribute(c_data);

    blePeripheral.begin();

    for(int i = 0; i < 20; i++) {
        data[i] = 'a';
    }
}

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

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

        while (central.connected()) {
            data[0]++;
            if (c_data.setValue(data), 20) {
                sprintf(buffer, "data: %02X %02X %02X", data[0], data[1],
                        data[2]);
                Serial.println(buffer);
            }

            delay(1000);
        }

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

Upload it and open this link to view the data https://googlechrome.github.io/samples/web-bluetooth/notifications-async-await.html?service=0xfff0&characteristic=0xfff1.

after that, reprogram it with this sketch:

#include <BLEPeripheral.h>
#include <SPI.h>

char data[20];
char buffer[20];

BLEPeripheral blePeripheral = BLEPeripheral();
BLEService s_primary = BLEService("fff0");
BLEFixedLengthCharacteristic c_data =
    BLEFixedLengthCharacteristic("fff1", BLERead | BLENotify, 20);

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

    blePeripheral.setLocalName("Nrf52");
    blePeripheral.setAdvertisedServiceUuid(s_primary.uuid());

    blePeripheral.addAttribute(s_primary);
    blePeripheral.addAttribute(c_data);

    blePeripheral.begin();

    for(int i = 0; i < 20; i++) {
        data[i] = 0;
    }
}

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

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

        while (central.connected()) {
            data[0]++;
            if (c_data.setValue(data), 20) {
                sprintf(buffer, "data: %02X %02X %02X", data[0], data[1],
                        data[2]);
                Serial.println(buffer);
            }

            delay(1000);
        }

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

Now use the same page again, and instead of 0x01 0x00 0x00...., the output is 0x01 0x61 0x61 ... I got the same output in nRF connect as well. 'a' is 0x61 in ascii.

more information

I'm using the master branch of the sketch. I'm using the NRF52832 chip and platformio ide to program it.

Here is my platformio.ini file

[env:nrf52_dk]
platform = nordicnrf52
board = nrf52_dk
framework = arduino
monitor_speed = 115200
; SoftDevice version
build_flags = -DNRF52_S132
lib_deps =
  BLEPeripheral