Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Cannot get BLE Characteristic to read or write
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Cannot get BLE Characteristic to read or write

by dougconran on Sun Aug 09, 2020 9:00 am

I am still struggling to get this project working. To recap, I'm trying to develop a Bluetooth peripheral that can be written to and read from so as to act as a simulator. It has 2 Characteristics, one is a NOTIFY one to pump out data which is working and one to READ and WRITE data which is not. My code is based on the Heart Rate Monitor example customhrm.py and https://learn.adafruit.com/bluefruit-nrf52-feather-learning-guide/bleclientcharacteristic from the Adafruit Bluefruit Learning Guide and is as follows:-

Code: Select all | TOGGLE FULL SIZE
#include <bluefruit.h>

//static BLEUUID serviceUUID("000018f0-0000-1000-8000-00805f9b34fb");
//static BLEUUID    readUUID("00002af0-0000-1000-8000-00805f9b34fb");   //  NOTIFY
//static BLEUUID   writeUUID("00002af1-0000-1000-8000-00805f9b34fb");   //  READ, WRITE

const uint8_t UUID_SERVICE[] =
{
    0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
    0x00, 0x10, 0x00, 0x00, 0xf0, 0x18, 0x00, 0x00
};

const uint8_t UUID_CHR_NOTIFY[] =
{
    0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
    0x00, 0x10, 0x00, 0x00, 0xf0, 0x2a, 0x00, 0x00
};
const uint8_t UUID_CHR_READWRITE[] =
{
    0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
    0x00, 0x10, 0x00, 0x00, 0xf1, 0x2a, 0x00, 0x00
};
 
BLEService        rede  = BLEService(UUID_SERVICE);
BLECharacteristic reden = BLECharacteristic(UUID_CHR_NOTIFY);
BLECharacteristic rederw = BLECharacteristic(UUID_CHR_READWRITE);

BLEDis bledis;    // DIS (Device Information Service) helper class instance

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

  Serial.println("Bluefruit52 Simulator");
  Serial.println("---------------------\n");

  // Initialise the Bluefruit module
  Serial.println("Initialise the Bluefruit nRF52 module");
  Bluefruit.begin();

  // Set the advertised device name (keep it short!)
  Serial.println("Setting Device Name to 'IOS-BLINK'");
  Bluefruit.setName("BLINK");

  // Set the connect/disconnect callback handlers
  Bluefruit.Periph.setConnectCallback(connect_callback);
  Bluefruit.Periph.setDisconnectCallback(disconnect_callback);

  // Configure and Start the Device Information Service
  Serial.println("Configuring the Device Information Service");
  bledis.setManufacturer("Adafruit Industries");
  bledis.setModel("Bluefruit ItsyBitsy");
  bledis.begin();

  // Setup the service
  // BLEService and BLECharacteristic classes
  Serial.println("Configuring ...");
  setuprede();

  // Setup the advertising packet(s)
  Serial.println("Setting up the advertising payload(s)");
  startAdv();

  Serial.println("Ready Player One!!!");
  Serial.println("\nAdvertising");
}

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

  // Include HRM Service UUID
  Bluefruit.Advertising.addService(rede);

  // Include Name
  Bluefruit.Advertising.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 
}

void setuprede(void)
{
  rede.begin();

////////  Notify Characteristic  //////////
  reden.setProperties(CHR_PROPS_NOTIFY);
  reden.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
  reden.setMaxLen(20);
  reden.setCccdWriteCallback(reden_callback);
  reden.begin();

///////////  READ/WRITE Characteristic  /////////////////
  rederw.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE);
  rederw.setPermission(SECMODE_OPEN, SECMODE_OPEN);
  rederw.setFixedLen(7);
  rederw.begin();
  char hrmdata[8] = "Ready\r";
  rederw.write(hrmdata, 7);                 //   <<=========  Not working
}

void connect_callback(uint16_t conn_handle)
{
  // Get the reference to current connection
  BLEConnection* connection = Bluefruit.Connection(conn_handle);

  char central_name[32] = { 0 };
  connection->getPeerName(central_name, sizeof(central_name));

  Serial.print("Connected to ");
  Serial.println(central_name);
}

void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
  (void) conn_handle;
  (void) reason;

  Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);
  Serial.println("Advertising!");
}

void reden_callback(uint16_t conn_hdl, BLECharacteristic* chr, uint16_t cccd_value)
{
//    Display the raw request packet
//    Serial.print("CCCD Updated: ");
//    Serial.print(cccd_value);
//    Serial.println("");
}

void loop()
{
  digitalToggle(LED_RED);
 
  if ( Bluefruit.connected() ) {

    char atdata[20] = "OK\r\n";
    uint8_t recdata = 0;
    Serial.print(" Init : ");
    Serial.print(recdata);
   
    recdata = rederw.read8();
   
    Serial.print(" - Val : ");
    Serial.println(recdata);      //   <<===========  Produces unchanging arbitrary number
/*   
    if ( reden.notify(atdata, strlen(atdata)) ){
      Serial.print("Sent Data : ");
      Serial.print(atdata);
      Serial.print("Length : ");
      Serial.println(strlen(atdata));
    }else{
      Serial.println("ERROR: Notify not set in the CCCD or not connected!");
    }
*/
  }
  delay(2000);
}


Notify is working fine but write produces nothing and read produces an unchanging and erroneous result (I am, in fact, entering 132):-

Code: Select all | TOGGLE FULL SIZE
Connected to Samsung Galaxy S7
 Init : 0 - Val : 82
 Init : 0 - Val : 82
 Init : 0 - Val : 82
 Init : 0 - Val : 82
 Init : 0 - Val : 82


In fact, I want to be able to read text (up to 10 characters) but I can't even get a single uint_8 value to work although, as far as I can see I'm following the Learning Guide.

What am I doing wrong and how do I fix it and, going on from that, how do I read up to 10 characters sent from a client?

dougconran
 
Posts: 24
Joined: Wed Nov 08, 2017 8:25 am

Please be positive and constructive with your questions and comments.