// @author James Hudson bugs.feedback.whatever@gmail.com
// @todo license text and header - please see github project for license
// A simple example program to retrieve notifications from your device, and output them to the Serial console.
// Header for this library, from https://www.github.com/Smartphone-Companions/ESP32-ANCS-Notifications.git
#include "esp32notifications.h"
/////
// Different hardware presets; uncomment the correct device.
// Or feel free to add your own board layout for your specific hardware.
//////////
// Example code begins
// Create an interface to the BLE notification library
BLENotifications notifications;
// Holds the incoming call's ID number, or zero if no notification
uint32_t incomingCallNotificationUUID;
// This callback will be called when a Bluetooth LE connection is made or broken.
// You can update the ESP 32's UI or take other action here.
void onBLEStateChanged(BLENotifications::State state) {
switch(state) {
case BLENotifications::StateConnected:
Serial.println("StateConnected - connected to a phone or tablet");
break;
case BLENotifications::StateDisconnected:
Serial.println("StateDisconnected - disconnected from a phone or tablet");
/* We need to startAdvertising on disconnection, otherwise the ESP 32 will now be invisible.
IMO it would make sense to put this in the library to happen automatically, but some people in the Espressif forums
were requesting that they would like manual control over re-advertising.*/
notifications.startAdvertising();
break;
}
}
// A notification arrived from the mobile device, ie a social media notification or incoming call.
// parameters:
// - notification: an Arduino-friendly structure containing notification information. Do not keep a
// pointer to this data - it will be destroyed after this function.
// - rawNotificationData: a pointer to the underlying data. It contains the same information, but is
// not beginner-friendly. For advanced use-cases.
void onNotificationArrived(const ArduinoNotification * notification, const Notification * rawNotificationData) {
Serial.print("Got notification: ");
Serial.println(notification->title); // The title, ie name of who sent the message
Serial.println(notification->message); // The detail, ie "be home for dinner at 7".
Serial.println(notification->type); // Which app sent it
Serial.println(notifications.getNotificationCategoryDescription(notification->category)); // ie "social media"
Serial.println(notification->categoryCount); // How may other notifications are there from this app (ie badge number)
}
// A notification was cleared
void onNotificationRemoved(const ArduinoNotification * notification, const Notification * rawNotificationData) {
Serial.print("Removed notification: ");
Serial.println(notification->title);
Serial.println(notification->message);
Serial.println(notification->type);
}
// Standard Arduino function which is called once when the device first starts up
void setup() {
// Button configuration. It is usual to have buttons configured as INPUT_PULLUP in the hardware design,
// but check the details for your specific device
Serial.begin(115200);
while(!Serial) {
delay(10);
}
Serial.println("ESP32-ANCS-Notifications Example");
Serial.println("------------------------------------------");
// Set up the BLENotification library
notifications.begin("BLEConnection");
notifications.setConnectionStateChangedCallback(onBLEStateChanged);
notifications.setNotificationCallback(onNotificationArrived);
notifications.setRemovedCallback(onNotificationRemoved);
}
// Standard Arduino function that is called in an endless loop after setup
void loop() {
}
After loading the sketch on the ESP 32 WROOM, the board goes into a loop and reloads after each attempt to start listening.
Log:
Code: