add Central Char enable/disable notify,indicate()
This commit is contained in:
1 parent
e38908b9da
commit
108b6555a1
8 files changed
+93
-8
No files matched your search
@@ -100,7 +100,7 @@ void loop()
|
||||
Serial.println("Found it");
|
||||
|
||||
Serial.println("Enable TXD's notify");
|
||||
bleCentralUart.enableNotify();
|
||||
bleCentralUart.enableTXD();
|
||||
|
||||
Serial.println("Ready to receive from peripheral");
|
||||
}else
|
||||
|
||||
@@ -58,8 +58,11 @@ void loop()
|
||||
// Discover service if not yet
|
||||
if ( !bleancs.discovered() )
|
||||
{
|
||||
Serial.print("Discovering ANCS ... ");
|
||||
if ( bleancs.discover( Bluefruit.connHandle() ) )
|
||||
{
|
||||
Serial.println("Found it");
|
||||
Serial.println("Enable Notification Source's CCCD");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ bool BLECentralCharacteristic::discoverDescriptor(uint16_t conn_handle)
|
||||
if ( disc_rsp.descs[i].uuid.type == BLE_UUID_TYPE_BLE &&
|
||||
disc_rsp.descs[i].uuid.uuid == BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG )
|
||||
{
|
||||
LOG_LV1(BLECentralUart, "Found CCDD: handle = %d", disc_rsp.descs[i].handle);
|
||||
LOG_LV1(Discovery, "Found CCDD: handle = %d", disc_rsp.descs[i].handle);
|
||||
_cccd_handle = disc_rsp.descs[i].handle;
|
||||
}
|
||||
}
|
||||
@@ -205,9 +205,8 @@ void BLECentralCharacteristic::setNotifyCallback(notify_cb_t fp)
|
||||
_notify_cb = fp;
|
||||
}
|
||||
|
||||
bool BLECentralCharacteristic::enableNotify(void)
|
||||
bool BLECentralCharacteristic::writeCCCD(uint16_t value)
|
||||
{
|
||||
uint16_t value = 0x0001;
|
||||
ble_gattc_write_params_t param =
|
||||
{
|
||||
.write_op = BLE_GATT_OP_WRITE_CMD,
|
||||
@@ -226,6 +225,28 @@ bool BLECentralCharacteristic::enableNotify(void)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BLECentralCharacteristic::enableNotify(void)
|
||||
{
|
||||
VERIFY( _chr.char_props.notify );
|
||||
return writeCCCD(0x0001);
|
||||
}
|
||||
|
||||
bool BLECentralCharacteristic::disableNotify(void)
|
||||
{
|
||||
return writeCCCD(0x0000);
|
||||
}
|
||||
|
||||
bool BLECentralCharacteristic::enableIndicate (void)
|
||||
{
|
||||
VERIFY( _chr.char_props.indicate );
|
||||
return writeCCCD(0x0002);
|
||||
}
|
||||
|
||||
bool BLECentralCharacteristic::disableIndicate (void)
|
||||
{
|
||||
return writeCCCD(0x0000);
|
||||
}
|
||||
|
||||
void BLECentralCharacteristic::_eventHandler(ble_evt_t* evt)
|
||||
{
|
||||
switch(evt->header.evt_id)
|
||||
|
||||
@@ -65,7 +65,13 @@ class BLECentralCharacteristic
|
||||
uint16_t write_resp(const void* data, int len);
|
||||
|
||||
/*------------- Notify -------------*/
|
||||
bool enableNotify(void);
|
||||
bool writeCCCD (uint16_t value);
|
||||
|
||||
bool enableNotify (void);
|
||||
bool disableNotify (void);
|
||||
|
||||
bool enableIndicate (void);
|
||||
bool disableIndicate (void);
|
||||
|
||||
|
||||
/*------------- Callbacks -------------*/
|
||||
|
||||
@@ -59,11 +59,16 @@ bool BLECentralUart::begin(void)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BLECentralUart::enableNotify(void)
|
||||
bool BLECentralUart::enableTXD(void)
|
||||
{
|
||||
return _txd.enableNotify();
|
||||
}
|
||||
|
||||
bool BLECentralUart::disableTXD(void)
|
||||
{
|
||||
return _txd.disableNotify();
|
||||
}
|
||||
|
||||
void BLECentralUart::setRxCallback( rx_callback_t fp)
|
||||
{
|
||||
_rx_cb = fp;
|
||||
|
||||
@@ -54,9 +54,11 @@ class BLECentralUart : public BLECentralService, public Stream
|
||||
virtual bool begin(void);
|
||||
virtual bool discover(uint16_t conn_handle);
|
||||
|
||||
bool enableNotify(void);
|
||||
void setRxCallback( rx_callback_t fp);
|
||||
|
||||
bool enableTXD(void);
|
||||
bool disableTXD(void);
|
||||
|
||||
// Stream API
|
||||
virtual int read ( void );
|
||||
virtual int read ( uint8_t * buf, size_t size );
|
||||
|
||||
@@ -36,6 +36,9 @@
|
||||
|
||||
#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);
|
||||
|
||||
/* ANCS Service : 7905F431-B5CE-4E99-A40F-4B1E122D00D0
|
||||
* Notification Source : 9FBF120D-6301-42D9-8C58-25E699A21DBD
|
||||
* Control Point : 69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9
|
||||
@@ -85,6 +88,26 @@ bool BLEAncs::begin(void)
|
||||
_notification.begin();
|
||||
_data.begin();
|
||||
|
||||
_notification.setNotifyCallback(bleancs_notification_notify_cb);
|
||||
_data.setNotifyCallback(bleancs_data_notify_cb);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BLEAncs::enableNotification(void)
|
||||
{
|
||||
// enable both Nofication & Data Source
|
||||
_notification.enableNotify();
|
||||
_data.enableNotify();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BLEAncs::disableNotification(void)
|
||||
{
|
||||
_notification.disableNotify();
|
||||
_data.disableNotify();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -100,10 +123,32 @@ bool BLEAncs::discover(uint16_t conn_handle)
|
||||
VERIFY( 3 == Bluefruit.Discovery.discoverCharacteristic(conn_handle, chr_arr, 3) );
|
||||
|
||||
_discovered = true;
|
||||
|
||||
// ANCS requires bonding to work, it makes sense to request security here as well
|
||||
Bluefruit.requestBonding();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BLEAncs::disconnect(void)
|
||||
{
|
||||
|
||||
BLECentralService::disconnect();
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Callback
|
||||
*------------------------------------------------------------------*/
|
||||
void bleancs_notification_notify_cb(BLECentralCharacteristic& chr, uint8_t* data, uint16_t len)
|
||||
{
|
||||
BLEAncs& svc = (BLEAncs&) chr.parentService();
|
||||
|
||||
PRINT_BUFFER(data, len);
|
||||
}
|
||||
|
||||
void bleancs_data_notify_cb(BLECentralCharacteristic& chr, uint8_t* data, uint16_t len)
|
||||
{
|
||||
BLEAncs& svc = (BLEAncs&) chr.parentService();
|
||||
|
||||
PRINT_BUFFER(data, len);
|
||||
}
|
||||
@@ -54,6 +54,9 @@ class BLEAncs : public BLECentralService
|
||||
virtual bool begin(void);
|
||||
virtual bool discover(uint16_t conn_handle);
|
||||
|
||||
bool enableNotification(void);
|
||||
bool disableNotification(void);
|
||||
|
||||
protected:
|
||||
virtual void disconnect(void);
|
||||
|
||||
|
||||
Reference in new issue
Block a user