I'm working on Adafruit Bluefruit nRF52 here .
I need help with setting up multiple UUID connection.
- Start service and characteristics based on UUID 001
- Send or Receive Data
- Disconnects from the UUID 001
- Start service again based on UUID 002
...so on
What I'm struggling with is I can't disconnect from the client at will. I only have Disconect_callback function but I can initiate disconnection.
Secondly, even after resetting the Client node, the setup(); function doesn't restarts Service and Chracteristics based on UUID 002.
Below Code what I'm been trying
#include <bluefruit.h>
uint16_t epds_uuid ;
uint16_t epdc_uuid ;
BLEService epdS /*= BLEService(epds_uuid)*/;
BLECharacteristic epdC /*= BLECharacteristic(epdc_uuid)*/;
BLEDis bledis; // DIS (Device Information Service) helper class instance
uint8_t bps = 0;
void setup()
{
Serial.begin(115200);
while ( !Serial ) delay(10); // for nrf52840 with native usb
Serial.println("Bluefruit52 HRM Example");
Serial.println("-----------------------\n");
// Initialise the Bluefruit module
Serial.println("Initialise the Bluefruit nRF52 module");
Bluefruit.begin();
// 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 Feather52");
bledis.begin();
// Setup the Heart Rate Monitor service using
// BLEService and BLECharacteristic classes
Serial.println("Configuring the EPD Image Service");
setupEPD();
// 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(epdS);
// Include Name
Bluefruit.Advertising.addName();
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
}
uint8_t change_uuid = 0;
void setupEPD(void)
{
if (change_uuid == 0)
{
Serial.println("UUID ; 001");
epds_uuid = 0x1234;
epdc_uuid = 0x1244;
}
else
{
Serial.println("UUID ; 002");
epds_uuid = 0x1236;
epdc_uuid = 0x1248;
}
++change_uuid;
epdS = BLEService(epds_uuid);
epdC = BLECharacteristic(epdc_uuid);
epdS.begin();
epdC.setProperties(CHR_PROPS_NOTIFY);
epdC.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
epdC.setFixedLen(2);
epdC.setCccdWriteCallback(cccd_callback); // Optionally capture CCCD updates
epdC.begin();
}
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!");
setup();
}
void cccd_callback(uint16_t conn_hdl, BLECharacteristic* chr, uint16_t cccd_value)
{
// Display the raw request packet
Serial.print("CCCD Updated: ");
//Serial.printBuffer(request->data, request->len);
Serial.print(cccd_value);
Serial.println("");
// Check the characteristic this CCCD update is associated with in case
// this handler is used for multiple CCCD records.
if (chr->uuid == epdC.uuid) {
if (chr->notifyEnabled(conn_hdl)) {
Serial.println("EPD Image 'Notify' enabled");
} else {
Serial.println("EPD Image 'Notify' disabled");
}
}
}
#include "image1data.h"
int once = 0;
void loop()
{
digitalToggle(LED_RED);
if ( Bluefruit.connected() ) {
if (once == 0)
{
epdC.notify(image1data, 4000);
delay(1000);
}
}
}