add bleancs setNotificationCallback()
This commit is contained in:
1 parent
7c932db50f
commit
041a42d904
3 files changed
+55
-11
No files matched your search
@@ -31,6 +31,7 @@ void setup()
|
||||
|
||||
// Configure and Start Service
|
||||
bleancs.begin();
|
||||
bleancs.setNotificationCallback(ancs_notification_callback);
|
||||
|
||||
// Set up Advertising Packet
|
||||
setupAdv();
|
||||
@@ -76,6 +77,23 @@ void loop()
|
||||
}
|
||||
}
|
||||
|
||||
void ancs_notification_callback(ancsNotification_t* notif)
|
||||
{
|
||||
// Check BLEAncs.h for ancsNotification_t
|
||||
const char* event_str[] = { "Added", "Modified", "Removed" };
|
||||
const char* cat_str [] =
|
||||
{
|
||||
"Other" , "Incoming Call" , "Missed Call", "Voice Mail" ,
|
||||
"Social" , "Schedule" , "Email" , "News" ,
|
||||
"Health and Fitness", "Business and Finance", "Location" , "Entertainment"
|
||||
};
|
||||
|
||||
Serial.printf("[Notification %s] ", event_str[notif->eventID]);
|
||||
Serial.printf("%s (%d) ", cat_str[notif->categoryID], notif->categoryCount);
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void connect_callback(void)
|
||||
{
|
||||
Serial.println("Connected");
|
||||
|
||||
@@ -36,8 +36,8 @@
|
||||
|
||||
#include "bluefruit.h"
|
||||
|
||||
void bleancs_notification_notify_cb(BLECentralCharacteristic& chr, uint8_t* data, uint16_t len);
|
||||
void bleancs_data_notify_cb(BLECentralCharacteristic& chr, uint8_t* data, uint16_t len);
|
||||
void bleancs_notification_cb(BLECentralCharacteristic& chr, uint8_t* data, uint16_t len);
|
||||
void bleancs_data_cb(BLECentralCharacteristic& chr, uint8_t* data, uint16_t len);
|
||||
|
||||
/* ANCS Service : 7905F431-B5CE-4E99-A40F-4B1E122D00D0
|
||||
* Notification Source : 9FBF120D-6301-42D9-8C58-25E699A21DBD
|
||||
@@ -73,7 +73,7 @@ BLEAncs::BLEAncs(void)
|
||||
: BLECentralService(BLEANCS_UUID_SERVICE), _control(BLEANCS_UUID_CHR_CONTROL),
|
||||
_notification(BLEANCS_UUID_CHR_NOTIFICATION), _data(BLEANCS_UUID_CHR_DATA)
|
||||
{
|
||||
|
||||
_notif_cb = NULL;
|
||||
}
|
||||
|
||||
bool BLEAncs::begin(void)
|
||||
@@ -88,12 +88,17 @@ bool BLEAncs::begin(void)
|
||||
_notification.begin();
|
||||
_data.begin();
|
||||
|
||||
_notification.setNotifyCallback(bleancs_notification_notify_cb);
|
||||
_data.setNotifyCallback(bleancs_data_notify_cb);
|
||||
_notification.setNotifyCallback(bleancs_notification_cb);
|
||||
_data.setNotifyCallback(bleancs_data_cb);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BLEAncs::setNotificationCallback(notification_callback_t fp)
|
||||
{
|
||||
_notif_cb = fp;
|
||||
}
|
||||
|
||||
bool BLEAncs::enableNotification(void)
|
||||
{
|
||||
// enable both Nofication & Data Source
|
||||
@@ -132,20 +137,30 @@ void BLEAncs::disconnect(void)
|
||||
BLECentralService::disconnect();
|
||||
}
|
||||
|
||||
void BLEAncs::_handleNotification(uint8_t* data, uint16_t len)
|
||||
{
|
||||
if ( len != 8 ) return;
|
||||
|
||||
if ( _notif_cb ) _notif_cb((ancsNotification_t*) data);
|
||||
}
|
||||
|
||||
void BLEAncs::_handleData(uint8_t* data, uint16_t len)
|
||||
{
|
||||
PRINT_BUFFER(data, len);
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Callback
|
||||
*------------------------------------------------------------------*/
|
||||
void bleancs_notification_notify_cb(BLECentralCharacteristic& chr, uint8_t* data, uint16_t len)
|
||||
void bleancs_notification_cb(BLECentralCharacteristic& chr, uint8_t* data, uint16_t len)
|
||||
{
|
||||
BLEAncs& svc = (BLEAncs&) chr.parentService();
|
||||
|
||||
PRINT_BUFFER(data, len);
|
||||
svc._handleNotification(data, len);
|
||||
}
|
||||
|
||||
void bleancs_data_notify_cb(BLECentralCharacteristic& chr, uint8_t* data, uint16_t len)
|
||||
void bleancs_data_cb(BLECentralCharacteristic& chr, uint8_t* data, uint16_t len)
|
||||
{
|
||||
BLEAncs& svc = (BLEAncs&) chr.parentService();
|
||||
|
||||
PRINT_BUFFER(data, len);
|
||||
svc._handleData(data, len);
|
||||
}
|
||||
@@ -128,11 +128,14 @@ VERIFY_STATIC( sizeof(ancsNotification_t) == 8);
|
||||
class BLEAncs : public BLECentralService
|
||||
{
|
||||
public:
|
||||
typedef void (*notification_callback_t) (ancsNotification_t* notif);
|
||||
|
||||
BLEAncs(void);
|
||||
|
||||
virtual bool begin(void);
|
||||
virtual bool discover(uint16_t conn_handle);
|
||||
|
||||
void setNotificationCallback(notification_callback_t fp);
|
||||
bool enableNotification(void);
|
||||
bool disableNotification(void);
|
||||
|
||||
@@ -143,6 +146,14 @@ class BLEAncs : public BLECentralService
|
||||
BLECentralCharacteristic _control;
|
||||
BLECentralCharacteristic _notification;
|
||||
BLECentralCharacteristic _data;
|
||||
|
||||
notification_callback_t _notif_cb;
|
||||
|
||||
void _handleNotification(uint8_t* data, uint16_t len);
|
||||
void _handleData(uint8_t* data, uint16_t len);
|
||||
|
||||
friend void bleancs_notification_cb(BLECentralCharacteristic& chr, uint8_t* data, uint16_t len);
|
||||
friend void bleancs_data_cb (BLECentralCharacteristic& chr, uint8_t* data, uint16_t len);
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user