mirror of
https://github.com/Smartphone-Companions/ESP32-ANCS-Notifications.git
synced 2025-01-22 11:28:29 +00:00
Removed one hack from the old BLE32 libraries so the library builds on a pure ESP32 Espressif Arduino library.
This commit is contained in:
parent
8194511b31
commit
60d8063232
@ -1,79 +0,0 @@
|
||||
// @author James Hudson bugs.feedback.whatever@gmail.com
|
||||
// @todo license text and header - please see github project for license
|
||||
|
||||
// You will need to add these hardware buttons
|
||||
#define BUTTON_A 4 // left button
|
||||
#define BUTTON_B 7 // center switch
|
||||
#define BUTTON_C 5 // right button
|
||||
|
||||
|
||||
BLENotifications notifications;;
|
||||
|
||||
void setup() {
|
||||
// Button configuration
|
||||
pinMode(BUTTON_A, INPUT_PULLUP);
|
||||
pinMode(BUTTON_B, INPUT_PULLUP);
|
||||
pinMode(BUTTON_C, INPUT_PULLUP);
|
||||
|
||||
Serial.begin(115200);
|
||||
while(!Serial) {
|
||||
delay(10);
|
||||
}
|
||||
|
||||
Serial.println("BLENotifications BLE ANCS on ESP32 Example");
|
||||
Serial.println("------------------------------------------");
|
||||
|
||||
notifications.begin()
|
||||
notifications.setName("deviceName");
|
||||
notifications.setConnectCallback(connect_callback);
|
||||
notifications.setDisconnectCallback(disconnect_callback);
|
||||
notifications.setNotificationCallback(notification_callback);
|
||||
}
|
||||
|
||||
void notification_callback(AncsNotification_t* notif)
|
||||
{
|
||||
Serial.println("Got notifications");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Callback invoked when a BLE connection is made.
|
||||
*/
|
||||
void connect_callback(uint16_t conn_handle)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Callback invoked when a connection is dropped
|
||||
* @param conn_handle
|
||||
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
|
||||
*/
|
||||
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
|
||||
{
|
||||
(void) reason;
|
||||
|
||||
Serial.println();
|
||||
Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
if (BLENotifications.numPending == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
checkButtons();
|
||||
|
||||
notifications.actionPositive();
|
||||
notifications.actionNegative();
|
||||
}
|
||||
|
||||
/*
|
||||
X- Connect/Disconnect
|
||||
X- Set Name of Peripheral
|
||||
X- Notification added
|
||||
- Notification removed
|
||||
- Notification Attributes
|
||||
- Perform Notification Actions (positive or negative)
|
||||
*/
|
87
examples/ble_connection/ble_connection.ino
Normal file
87
examples/ble_connection/ble_connection.ino
Normal file
@ -0,0 +1,87 @@
|
||||
// @author James Hudson bugs.feedback.whatever@gmail.com
|
||||
// @todo license text and header - please see github project for license
|
||||
|
||||
#include "esp32notifications.h"
|
||||
|
||||
// Different hardware presets; feel free to add your own board layout.
|
||||
// You will need to add these hardware buttons to your dev board.
|
||||
// See the ESP32 pinout to choose a free GPIO pin.
|
||||
#define HARDWARE_HACKWATCH
|
||||
|
||||
#ifdef HARDWARE_HACKWATCH
|
||||
#define BUTTON_A 25 // left button
|
||||
#define BUTTON_B 26 // center button
|
||||
#define BUTTON_C 27 // right button
|
||||
#else
|
||||
#error Hardware buttons not supported!
|
||||
#endif
|
||||
|
||||
|
||||
BLENotifications notifications;
|
||||
|
||||
void onBLEStateChanged(BLENotifications::State state) {
|
||||
switch(state) {
|
||||
case BLENotifications::StateConnected:
|
||||
Serial.println("StateConnected");
|
||||
break;
|
||||
|
||||
case BLENotifications::StateDisconnected:
|
||||
Serial.println("StateDisconnected");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void onNotificationArrived() {
|
||||
Serial.println("Got notification.");
|
||||
}
|
||||
|
||||
|
||||
void setup() {
|
||||
// Button configuration
|
||||
pinMode(BUTTON_A, INPUT_PULLUP);
|
||||
pinMode(BUTTON_B, INPUT_PULLUP);
|
||||
pinMode(BUTTON_C, INPUT_PULLUP);
|
||||
|
||||
Serial.begin(115200);
|
||||
while(!Serial) {
|
||||
delay(10);
|
||||
}
|
||||
|
||||
Serial.println("BLENotifications BLE ANCS on ESP32 Example");
|
||||
Serial.println("------------------------------------------");
|
||||
|
||||
notifications.begin("deviceName");
|
||||
notifications.setConnectionStateChangedCallback(onBLEStateChanged);
|
||||
notifications.setNotificationCallback(onNotificationArrived);
|
||||
}
|
||||
|
||||
|
||||
void checkButtons() {
|
||||
|
||||
if (digitalRead(BUTTON_A) == LOW) {
|
||||
Serial.println("Positive action.");
|
||||
notifications.actionPositive();
|
||||
}
|
||||
else if (digitalRead(BUTTON_C) == LOW) {
|
||||
Serial.println("Negative action.");
|
||||
notifications.actionNegative();
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (notifications.getNumPending() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
checkButtons();
|
||||
}
|
||||
|
||||
/*
|
||||
X- Connect/Disconnect
|
||||
X- Set Name of Peripheral
|
||||
X- Notification added
|
||||
- Notification removed
|
||||
- Notification Attributes
|
||||
- Perform Notification Actions (positive or negative)
|
||||
*/
|
10
library.properties
Normal file
10
library.properties
Normal file
@ -0,0 +1,10 @@
|
||||
name=BLENotification ESP32 Library
|
||||
version=0.0.1
|
||||
author=Smartphone Companions
|
||||
maintainer=James Hudson <bugs.feedback.whatever@gmail.com>
|
||||
sentence=Arduino library for ESP32, for interacting with BLE notifications.
|
||||
paragraph=Arduino library for ESP32, for interacting with BLE notifications.
|
||||
category=Communication
|
||||
url=https://github.com/Smartphone-Companions
|
||||
architectures=esp32
|
||||
includes=esp32notifications.h
|
@ -1,17 +1,109 @@
|
||||
// Based on https://github.com/espressif/esp-idf/tree/master/examples/bluetooth/bluedroid/ble/ble_ancs
|
||||
|
||||
#include "esp32notifications.h"
|
||||
|
||||
BLENotifications::BLENotifications() {
|
||||
#include "BLEAddress.h"
|
||||
#include "BLEDevice.h"
|
||||
#include "BLEServer.h"
|
||||
#include "BLEClient.h"
|
||||
#include "BLEUtils.h"
|
||||
#include "BLE2902.h"
|
||||
|
||||
#include <esp_gatts_api.h>
|
||||
|
||||
static char LOG_TAG[] = "BLENotifications";
|
||||
|
||||
class MySecurity : public BLESecurityCallbacks {
|
||||
|
||||
uint32_t onPassKeyRequest(){
|
||||
ESP_LOGI(LOG_TAG, "PassKeyRequest");
|
||||
return 123456;
|
||||
}
|
||||
|
||||
void onPassKeyNotify(uint32_t pass_key){
|
||||
ESP_LOGI(LOG_TAG, "On passkey Notify number:%d", pass_key);
|
||||
}
|
||||
|
||||
bool onSecurityRequest(){
|
||||
ESP_LOGI(LOG_TAG, "On Security Request");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool onConfirmPIN(unsigned int){
|
||||
ESP_LOGI(LOG_TAG, "On Confrimed Pin Request");
|
||||
return true;
|
||||
}
|
||||
|
||||
void onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl){
|
||||
ESP_LOGI(LOG_TAG, "Starting BLE work!");
|
||||
if(cmpl.success){
|
||||
uint16_t length;
|
||||
esp_ble_gap_get_whitelist_size(&length);
|
||||
ESP_LOGD(LOG_TAG, "size: %d", length);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class MyServerCallbacks: public BLEServerCallbacks {
|
||||
private:
|
||||
struct gatts_connect_evt_param { // @todo include from sdk/include/bt/esp_gatts_api.h
|
||||
uint16_t conn_id; /*!< Connection id */
|
||||
esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */
|
||||
} connect;
|
||||
|
||||
public:
|
||||
BLENotifications * instance;
|
||||
|
||||
MyServerCallbacks(BLENotifications * parent)
|
||||
: instance(parent) {
|
||||
}
|
||||
|
||||
void onConnect(BLEServer* pServer, esp_ble_gatts_cb_param_t *param) {
|
||||
ESP_LOGI(LOG_TAG, "Device connected");
|
||||
Serial.println("**Device connected**");
|
||||
gatts_connect_evt_param * connectEventParam = (gatts_connect_evt_param *) param;
|
||||
Serial.println(BLEAddress(connectEventParam->remote_bda).toString().c_str());
|
||||
/*MyClient* pMyClient = new MyClient();
|
||||
pMyClient->setStackSize(18000);
|
||||
pMyClient->start(new BLEAddress(BLEDevice::m_remoteBda));*/ // @todo - memory leak?
|
||||
if (instance->cbStateChanged) {
|
||||
instance->cbStateChanged(BLENotifications::StateConnected);
|
||||
}
|
||||
};
|
||||
|
||||
void onDisconnect(BLEServer* pServer) {
|
||||
ESP_LOGI(LOG_TAG, "Device disconnected");
|
||||
Serial.println("**Device disconnected**");
|
||||
if (instance->cbStateChanged) {
|
||||
instance->cbStateChanged(BLENotifications::StateDisconnected);
|
||||
}
|
||||
// @todo, disconnect, free stack?
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
BLENotifications::BLENotifications()
|
||||
: cbStateChanged(0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool BLENotifications::begin(const char * name) {
|
||||
return false;
|
||||
BLEDevice::init(name);
|
||||
server = BLEDevice::createServer();
|
||||
server->setCallbacks(new MyServerCallbacks(this));
|
||||
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT);
|
||||
BLEDevice::setSecurityCallbacks(new MySecurity());
|
||||
|
||||
startAdvertising();
|
||||
}
|
||||
|
||||
|
||||
void BLENotifications::setConnectionStateChangedCallback(ble_notifications_state_changed_t) {
|
||||
|
||||
void BLENotifications::setConnectionStateChangedCallback(ble_notifications_state_changed_t callback) {
|
||||
cbStateChanged = callback;
|
||||
}
|
||||
|
||||
|
||||
@ -29,4 +121,22 @@ void BLENotifications::actionNegative() {
|
||||
|
||||
}
|
||||
|
||||
void BLENotifications::startAdvertising() {
|
||||
// Start soliciting ANCS
|
||||
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
|
||||
BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
|
||||
oAdvertisementData.setFlags(0x01);
|
||||
oAdvertisementData.setServiceSolicitation(BLEUUID("7905F431-B5CE-4E99-A40F-4B1E122D00D0"));
|
||||
pAdvertising->setAdvertisementData(oAdvertisementData);
|
||||
|
||||
// Set security
|
||||
BLESecurity *pSecurity = new BLESecurity();
|
||||
pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_BOND);
|
||||
pSecurity->setCapability(ESP_IO_CAP_OUT);
|
||||
pSecurity->setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK);
|
||||
|
||||
//Start advertising
|
||||
pAdvertising->start();
|
||||
}
|
||||
|
||||
|
||||
|
@ -3,14 +3,19 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
/**
|
||||
* Arduino library for the ESP32, for receiving BLE notifications from another device.
|
||||
*
|
||||
* This class was designed with simplicity and ease-of-use in mind.
|
||||
*/
|
||||
class BLENotifications {
|
||||
public:
|
||||
/**
|
||||
* State of the BLE connection.
|
||||
*/
|
||||
enum State {
|
||||
StateConnected,
|
||||
StateDisconnected
|
||||
StateConnected, /// A device connected to the ESP32
|
||||
StateDisconnected /// A device disconnected from the ESP32
|
||||
};
|
||||
|
||||
/**
|
||||
@ -41,6 +46,16 @@ class BLENotifications {
|
||||
void actionNegative();
|
||||
|
||||
int getNumPending() const { return 0; }
|
||||
|
||||
private:
|
||||
void startAdvertising();
|
||||
|
||||
private:
|
||||
ble_notifications_state_changed_t cbStateChanged;
|
||||
|
||||
class BLEServer* server;
|
||||
|
||||
friend class MyServerCallbacks; //Allow internal handlers to access the callbacks of the main class
|
||||
};
|
||||
|
||||
#endif // ESP32NOTIFICATIONS_H_
|
||||
|
Loading…
Reference in New Issue
Block a user